using UnityEngine;
using System.Collections;

public class Interface : MonoBehaviour {
	
	public Texture m_Logo;
	public GUIStyle m_Style;
	public GUIStyle m_CenteredStyle;
	public GUIStyle m_BriefingStyle;
	
	public int m_MenuState = (int)States.BEGIN;
	public string m_MissionBriefing;
	private int m_MaxAssignments;
	
	private int m_AssignmentsTaken = 0;
	
	private bool m_ShowInterface = true;
	
	
	public void Show() { m_ShowInterface = true; }
	public void Hide() { m_ShowInterface = false; }
	
	enum States {BEGIN = 0, INGAME, DEAD, CREDITS};
	
	public void SetMenuState(int state) { m_MenuState = state; }
	
	//To show how long you are alive
	private float m_Timer;
	private float m_FlickerTimer;
	private float m_ScrollingTimer;
	
	private float m_IntroTimer;
	
	// Use this for initialization
	void Start ()
	{
		//Application.LoadLevel(0); 
		m_Timer = 0.0f;
		m_IntroTimer = 0;
		m_FlickerTimer = 0.0f;
		m_ScrollingTimer = 0.0f;
		m_MaxAssignments = GameObject.FindGameObjectsWithTag("Assignment").Length;
	}
	
	// Update is called once per frame
	void Update ()
	{
		if (Input.GetButtonUp("Enter"))
		{
			switch(m_MenuState)
			{
				case (int)States.BEGIN:
				 	HandleMenuBegin();
					break;
					
				case (int)States.INGAME:
					HandleMenuInGame();
					break;
				
				case (int)States.DEAD:
					HandleMenuDead();
					break;
					
				case (int)States.CREDITS:
					HandleMenuCredits();
					break;
					
				default:
					break;
			}
		}
		
		if (m_MenuState != (int)States.DEAD) m_Timer += Time.deltaTime;
		
		float y = Screen.height - (m_ScrollingTimer * 100) + 180;
		
		if (m_MenuState == (int)States.CREDITS && y >= Screen.height/2) m_ScrollingTimer += Time.deltaTime;
		
		//Insert coin flickering
		m_FlickerTimer += Time.deltaTime;
		if (m_FlickerTimer > 1.0f) m_FlickerTimer = 0.0f;
		
		
		//Intro animation
		if(m_IntroTimer>0)
			m_IntroTimer+=Time.deltaTime;
		
		if(m_IntroTimer>=2)
		{		
			//Change state of the menu
			m_MenuState = (int)States.INGAME;
			
			//Reset the timer
			m_Timer = 0.0f;
			
			Application.LoadLevel(1);
		}
	}
	
	void OnGUI()
	{
		if (m_ShowInterface)
		{
			switch(m_MenuState)
			{
				case (int)States.BEGIN:
				 	DrawMenuBegin();
					break;
					
				case (int)States.INGAME:
					DrawMenuInGame();
					break;
				
				case (int)States.DEAD:
					DrawMenuDead();
					break;
					
				case (int)States.CREDITS:
					DrawMenuCredits();
					break;
					
				default:
					break;
			}
		}
	}
		
	//Drawing the menu's
	private void DrawMenuBegin()
	{
		int x = (Screen.width/2) - (548/2);
		int y = (Screen.height/2) - 248/2;
		
		//Show the logo
		GUI.DrawTexture(new Rect(x, y, 548, 248), m_Logo);	
		
		//Main menu message
		if (m_FlickerTimer < 0.5f) GUI.Label(new Rect(Screen.width/2, Screen.height - (Screen.height/8), 100, 50), "INSERT COIN", m_CenteredStyle);		
	}
	
	private void DrawMenuInGame()
	{
		GUI.Label(new Rect(10, 20, 200, 20), m_Timer.ToString("#0.0"), m_Style);	
		
		int toTake = m_MaxAssignments - m_AssignmentsTaken;
		
		string message = "Deliver " + toTake + " more assignment";
		
		if (toTake > 1) message += "s!";
		else 			message += "!";
		
		if (m_AssignmentsTaken == 0) GUI.Label(new Rect(Screen.width/2, 0, 100, 40), m_MissionBriefing, m_BriefingStyle);
		else if (toTake > 0) 	 	 GUI.Label(new Rect(Screen.width/2, 0, 100, 40), message, m_BriefingStyle);
		else 						 GUI.Label(new Rect(Screen.width/2, 0, 100, 40), "Alright, end of the warmup. Make it to the exit!", m_BriefingStyle);
	}
	
	private void DrawMenuDead()
	{
		string message = "You survived DAE for " + m_Timer.ToString("#0.0") + " seconds!";
		GUI.Label(new Rect(Screen.width/2, Screen.height - (Screen.height/7), 100, 50), "Congratiulations!", m_CenteredStyle);	
		GUI.Label(new Rect(Screen.width/2, Screen.height - (Screen.height/7) + 40, 100, 50), message, m_CenteredStyle);	
	}
	
	private void DrawMenuCredits()
	{
		float y = Screen.height - (m_ScrollingTimer * 100);
		
		GUI.Label(new Rect(Screen.width/2, y, 100, 50), "Thanks for playing!", m_CenteredStyle);	
		GUI.Label(new Rect(Screen.width/2, y + 100, 100, 50), "Bruno Lebrun", m_CenteredStyle);
		GUI.Label(new Rect(Screen.width/2, y + 140, 100, 50), "Karim Houari", m_CenteredStyle);
		GUI.Label(new Rect(Screen.width/2, y + 180, 100, 50), "Stijn Grooten", m_CenteredStyle);
		GUI.Label(new Rect(Screen.width/2, y + 220, 100, 50), "Stijn Doyen", m_CenteredStyle);
		GUI.Label(new Rect(Screen.width/2, y + 260, 100, 50), "Stijn Delaruelle", m_CenteredStyle);
		
		GUI.Label(new Rect(Screen.width/2, y + 360, 100, 50), "Still crazy enough to study DAE?", m_CenteredStyle);
	}
	
	//When enter is pressed in these menu's
	private void HandleMenuBegin()
	{
		//play sound
		GameObject.FindGameObjectWithTag("Unit").GetComponent<Unit>().m_StressMeter = 100;
		m_IntroTimer=0.01f;
	}
	
	private void HandleMenuInGame()
	{
		//End the game
		
		//Reset the menu state
		Application.LoadLevel(0); 
	}
	
	private void HandleMenuDead()
	{
		//End the game
		Application.LoadLevel(0); 
	}
	
	private void HandleMenuCredits()
	{
		//End the game
		Application.LoadLevel(0);
	}
	
	public void DeliverAssignment()
	{
		m_AssignmentsTaken += 1;
	}
}