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

namespace Intrepid.Gameplay.Entities.Player.Sentinel.StateMachine.States
{
    public class SentinelStateWalk : SentinelState
    {
        [GroupConfigurations, SerializeField] private AnimationClip walk;
        [GroupConfigurations, SerializeField, Range(0, 100)] private float animationSpeed = 2;

        private Vector2 Movement { get; set; }
        
        public override SentinelStateType GetStateKey()
        {
            return SentinelStateType.Walk;
        }

        protected override void Subscriptions()
        {
        }

        protected override void RunOnEnter(ParameterStore parameterStore)
        {
            Brain.Entity.ModelAnimation.CrossFade(walk, 0.15f, 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.Walk.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.Run);
                    return true;
                default:
                    return false;
            }
        }
    }
}