using UnityEngine;
using System.Collections;
[System.Serializable]
public class MainMenu : MonoBehaviour {
	public GUISkin skin;
	public Texture2D logo;
	public Texture2D background;
	public Texture2D credits;
	
	public int level;
	
	public Map[] levels;
	private int selectedLevel;
	private int players;
	private int roundTime;
	public string[,] text = new string[10,10];
	// Use this for initialization
	void Start () {
		text[0,1] = "Play";
		text[0,2] = "Options";
		text[0,3] = "Credits";
		text[0,4] = "Exit";
	}
	
	// Update is called once per frame
	void Update () {
	
	}
	
	
	void OnGUI()
	{
		GUI.skin = skin;
		
		float xSizePercent = Screen.width / 1680.0f;
		float ySizePercent = Screen.height / 1050.0f;	
		
		GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height),background);//background
		GUI.DrawTexture(new Rect(Screen.width/2 - 300*xSizePercent,20*ySizePercent,600*xSizePercent,250*ySizePercent),logo);//background
		
		switch(level)
		{
		case 0:
			for(int i=1;i<5;i++)
			{
				if(GUI.Button(new Rect(Screen.width/2 - 150*xSizePercent,Screen.height/3 + i*70*ySizePercent,300*xSizePercent,60*ySizePercent),text[0,i]))
				{
					level=i;
				}
			}
			break;
		case 1:
			if(GUI.Button(new Rect(Screen.width/5,Screen.height*0.9f,200*xSizePercent,60*ySizePercent),"Back"))
			{
				level=0;
			}
			Rect rect0 = new Rect(Screen.width/5,Screen.height/5,300*xSizePercent,50*ySizePercent);
			if(GUI.Button(new Rect(rect0.x,rect0.y,50*xSizePercent,50*ySizePercent),"<"))
				selectedLevel--;
			if(selectedLevel<0)
				selectedLevel=levels.Length-1;
			
			rect0.x+=400*xSizePercent;
			if(GUI.Button(new Rect(rect0.x,rect0.y,50*xSizePercent,50*ySizePercent),">"))
				selectedLevel++;
			if(selectedLevel>levels.Length-1)
				selectedLevel=0;
			rect0.x-=400*xSizePercent;
			
			rect0.x+=200*xSizePercent - GUI.skin.label.CalcSize(new GUIContent(levels[selectedLevel].name)).x/2;
			GUI.Label(rect0,levels[selectedLevel].name); rect0.y+=50*ySizePercent; rect0.x-=200*xSizePercent - GUI.skin.label.CalcSize(new GUIContent(levels[selectedLevel].name)).x/2;
			
			if(levels[selectedLevel].preview)
			{
				rect0.width = 320*xSizePercent; rect0.height = 182*ySizePercent;
				GUI.DrawTexture(rect0,levels[selectedLevel].preview); rect0.x+=rect0.width*1.1f;
				
				rect0.width = 300*xSizePercent; rect0.height = 50*ySizePercent;
			}
			
			rect0.y+=70*ySizePercent;
			GUI.Label(rect0,"Players: "+players+"/"+levels[selectedLevel].maxPlayers);rect0.x+=GUI.skin.label.CalcSize(new GUIContent("Players: "+players+"/"+levels[selectedLevel].maxPlayers)).x*1.1f;
			if(GUI.Button(new Rect(rect0.x,rect0.y,50*xSizePercent,25*ySizePercent),"<"))
				players--;
			rect0.x+=50*xSizePercent;
			if(GUI.Button(new Rect(rect0.x,rect0.y,50*xSizePercent,25*ySizePercent),">"))	
				players++;
			
			if(players<1)
				players=1;
			if(players>levels[selectedLevel].maxPlayers)
				players=levels[selectedLevel].maxPlayers;
			
			
			
			rect0.y+=70*ySizePercent; rect0.x-=50*xSizePercent+GUI.skin.label.CalcSize(new GUIContent("Players: "+players+"/"+levels[selectedLevel].maxPlayers)).x*1.1f; 
			GUI.Label(rect0,"Round time: "+roundTime.ToString()+":00");rect0.x+=GUI.skin.label.CalcSize(new GUIContent("Round time: "+roundTime.ToString()+":00")).x*1.1f;
			if(GUI.Button(new Rect(rect0.x,rect0.y,50*xSizePercent,25*ySizePercent),"<"))
				roundTime-=5;
			rect0.x+=50*xSizePercent;
			if(GUI.Button(new Rect(rect0.x,rect0.y,50*xSizePercent,25*ySizePercent),">"))	
				roundTime+=5;
			rect0.x+=50*xSizePercent;
			
			if(roundTime<5)
				roundTime=5;
			if(roundTime>20)
				roundTime=20;
			
			
			if(GUI.Button(new Rect(Screen.width*0.8f,Screen.height*0.9f,200*xSizePercent,60*ySizePercent),"Play"))
			{
				GameOptions.roundTime = roundTime*60;
				GameOptions.players = players;
				Time.timeScale=1f;
				Application.LoadLevel(levels[selectedLevel].name);
			}
			
			break;
		case 2:
			if(GUI.Button(new Rect(Screen.width/5,Screen.height*0.9f,200*xSizePercent,60*ySizePercent),"Back"))
			{
				level=0;
			}
			Rect rect = new Rect(Screen.width/3,Screen.height/3,500*xSizePercent,50*ySizePercent);
			
			GUI.Label(rect,"Mouse sensivity: "); rect.x+=GUI.skin.label.CalcSize(new GUIContent("Mouse sensivity: ")).x;
			GameOptions.mouseSensivity = GUI.HorizontalSlider(rect,GameOptions.mouseSensivity,0.1f,10); rect.x+=rect.width*1.1f;
			GUI.Label(rect,GameOptions.mouseSensivity.ToString()); rect.x-=rect.width*1.1f; rect.y+= 70*ySizePercent; rect.x-=GUI.skin.label.CalcSize(new GUIContent("Mouse sensivity: ")).x;
			
			GUI.Label(rect,"Sounds volume: "); rect.x+=GUI.skin.label.CalcSize(new GUIContent("Sounds volume: ")).x;
			GameOptions.soundVolume = GUI.HorizontalSlider(rect,GameOptions.soundVolume,0,1); rect.x+=rect.width*1.1f;
			GUI.Label(rect,GameOptions.soundVolume.ToString()); rect.x-=rect.width*1.1f; rect.y+= 70*ySizePercent; rect.x-=GUI.skin.label.CalcSize(new GUIContent("Sounds volume: ")).x;
			
			GUI.Label(rect,"Music volume: "); rect.x+=GUI.skin.label.CalcSize(new GUIContent("Music volume: ")).x;
			GameOptions.musicVolume = GUI.HorizontalSlider(rect,GameOptions.musicVolume,0,1); rect.x+=rect.width*1.1f;
			GUI.Label(rect,GameOptions.musicVolume.ToString()); rect.x-=rect.width*1.1f; rect.y+= 70*ySizePercent; rect.x-=GUI.skin.label.CalcSize(new GUIContent("Music volume: ")).x;
			break;
		case 3:
			GUI.DrawTexture(new Rect(Screen.width/2 - 200*xSizePercent, 200*ySizePercent,400*xSizePercent,800*ySizePercent),credits);
			
			if(GUI.Button(new Rect(Screen.width/5,Screen.height*0.9f,200*xSizePercent,60*ySizePercent),"Back"))
			{
				level=0;
			}
			Rect rect2 = new Rect(Screen.width/3,Screen.height/3,500*xSizePercent,50*ySizePercent);

			
			break;
		case 4:
			Application.Quit();
			break;
		}

	}
}
