using UnityEngine;
using System.Collections;
using System;
using UnityEngine.UI;

public class StateMachine : MonoBehaviour {

    public Text m_text;


    /// <summary>
    ///     Different states of the machine. Corresponds to the different screens or behaviours of the game
    /// </summary>
    public enum STATE
    {
        MENU,
        LISTOFLEVELS,
        LOADING,
        PLAY,
        QUIT,
        WIN,
        CONTROLS,
        RESET,

        COUNT
    }

    /// <summary>
    ///     This class is a singleton
    /// </summary>
    /// 
    public static StateMachine instance;

    /// <summary>
    /// List of process every state is assigned
    /// <para>Need to put processes in the same order than STATE enum</para>
    /// </summary>
    [SerializeField] private Process[] processList;

    /// <summary>
    /// Current state of the machine
    /// </summary>
    /// 
    private STATE currentState;
    public STATE CurrentState{
        get{
            return this.currentState;
        }
        set{
            UpdateState(value);
        }
    }
    
    void Awake()
    {
        if (instance == null)
            instance = this;
    }

    /// <summary>
    /// Use this for initialization
    /// </summary>
	void Start () {
        currentState = 0;

        initStates();

        UpdateState(CurrentState);
    }

    /// <summary>
    /// Initialize every state/process
    /// <para>Ask to every process this class own to initialize. Set every process this class own to inactive.</para>
    /// </summary>
    private void initStates()
    {
        foreach (Process p in processList)
        {
            p.SetActive(false);
            p.Init();
        }
    }

    /// <summary>
    /// Change the current state to newState
    /// </summary>
    /// <param name="newState">The state to turn on</param>
    
    public void UpdateState(STATE newState)
    {

        //Debug.Log(processList);
        //Debug.Log(CurrentState);
        processList[(int)CurrentState].SetActive(false);
        
        processList[(int)newState].SetActive(true);


        currentState = newState;
    }
}
