using System;
using UnityEngine;

namespace State.PlayerStateMachine
{
    public class PlayerStateMachine : StateMachine
    {
        public Pawn controlledPawn;
        private bool _forwardInput;

        public void Issue(GameState.Command command)
        {
            if(_forwardInput) CurrentState?.Handle(command);
        }

        public void SetMove(Vector2 input)
        {
            if(_forwardInput) CurrentState?.Handle(input);
        }

        private void Awake()
        {
            GameStateMachine.GameStateMachine.Instance.OnStateChange += HandleStateChange;
            HandleStateChange(GameStateMachine.GameStateMachine.Instance.CurrentState);
            ChangeState(new Idle());
        }

        private void HandleStateChange(GameState newState)
        {
            _forwardInput = newState switch
            {
                GameStateMachine.PlayLevel => true,
                _ => false
            };
        }
    }
}