using System.Threading.Tasks;
using Godot;
using KarrotStarterTemplate.Common.AnimNotifies;
using KarrotStarterTemplate.Common.CameraController;
using KarrotStarterTemplate.Common.CharacterMovement;

namespace KarrotStarterTemplate.Common.StateMachines.Custom.PlayerStateMachines;

public abstract partial class BasePlayerState : State
{

    [Export] public string transitionName { get; set; }
    public CharacterMovementController characterMovement  => basePlayerStateMachine.characterMovement;
    protected BasePlayerStateMachine basePlayerStateMachine => (BasePlayerStateMachine)parentStateMachine;
    protected CharacterBody3D characterBody => basePlayerStateMachine?.ownerCharacterBody;
    protected CharacterCameraController cameraController => basePlayerStateMachine?.cameraController;
    protected AnimationTree animationTree => basePlayerStateMachine?.animationTree;
    protected AnimNotify animationNotify => basePlayerStateMachine?.animationNotify;

    protected string transitionRequestPath => $"parameters/Transition/transition_request";

    protected override Task OnEnter()
    {
        if (transitionName != null)
        {
            animationTree.Set(transitionRequestPath, transitionName);
            animationNotify.AnimationNotify += OnAnimationNotify;
        }
        
        return Task.CompletedTask;
    }
    
    protected override Task OnExit()
    {
        animationNotify.AnimationNotify -= OnAnimationNotify;
        return Task.CompletedTask;
    }

    protected override string OnCheckRelevance()
    {
        if (package.GetBool("roll"))
        {
            return "Roll";
        }
        
        if (!package.GetVector3("move_direction").IsZeroApprox())
        {
            if (package.GetBool("run"))
            {
                return "Run";
            }
            
            return "Walk";
        }

        return "Idle";
    }

    protected virtual void OnAnimationNotify(string name, string actionName)
    {
    }

}