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

namespace Cirno.Scripts.Components.FSM.Player;

public partial class Dead : PlayerStateBase
{
    public override PlayerState StateId => PlayerState.Dead;
    
    [Export]
    private PlayerAnimationProvider _animationProvider;
    
    [Export]
    private InputProvider _inputProvider;

    [Export]
    private ActorResourceProvider _healthProvider;
    
    [Export]
    private ActorResourceProvider _motivationProvider;

    private bool _isGameOver = false;
    
    public override void EnterState()
    {
        _animationProvider.PlayDeathAnimation();

        if (_motivationProvider.CurrentResource < 100f)
        {
            // If motivation is not enough show game over scene
            Hud.Instance.ShowGameOver();
            _isGameOver = true;
        }
        else
        {
            // Else show respawn notification
            Hud.Instance.ShowTerminated();
            _isGameOver = false;
        }
    }

    public override void ExitState()
    {
        // Hide game over
        Hud.Instance.HideTerminated();
    }

    public override void ProcessState(double delta)
    {
        // wait for button
        if (_inputProvider.GetShootJustPressed())
        {
            if (_isGameOver)
            {
                // Restart Level
                GlobalState.Instance.RestartLevel();
            }
            else
            {
                Respawn();
            }
            
        }
    }

    public override void PhysicsProcessState(double delta)
    {
        
    }
    
    public void Respawn()
    {
        MainObject.GlobalPosition = GameManager.Instance.LastCheckpointPosition;
        _healthProvider.FillResource();
        GameManager.Instance.ClearBullets();

        _motivationProvider.CurrentResource -= 100f;
        if (_motivationProvider.CurrentResource <= 1f)
        {
            _motivationProvider.CurrentResource = 1f;
        }
        
        ChangeState(PlayerState.Active);
    }
}