using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class MainMenu : MonoBehaviour
{
	public bool isTransmissionLost;
	public Font customFont;
	public Texture logoTexture;
	public Texture menuStartTexture;
	public Texture menuQuitTexture;
	public AudioClip mainMenuBlip;
	private bool canTakeAxisInput = true;
	public enum MainMenuSelection
	{
		Start = 0,
		Quit = 1
	}
	public MainMenuSelection currentMenuSelection = MainMenuSelection.Start;
	public struct ScrollingText
	{
		public float messageLength;
		public string messageString;
		public float messageHorizontalBuffer;
		public float messageVerticalBuffer;
		public float timeScale;
		public float lastLoopTime;
		public int fontSize;
		public Color customColor;
		public ScrollingText (float messageLength, string messageString,					// I need a wider monitor.
							  float messageHorizontalBuffer, float messageVerticalBuffer,
							  float timeScale, float lastLoopTime,
							  int fontSize, Color customColor) 
		{
			this.messageLength = messageLength;
			this.messageString = messageString;
			this.messageHorizontalBuffer = messageHorizontalBuffer;
			this.messageVerticalBuffer = messageVerticalBuffer;
			this.timeScale = timeScale;
			this.lastLoopTime = lastLoopTime;
			this.fontSize = fontSize;
			this.customColor = customColor;
		}
	}
	public List<ScrollingText> scrollingTextList = new List<ScrollingText>();
	void Start ()
	{
		isTransmissionLost = false;															// No, seriously, a big fucking monitor.
		scrollingTextList.Add(new ScrollingText(999.0f, "PENAL COLONY ND-143X OUT OF CONFEDERATION CONTROL",  32.0f,  -64.0f, 200.0f, Time.timeSinceLevelLoad, 32, Color.red));
		scrollingTextList.Add(new ScrollingText(999.0f, "QUARANTINE LOCKDOWN ENGAGED. DISTRESS SIGNAL SENT", 128.0f,  112.0f, 150.0f, Time.timeSinceLevelLoad, 32, new Color(1.0f, 0.5f, 0.0f, 1.0f)));
		scrollingTextList.Add(new ScrollingText(999.0f, "INMATES HAVE ACCESS TO GUARD ARMAMENTS AND ARMORS", 192.0f,  176.0f, 180.0f, Time.timeSinceLevelLoad, 32, new Color(1.0f, 1.0f, 0.0f, 1.0f)));
		scrollingTextList.Add(new ScrollingText(999.0f, "BREAKOUT PROTOCOL TO ARREST AND SENTENCE ON SIGHT", 256.0f,  240.0f, 125.0f, Time.timeSinceLevelLoad, 32, new Color(0.85f, 0.0f, 0.85f, 1.0f)));
		scrollingTextList.Add(new ScrollingText(999.0f, "WARNING: COLONY DRIFTING TOWARD AN ASTEROID CLOUD", 256.0f, -128.0f, 175.0f, Time.timeSinceLevelLoad, 32, new Color(0.0f, 0.85f, 1.0f, 1.0f)));
		scrollingTextList.Add(new ScrollingText(999.0f, "PERP AGGRESSION WARRANTS EXPEDITED DEATH PENALTY",  308.0f, -192.0f, 125.0f, Time.timeSinceLevelLoad, 32, new Color(1.0f, 0.75f, 0.0f, 1.0f)));
		scrollingTextList.Add(new ScrollingText(999.0f, "CONFISCATE AND REQUISITION ALL AMMUNITION FOUND",   512.0f, -384.0f, 95.0f, Time.timeSinceLevelLoad, 32, new Color(0.5f, 0.5f, 0.25f, 1.0f)));
	}
	void Update ()
	{
		// Reset axis inputs.
		if (Input.GetAxis("ForwardBackwardAxis") == 0.0f)
		{
			canTakeAxisInput = true;
		}
		// Player inputs.
		if ((Input.GetButtonDown("Forward")		||
			 Input.GetButtonDown("Backward")		||
			 Input.GetAxis("ForwardBackwardAxis") != 0.0f)	&&
			canTakeAxisInput)
		{
			canTakeAxisInput = false;
			switch (currentMenuSelection)	// May need more menu options in the future, but this is basically a toggle atm.
			{
				case MainMenuSelection.Quit:
					setMainMenuSelection(MainMenuSelection.Start);
					break;
				case MainMenuSelection.Start:
					setMainMenuSelection(MainMenuSelection.Quit);
					break;
			}
		}
		if (Input.GetButtonDown("Fire")			||
			Input.GetKeyDown(KeyCode.Return)	||
			Input.GetKeyDown(KeyCode.Space))
		{
			switch (currentMenuSelection)
			{
				case MainMenuSelection.Start:
					Application.LoadLevel("main");
					break;
				case MainMenuSelection.Quit:
					Application.Quit();
					break;
			}
		}
	}
	public void OnGUI ()
	{
		GUI.matrix = Matrix4x4.TRS(new Vector3(0.0f, 0.0f, 0.0f), Quaternion.identity, new Vector3(1.0f, 1.0f, 1.0f));
		GUIStyle customGuiStyle = new GUIStyle();
		customGuiStyle.font = customFont;
		// Death counter.
		int deaths = PlayerPrefs.GetInt("deaths", 0);
		customGuiStyle.fontSize = 16;
		customGuiStyle.normal.textColor = Color.red;
		string postDeathsString = " Unlawful Deaths";
		if (deaths == 1)
		{
			postDeathsString = " Unlawful Death";
		}
		GUI.Label(new Rect(Screen.width / 2.0f - 80.0f, Screen.height - 64.0f, 64.0f, 96.0f), deaths + postDeathsString, customGuiStyle);
		// Victory counter
		int victories = PlayerPrefs.GetInt("victories", 0);
		customGuiStyle.fontSize = 16;
		customGuiStyle.normal.textColor = Color.green;
		string postVictoriesString = " Lawful Victories";
		if (victories == 1)
		{
			postVictoriesString = " Lawful Victory";
		}
		GUI.Label(new Rect(Screen.width / 2.0f - 80.0f, Screen.height - 92.0f, 64.0f, 96.0f), victories + postVictoriesString, customGuiStyle);
		// Looping text.
		bool isLoopTriggered;	// Amazing variable name; +1, would name again.
		int listLimit = scrollingTextList.Count;
		for (int i = 0; i < listLimit; i++)
		{
			isLoopTriggered = false;
			customGuiStyle.fontSize = scrollingTextList[i].fontSize;
			customGuiStyle.normal.textColor = scrollingTextList[i].customColor;
			float timeSinceLevelLoad = Time.timeSinceLevelLoad;
			float xPosition = Screen.width + scrollingTextList[i].messageHorizontalBuffer - (timeSinceLevelLoad - scrollingTextList[i].lastLoopTime) * scrollingTextList[i].timeScale;
			if (xPosition < -1.0f * scrollingTextList[i].messageLength)
			{
				ScrollingText st = scrollingTextList[i];	// Structs passed by value
				st.lastLoopTime =timeSinceLevelLoad;		// within the List so create
				scrollingTextList[i] = st;					// and re-assign to mutate.
				xPosition += Screen.width + scrollingTextList[i].messageLength;
				isLoopTriggered = true;
			}
			GUI.Label(new Rect(xPosition, Screen.height / 2.0f + scrollingTextList[i].messageVerticalBuffer, 128.0f, 64.0f), scrollingTextList[i].messageString, customGuiStyle);
			if (isLoopTriggered	&&
				Random.Range(0, 101) <= 50)
			{//Debug.Log("<color=cyan>Removing message #" + i + "</color>");
				scrollingTextList.RemoveAt(i);
				if (i > 0)
				{
					i--;
				}
				if (listLimit > 0)
				{
					listLimit--;
				}
				if (scrollingTextList.Count <= 0)
				{
					scrollingTextList.Clear();
					if (!isTransmissionLost)
					{//Debug.Log("<color=yellow>TRANSMISSION LOST</color>");
						scrollingTextList.Add(new ScrollingText(480.0f, "Transmissions lost...", 240.0f, -192.0f, 75.0f, Time.timeSinceLevelLoad, 16, new Color(1.0f, 1.0f, 1.0f, 1.0f)));
						isTransmissionLost = true;
					}
				}
			}
		}
		// Menu selection icons.
		switch (currentMenuSelection)
		{
			case MainMenuSelection.Start:
				GUI.DrawTexture(new Rect(Screen.width / 2.0f - 128.0f, Screen.height / 2.0f - 26.0f, 64.0f, 64.0f), menuStartTexture, ScaleMode.StretchToFill, true, 0.0f);
				break;
			case MainMenuSelection.Quit:
				GUI.DrawTexture(new Rect(Screen.width / 2.0f - 128.0f, Screen.height / 2.0f + 25.0f, 64.0f, 64.0f), menuQuitTexture, ScaleMode.StretchToFill, true, 0.0f);
				break;
		}
		// Logo - down here so it shows on top of scrolling text.
		GUI.DrawTexture(new Rect(Screen.width / 2.0f - 208.0f, Screen.height / 2.0f - 208.0f, 512.0f, 512.0f), logoTexture, ScaleMode.StretchToFill, true, 0.0f);
	}
	public void setMainMenuSelection (MainMenuSelection mms)
	{
		currentMenuSelection = mms;
		this.gameObject.audio.PlayOneShot(mainMenuBlip);
	}
}