using System.Collections.Generic;
using System.Linq;
using Cirno.Scripts.Components;
using Godot;
using Godot.Collections;

namespace Cirno.Scripts.Resources;

[GlobalClass]
[Tool]
public partial class BulletResource : Resource
{
    [Export]
    public PackedScene BulletScene { get; set; }
    [Export] public PackedScene DestructionParticlesScene { get; set; }
    [Export] public float BulletSpeed = 100f;
    [Export] public Vector2 Direction = Vector2.Right;
    [Export] public float BulletDamage = 1;
    [Export] public float MaxDamage = 1;
    [Export] public float Knockback = 1;
    [Export] public float LifeTime = 10f;
    [Export] public bool DestroyOnCollision = true;
    [Export] public BulletOwner Owner = BulletOwner.None;
    [Export] public DamageType DamageType = DamageType.Neutral;
    [Export] public bool RotateSprite = false;
    [Export] public bool Controllable = false;
    [Export] public bool Freezable { get; set; } = true;
    [Export] public bool Grazeable { get; set; } = true;
    [Export] public float GrazeValue { get; set; } = 0.2f;

    [Export] public BulletFlags Attributes { get; set; }
    
    [Export]
    public BulletCreationModifier Modifier;
    [Export] public Array<TimeModifier> TimeModifiers;

    public BulletInfo MakeBullet(Vector2 position, int count = 1, float spread = 0f, float rotationOffset = 0f)
    {
        return new BulletInfo(this)
        {
            Position = position,
            Direction = Direction,
            Speed = BulletSpeed,
            Owner = Owner,
            DamageType = DamageType,
            Damage = BulletDamage,
            Knockback = Knockback,
            BulletCount = count,
            Spread = spread,
            BulletScene = BulletScene,
            RotationOffset = rotationOffset,
            Modifier = Modifier,
            LifeTime = LifeTime,
            DestroyOnCollision = DestroyOnCollision,
            DestructionParticlesScene = DestructionParticlesScene,
            RotateSprite = RotateSprite,
            Controllabe = Controllable,
            Freezable = Freezable,
            TimeModifiers = TimeModifiers.Select(x => x).ToList(),
            Grazeable = Grazeable,
            GrazeValue = GrazeValue,
            Attributes = Attributes,
            // TimeModifiers = TimeModifiers?.Where(mod => mod is TimeModifier).Cast<TimeModifier>().Select(m => new ModifierWrapper()
            // {
            //     TimeModifier = m,
            //     Applied = false
            // }).ToList()
            // TimeModifiers = TimeModifiers?.Select(x => new ModifierWrapper()
            // {
            //     Applied = false,
            //     TimeModifier = x
            // })
            // TimeModifiers = TimeModifiers?.Where(mod => mod is TimeModifier).Cast<TimeModifier>().ToList() ??
            //                 new List<TimeModifier>()
        };
    }
}