using Intrepid.Structures;
using Intrepid.Utilities;
using UnityEngine;

namespace Intrepid.Gameplay.Entities.Player.Sentinel.StateMachine.States
{
    public class SentinelStateRun : SentinelState
    {
        [GroupConfigurations, SerializeField] private AnimationClip run;
        [GroupConfigurations, SerializeField, Range(0, 10)] private float animationSpeed = 3f;
        
        private Vector2 Movement { get; set; }
        
        public override SentinelStateType GetStateKey()
        {
            return SentinelStateType.Run;
        }

        protected override void Subscriptions()
        {
        }

        protected override void RunOnEnter(ParameterStore parameterStore)
        {
            Brain.Entity.ModelAnimation.CrossFade(run, 0.1f, animationSpeed);
        }

        protected override void RunOnExit()
        {
            Brain.Entity.ChangeOrientation(Movement);
        }

        public override void RunOnUpdate()
        {
            if (CheckPDA()) return;
            Movement = Brain.InputResolver.Sentinel.Movement();
            Brain.Entity.ChangeOrientation(Movement);
            VerifyInteraction();
            if (TryConsumeDash())
            {
                Brain.ChangeState(SentinelStateType.DashImpulse);
                return;
            }
            if (CheckMovement()) return;
            Brain.Entity.DesiredVelocity = Movement * Brain.Config.Run.Speed;
        }

        private bool CheckMovement()
        {
            switch (Movement.sqrMagnitude)
            {
                case <= SentinelConstants.Thresholds.Movement.DeadZoneSqrt:
                    Brain.ChangeState(SentinelStateType.Idle);
                    return true;
                case < SentinelConstants.Thresholds.Movement.WalkZoneSqrt:
                    Brain.ChangeState(SentinelStateType.Walk);
                    return true;
                default:
                    return false;
            }
        }
    }
}