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

namespace Cirno.Scripts.Components.FSM.Enemy;

public partial class AnimationModule : ModuleBase<EnemyState, CharacterBody2D>
{
    private IStateMachine<EnemyState, CharacterBody2D> _machine;
    
    [Export] public PlayerAnimationProvider AnimationProvider { get; set; }
    
    [Export] public EnemyStorageModule StorageModule { get; set; }
    [Export] public ActorResourceProvider HealthProvider { get; set; }
    
    public override void EnterState(EnemyState state)
    {
        AnimationProvider.SetAnimation(StorageModule.AimingDirection);
        AnimationProvider.SetAnimation(Vector2.Zero);
        if (HealthProvider is not null)
        {
            HealthProvider.ResourceDecreased += HealthProviderOnResourceDecreased;
        }
    }

    private void HealthProviderOnResourceDecreased(float oldValue, float newValue, float maxValue)
    {
        AnimationProvider?.Blink();
    }

    public override void ExitState(EnemyState state)
    {
        AnimationProvider.SetAnimation(Vector2.Zero);
        if (HealthProvider is not null)
        {
            HealthProvider.ResourceDecreased -= HealthProviderOnResourceDecreased;
        }
    }

    public override void Init(IStateMachine<EnemyState, CharacterBody2D> machine)
    {
        _machine = machine;
    }

    public override void Process(double delta)
    {
        AnimationProvider.SetAnimation(StorageModule.AimingDirection);

        if (_machine.MainObject.Velocity == Vector2.Zero)
        {
            AnimationProvider.SetAnimation(Vector2.Zero);
        }
    }

    public override void PhysicsProcess(double delta)
    {
        
    }
}