using System.Collections;
using System.Collections.Generic;
using System;
using System.Text.RegularExpressions;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class Main : MonoBehaviour 
{
	public GameObject MainMenu;
	public GameObject PanelLogo;
	public GameObject PanelButtons;

	public GameObject PlayButton;
	public GameObject ConfigButton;
	public GameObject ExitButton;

	public GameObject ConfigPagePanel;
	public GameObject BackButton;

	public GameObject QuitPanel;
	public GameObject YesButton;
	public GameObject NoButton;

	private Button playButton;
	private Button configButton;
	private Button exitButton;
	private Button backButton;
	private Button yesButton;
	private Button noButton;

	private int iteratorAnimationLogo;
	private int iteratorAnimationPlay;
	private int iteratorAnimationConfig;
	private int iteratorAnimationExit;

	private int iteratorAnimationConfigPanel;

	private int iteratorAnimationQuitPanel;

	private int Flag = 0;
	private int moveButtonPanel;


	// Use this for initialization
	void Start () 
	{
        MainMenu.SetActive(true);
        PanelLogo.transform.Rotate(0, 0, 90);
        iteratorAnimationLogo = 0;
        iteratorAnimationPlay = 0;
        iteratorAnimationConfig = 0;
        iteratorAnimationExit = 0;
        iteratorAnimationConfigPanel = 0;
        iteratorAnimationQuitPanel = 0;
        moveButtonPanel = 0;

        playButton = PlayButton.GetComponent<Button>();
        playButton.onClick.AddListener(Play);
        configButton = ConfigButton.GetComponent<Button>();
        configButton.onClick.AddListener(ConfigPage);
        exitButton = ExitButton.GetComponent<Button>();
        exitButton.onClick.AddListener(ShowQuitMenu);

        backButton = BackButton.GetComponent<Button>();
        backButton.onClick.AddListener(CloseConfigPanel);

        yesButton = YesButton.GetComponent<Button>();
        yesButton.onClick.AddListener(Quit);
        noButton = NoButton.GetComponent<Button>();
        noButton.onClick.AddListener(HideQuitMenu);
    }

	// Update is called once per frame
	void Update () 
	{
		if (Flag == 0) 
		{
			if (iteratorAnimationLogo <= 14) 
			{
				PanelLogo.transform.Rotate (0, 0, -8);
				iteratorAnimationLogo++;
			}
			if (iteratorAnimationLogo > 14 && iteratorAnimationLogo <= 20) 
			{
				PanelLogo.transform.Rotate (0, 0, 5);
				iteratorAnimationLogo++;
			}

			if (iteratorAnimationLogo > 20) 
			{
				if (iteratorAnimationPlay >= 15)
					PlayButton.SetActive (true);
				else if (iteratorAnimationPlay < 15)
					iteratorAnimationPlay++;
			}

			if (iteratorAnimationPlay > 14) 
			{
				if (iteratorAnimationConfig >= 15)
					ConfigButton.SetActive (true);
				else if (iteratorAnimationConfig < 15)
					iteratorAnimationConfig++;
			}

			if (iteratorAnimationConfig > 14)
			{
				if (iteratorAnimationExit >= 15)
					ExitButton.SetActive (true);
				else if (iteratorAnimationExit < 15)
					iteratorAnimationExit++;
			}
		}

		if (Flag == 1) 
		{
			if(moveButtonPanel <= 5 )
			{
				PanelButtons.transform.Translate (-6, 0, 0);
				moveButtonPanel++;
			}

			if(moveButtonPanel > 5 && moveButtonPanel <= 13)
			{
				PanelButtons.transform.Translate (15, 0, 0);
				moveButtonPanel++;
			}	

			if (iteratorAnimationConfigPanel <= 10 && moveButtonPanel > 13) 
			{
				ConfigPagePanel.transform.Translate (-6,0,0);
				iteratorAnimationConfigPanel++;
			}
		}

		if (Flag == 2) 
		{
			
			if(moveButtonPanel > 6 && iteratorAnimationConfigPanel == 0)
			{
				PanelButtons.transform.Translate (-15, 0, 0);
				moveButtonPanel--;
			}	

			if(moveButtonPanel <= 6 && moveButtonPanel > 0 )
			{
				PanelButtons.transform.Translate (6, 0, 0);
				moveButtonPanel--;
			}

			if (iteratorAnimationConfigPanel > 0) 
			{
				ConfigPagePanel.transform.Translate (6,0,0);
				iteratorAnimationConfigPanel--;
			}
		}

		if (Flag == 3) 
		{
			if (iteratorAnimationLogo > 15) 
			{
				PanelLogo.transform.Rotate (0, 0, -5);
				iteratorAnimationLogo--;
			}
			if (iteratorAnimationLogo <= 15 && iteratorAnimationLogo > 0) 
			{
				PanelLogo.transform.Rotate (0, 0, 8);
				iteratorAnimationLogo--;
			}

			if(moveButtonPanel <= 5 )
			{
				PanelButtons.transform.Translate (-6, 0, 0);
				moveButtonPanel++;
			}

			if(moveButtonPanel > 5 && moveButtonPanel <= 10)
			{
				PanelButtons.transform.Translate (15, 0, 0);
				moveButtonPanel++;
			}	

			PlayButton.SetActive (false);
			ConfigButton.SetActive (false);
			ExitButton.SetActive (false);

			if (moveButtonPanel > 6 && iteratorAnimationConfigPanel == 0) 
			{
				PanelButtons.transform.Translate (-15, 0, 0);
				moveButtonPanel--;
			}

			if(moveButtonPanel <= 6 && moveButtonPanel > 0 )
			{
				PanelButtons.transform.Translate (6, 0, 0);
				moveButtonPanel--;
			}

			if (iteratorAnimationLogo == 0 && iteratorAnimationQuitPanel <= 10) 
			{
				QuitPanel.transform.Translate (0,8,0);
				iteratorAnimationQuitPanel++;
			}

		}

		if (Flag == 4)
		{
			if (iteratorAnimationQuitPanel > 0) 
			{
				QuitPanel.transform.Translate (0,-8,0);
				iteratorAnimationQuitPanel--;
			}
			if (iteratorAnimationQuitPanel == 0)
				Flag = 0;
		}
	}


	void ShowMainMenu()
	{
		Flag = 0;
	}
	void ConfigPage()
	{
		Flag = 1;
	}
	void CloseConfigPanel()
	{
		Flag = 2;	
	}

	void ShowQuitMenu()
	{
		Flag = 3;
	}
	void HideQuitMenu()
	{
		Flag = 4;
	}

	void Quit()
	{
		Quit();
	}

	void Play()
	{
        SceneManager.LoadScene("Game");
	}

}
