using System;
using Cirno.Scripts.Components.Actors;
using Cirno.Scripts.Resources;
using Godot;

namespace Cirno.Scripts.Components.FSM.Player;

public partial class PlayerFSMItemUseModule : ModuleBase<PlayerState, CharacterBody2D>
{
    [Export]
    public ActorResourceProvider Health { get; set; }
    
    [Export]
    public ActorResourceProvider Shield { get; set; }
    
    [Export] private PlayerStorageModule _storageModule;
    
    public Vector2 FacingDirection
    {
        get => _storageModule.FacingDirection;
        private set => _storageModule.FacingDirection = value;
    }
    
    public bool Enabled { get; set; } = false;

    public override void EnterState(PlayerState state)
    {
        Enabled = true;
    }

    public override void ExitState(PlayerState state)
    {
        Enabled = false;
    }

    private IStateMachine<PlayerState, CharacterBody2D> _machine;
    public IStateMachine<PlayerState, CharacterBody2D> Machine => _machine;
    
    public override void Init(IStateMachine<PlayerState, CharacterBody2D> machine)
    {
        InventoryManager.Instance.ItemUsed += this.UseItem;
        _machine = machine;
    }

    private void UseItem(LootItem item, int totalcount)
    {
        if (!Enabled) return;
        GD.Print($"Used {item.ItemName} in player");

        item.ItemEffect?.Execute(this, item);

        return;
        
        // switch (item.Item)
        // {
        //     case ItemTypes.KeycardRed:
        //         break;
        //     case ItemTypes.KeycardBlue:
        //         break;
        //     case ItemTypes.KeycardGreen:
        //         break;
        //     case ItemTypes.Ammo:
        //         break;
        //     case ItemTypes.Medkit:
        //         Heal(item);
        //         break;
        //     case ItemTypes.FrogBomb:
        //         SpawnSpiderBomb(item);
        //         break;
        //     case ItemTypes.Bomb:
        //         SpawnBomb(item);
        //         break;
        //     case ItemTypes.Mine:
        //         break;
        //     case ItemTypes.Battery:
        //         RechargeShield(item);
        //         break;
        //     case ItemTypes.Weapon:
        //         break;
        //     case ItemTypes.Power:
        //         break;
        //     case ItemTypes.Points:
        //         break;
        //     case ItemTypes.Credits:
        //         break;
        //     case ItemTypes.KeyItem:
        //         break;
        // }
        
    }

    // private void SpawnBomb(LootItem item)
            // {
            //     GD.Print("Spawned bomb");
            // }
            //
            // private void Heal(LootItem item)
            // {
            //     Health.CurrentResource += Mathf.CeilToInt(Health.MaxResource * 25 / 100);
            // }
            //
            // private void RechargeShield(LootItem item)
            // {
            //     Shield.CurrentResource += Mathf.CeilToInt(Health.MaxResource * 25 / 100);
            // }
            //
            // private void SpawnSpiderBomb(LootItem item)
            // {
            //     //var bullet = item.WeaponData.BulletData.BulletScene.Instantiate<Bullet>();
            //     //bullet.Initialize(item.WeaponData.BulletData.MakeBullet(this.GlobalPosition, 1,0, 0), GameManager.Instance);
            //     
            //
            //     //InventoryManager.Instance.RemoveItem(item.ItemKey, 1);
            //     // emit projectile
            //     var bullet = this.CreateChildOf<Bullet>(GameManager.Instance.BulletsContainer, item.WeaponData.BulletData.BulletScene, this.GlobalPosition);
            //
            //     var bulletData = item.WeaponData.MakeBullet(this.GlobalPosition);
            //
            //     bullet.Initialize(bulletData, GameManager.Instance);
            //     bullet.SetDirection(FacingDirection);
            //     bullet.RotateSpriteDegrees(-90);
            //     //bullet.SetDirection(_facingDirection);
            //     bullet.Speed = item.WeaponData.BulletData.BulletSpeed;
            //     
            //     _machine.SetState(PlayerState.Controlling);
            //     
            //     //RequestMovementDisable(true);
            //     // set camera
            //     GameManager.Instance.CameraTargetObject(bullet);
            //     // set event destroy
            //     bullet.OnDestroy += () =>
            //     {
            //         GameManager.Instance.CameraTargetPlayer();
            //         _machine.SetState(PlayerState.Active);
            //         //RequestMovementDisable(false);
            //     };
            // }

    public override void Process(double delta)
    {
        
    }

    public override void PhysicsProcess(double delta)
    {
        
    }
}