using Godot;

namespace Cirno.Scripts.Components.Actors;

public partial class FourWayAnimationHandler : AnimationHandler
{
    public override void Update(double delta)
    {
        _animatedSprite.Play(DirectionToString(_parent.FacingDirection));
        _animatedSprite.SpeedScale = _parent.Velocity.Length() > 0 ? 1 : 0;
    }
    
    protected override string DirectionToString(Vector2 direction)
    {
        var angle = Mathf.RadToDeg(direction.Angle());
        angle = Mathf.PosMod(angle, 360);

        if (angle >= 337.5 || angle < 22.5) return _directionsTable[FacingDirection.Right];
        if (angle >= 22.5 && angle < 67.5) return _directionsTable[FacingDirection.Right];
        if (angle >= 67.5 && angle < 112.5) return _directionsTable[FacingDirection.Down];
        if (angle >= 112.5 && angle < 157.5) return _directionsTable[FacingDirection.Left];
        if (angle >= 157.5 && angle < 202.5) return _directionsTable[FacingDirection.Left];
        if (angle >= 202.5 && angle < 247.5) return _directionsTable[FacingDirection.Left];
        if (angle >= 247.5 && angle < 292.5) return _directionsTable[FacingDirection.Up];
        if (angle >= 292.5 && angle < 337.5) return _directionsTable[FacingDirection.Right];

        return _directionsTable[FacingDirection.Up];
    }
}