using UnityEngine;

public class StateMachine
{

	public StateMachine()
	{
		// TODO: pass a reference to the player and pool states?
	}

	public BaseState CurrentState { get; private set; }
	public BaseState PreviousState { get; private set; }

	public void Update()
	{
		BaseState newState = CurrentState.HandleInput();

		if (newState != null)
		{
			ChangeState(newState);
			return;
		}

		newState = CurrentState.Update();

		if (newState != null)
		{
			ChangeState(newState);
			return;
		}
	}

	public void ChangeState(BaseState newState)
	{
		if (CurrentState != null && newState.GetType() == CurrentState.GetType())
		{
			Debug.LogWarning("Attempting to change state to the same state you're in (" + newState + ")!");
			return;
		}
		
        //Debug.Log("-------------- State transition from " + CurrentState + " to " + newState);
        //Debug.Log(newState);

		if (CurrentState != null)
			CurrentState.OnExit();

		PreviousState = CurrentState;
		CurrentState = newState;
		CurrentState.OnEnter();
	}
}