using UnityEngine;
using UnityEngine.SceneManagement;

public class MainMenu : MonoBehaviour
{
    internal enum State
    {
        Start = 0,
        Credits = 1,
    };

    [SerializeField]
    private GameObject[] m_positions;
    [SerializeField]
    private GameObject m_button;

    [Header("Menu")]
    [SerializeField]
    private GameObject m_startCanvas;
    [SerializeField]
    private GameObject m_creditsCanvas;

    private int m_currentPosition;
    private bool m_released;

    #region Unity Callback
    // Use this for initialization
    protected void Start () 
	{
	
	}
	
	// Update is called once per frame
	protected void Update ()
	{
	    if (Input.GetButtonDown("Submit") || Input.GetButtonDown("FastAttack"))
	    {
	        if (m_currentPosition == (int)State.Start)
	        {
	            SceneManager.LoadScene("game");
	        }
            else if (m_currentPosition == (int) State.Credits)
            {
                m_startCanvas.SetActive(false);
                m_creditsCanvas.SetActive(true);
            }
        }
        else if (Input.GetButtonDown("Cancel"))
        {
            Back();
        }

	    float move = Input.GetAxis("Vertical");
	    if (move > 0f)
	    {
            if (m_released == false)
            {
                return;
            }

            m_released = false;
            m_currentPosition = (m_currentPosition + 1)%m_positions.Length;

            UpdateButton();
        }
        else if (move < 0f)
        {
            if (m_released == false)
            {
                return;
            }

            m_released = false;
            m_currentPosition--;
            if (m_currentPosition < 0)
            {
                m_currentPosition = m_positions.Length - 1;
            }

            UpdateButton();
        }
        else
        {
            m_released = true;
        }
	}

    private void UpdateButton()
    {
        m_button.transform.position = m_positions[m_currentPosition].transform.position;
    }

    public void Back()
    {
        m_currentPosition = 0;
        m_startCanvas.SetActive(true);
        m_creditsCanvas.SetActive(false);

        UpdateButton();
    }

    #endregion
}
