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

namespace Intrepid.Gameplay.Entities.Player.Sentinel.StateMachine.States
{
    public class SentinelStateDashLanding : SentinelState
    {
        [GroupConfigurations, SerializeField] private AnimationClip dashLanding;

        private Timer Timer { get; } = new();
        
        public override SentinelStateType GetStateKey()
        {
            return SentinelStateType.DashLanding;
        }

        protected override void Subscriptions()
        {
        }

        protected override void RunOnEnter(ParameterStore parameterStore)
        {
            EndCadence();
            Brain.Controls.ResetDash();
            Brain.Entity.DesiredVelocity = Vector2.zero;
            Brain.Entity.ModelAnimation.CrossFade(dashLanding, .1f);
            Timer.Start();
        }

        protected override void RunOnExit()
        {
            Timer.Stop();
            Brain.InputResolver.Sentinel.Clear();
        }

        public override void RunOnUpdate()
        {
            if (Timer.IsNotRunning) return;
            Timer.Update(Timer.ScaledTime);
            if (Timer.Check(Brain.Config.DashLanding.Duration))
            {
                Timer.Stop();
                Brain.ChangeState(SentinelStateType.Idle);
            }
        }
    }
}