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

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

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

        protected override void Subscriptions()
        {
        }

        protected override void RunOnEnter(ParameterStore ps)
        {
            Brain.Entity.ModelAnimation.CrossFade(fallLanding, .1f);
            Brain.Entity.DesiredVelocity = Vector2.zero;
            Timer.Start();
        }

        protected override void RunOnExit()
        {
            Timer.Stop();
        }

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