using System.Threading.Tasks;
using Cirno.Scripts.Components.Actors;
using Cirno.Scripts.Components.FSM.Enemy;
using Cirno.Scripts.Enums;
using Cirno.Scripts.Resources;
using Cirno.Scripts.Resources.ScriptableBullets;
using Godot;

namespace Cirno.Scripts.Components.FSM.Boss;

public partial class Shooting : EnemyStateBase
{
    public override EnemyState StateId => EnemyState.Shooting;
    [Export] public GenericDamageReceiver DamageReceiver { get; private set; }

    [Export] public Weapon EquippedWeapon;

    [Export] public EnemyStorageModule StorageModule { get; private set; }

    [Export] public BossScriptHostModule BossScriptHostModule { get; private set; }
    
    [Export] public BossScript BossScript { get; set; }

    // private Marker2D _cameraMarker;
    //
    // [Export]
    // public Vector2 CameraOffset = Vector2.Zero;

    private int _currentPhaseIndex = 0;

    public Vector2 HomePosition => StorageModule.HomePosition;

    private BossPhase CurrentPhase => BossScript.Phases[_currentPhaseIndex];

    private bool _waiting = false;

    public float CurrentHealth => DamageReceiver.HealthProvider.CurrentResource;

    public override void Init(IStateMachine<EnemyState, CharacterBody2D> machine)
    {
        base.Init(machine);

        if (StorageModule.Root.EnemyResource.BossScript is not null)
        {
            this.BossScript = StorageModule.Root.EnemyResource.BossScript;
        }
    }

    public override void EnterState()
    {
        base.EnterState();

        DamageReceiver.ChangeState(true);

        DamageReceiver.HealthProvider.ResourceDepleted += HealthProviderOnResourceDepleted;

        StartPhase(CurrentPhase);
        
        //CallDeferred(MethodName.InitDeferred);
    }

    private void InitDeferred()
    {
    }

    private void GrabCamera()
    {
    }

    private void HealthProviderOnResourceDepleted()
    {
        ChangeState(EnemyState.Dead);
    }

    public override void ExitState()
    {
        base.ExitState();

        DamageReceiver.HealthProvider.ResourceDepleted -= HealthProviderOnResourceDepleted;

        DamageReceiver.ChangeState(false);
    }

    public override void PhysicsProcessState(double delta)
    {
        base.PhysicsProcessState(delta);
    }

    public override void ProcessState(double delta)
    {
        base.ProcessState(delta);

        if (_waiting) return;
        CurrentPhase.UpdatePhase(delta);

        if (CurrentHealth <= CurrentPhase.Threshold && _currentPhaseIndex + 1 < BossScript.Phases.Count)
        {
            _currentPhaseIndex++;
            //_bossHud.SpellCardName = CurrentPhase.PhaseName;
            StartPhase(CurrentPhase);
        }
    }

    private void StartPhase(BossPhase phase)
    {
        GameManager.Instance.ClearBullets();
        if (phase.PlayAnimation)
        {
            _waiting = true;

            DamageReceiver.ChangeState(false);
            _ = Switchphase(phase);
        }
        else
        {
            phase.Start(BossScriptHostModule);
        }
    }

    private async Task Switchphase(BossPhase phase)
    {
        await PlayAnimation();

        _waiting = false;
        DamageReceiver.ChangeState(true);
        phase.Start(this);
    }

    private async Task PlayAnimation()
    {
        await Task.Delay(1000);
    }
}