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

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

        protected override void Subscriptions()
        {
        }

        protected override void RunOnEnter(ParameterStore parameterStore)
        {
            Brain.Controls.ResetDash();
            Brain.Entity.ModelAnimation.CrossFade(idle, 0.2f, animationSpeed);
            Brain.Entity.DesiredVelocity = Vector2.zero;
        }

        public override void RunOnUpdate()
        {
            if (CheckPDA()) return;
            Movement = Brain.InputResolver.Sentinel.Movement();
            VerifyInteraction();
            if (TryConsumeDash())
            {
                Brain.ChangeState(SentinelStateType.DashImpulse);
                return;
            }
            if (CheckMovement()) return;
        }
        
        private bool CheckMovement()
        {
            switch (Movement.sqrMagnitude)
            {
                case > SentinelConstants.Thresholds.Movement.DeadZoneSqrt and < SentinelConstants.Thresholds.Movement.WalkZoneSqrt:
                    Brain.ChangeState(SentinelStateType.Walk);
                    return true;
                case >= SentinelConstants.Thresholds.Movement.WalkZoneSqrt:
                    Brain.ChangeState(SentinelStateType.Run);
                    return true;
                default:
                    return false;
            }
        }

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