using UnityEngine;
using System.Collections;

public class GizmoScript : MonoBehaviour {

	public GameObject P1cont;
	public GameObject P2cont;

	public GameObject MusicSpeaker;

	public AudioClip[] MusicQueue;
	public AudioClip[] Praises;
	public AudioClip[] Warnings;
	public AudioClip[] TutorialMsgs;
	public AudioClip[] Gameovers;

	public Texture2D bothud;
	public Texture2D fill;
	public Texture2D fill2;

	public Texture2D TexWarmup;
	public Texture2D TexStart;
	public Texture2D TexLogo;

	public bool gameover;

	AudioSource MAS_ref;
	AudioSource AAS_ref;

	bool playMusic;
	int musicState;
	public float gameTime;

	float checkintimer;
	public float beat_timer;
	float volume_health;
	float volume_display_health;
	float volume_flickertimer;

	float difficultybumptimer;
	public int difficulty;

	int score;
	int display_score;
	int combocounter;

	public GUIStyle scoreStyle;

	bool gameoverplayed = false;
	public float gameovertimer;

	public bool menu;
	public bool tutorial;

	int tutorialstate;

	public GameObject[] VFXObjs;
	public int NUM_VFX_OBJS;


	// Use this for initialization
	void Start () {

		MAS_ref = (AudioSource)MusicSpeaker.GetComponent ("AudioSource");
		AAS_ref = (AudioSource)this.GetComponent ("AudioSource");
		menu = true;
		tutorial = false;
		InitializeAllTheThings ();
		//StartGame ();
	}
	
	// Update is called once per frame
	void Update () 
	{
		gameTime += Time.deltaTime;
		checkintimer += Time.deltaTime;
		volume_flickertimer -= Time.deltaTime;
		beat_timer += Time.deltaTime;
		gameovertimer += Time.deltaTime;
		difficultybumptimer += Time.deltaTime;



		if(tutorial)
		{
			combocounter = 0;
			if(beat_timer >= 0.5f)
			{
				beat_timer -= 0.5f;
				volume_health -= 3.125f;
			}
			volume_display_health = volume_health;

			switch(tutorialstate)
			{
			case 0:
				// this is you

				if(gameTime > 4.0f)
				{
					++tutorialstate;
				}

				break;
			case 1:
				// how to color
				if(gameTime > 8.0f)
				{
					++tutorialstate;
				}
				break;

			case 2:
				// how to launch
				if(gameTime > 16.0f)
				{
					++tutorialstate;
					AAS_ref.clip = TutorialMsgs[1];
					AAS_ref.Play ();
					volume_health = 80.0f;
					volume_display_health = 80.0f;
				}

				break;
			case 3:
				// catch!
				if(score > 500)
				{
					++tutorialstate;
					AAS_ref.clip = TutorialMsgs[2];
					AAS_ref.Play ();
				}
				break;
			case 4:
				// volume is dying
				if(score > 1000)
				{
					++tutorialstate;
					AAS_ref.clip = TutorialMsgs[3];
					AAS_ref.Play ();
				}
				break;
			case 5:
				// time with beat!
				if(score > 2000)
				{
					++tutorialstate;
					AAS_ref.clip = TutorialMsgs[4];
					AAS_ref.Play ();
				}
				break;
			case 6:
				// never let the music die!
				if(score > 3000)
				{
					++tutorialstate;
					AAS_ref.clip = TutorialMsgs[5];
					AAS_ref.Play ();
				}
				break;

			}


		}
		else if(!menu)
		{
			if(playMusic)
			{
				if(gameTime >= 0.5f && musicState == 0)
				{
					MAS_ref.Stop();
					MAS_ref.clip = MusicQueue[1];
					MAS_ref.Play();
					musicState = 1;
				}

				//fill volume up
				if(gameTime >=8.5f && musicState == 1)
				{
					volume_display_health += (12.5f * Time.deltaTime);
					if(volume_display_health > 100.0f)
						volume_display_health = 100.0f;
				}
				else if(gameTime > 16.6f)
				{
					volume_display_health = volume_health;
				}
			
				if(gameTime > 16.5f && musicState == 1)
				{
					MAS_ref.Stop();
					MAS_ref.clip = MusicQueue[2];
					MAS_ref.loop = true;
					MAS_ref.Play();
					musicState = 2;
					checkintimer = -1.0f;
					beat_timer = 0.0f;
					volume_health = 100.0f;
					difficultybumptimer = 0.0f;
				}

				if(beat_timer >= 0.5f && playMusic)
				{
					beat_timer -= 0.5f;
					if(musicState == 2)
						volume_health -= 3.125f;
					//volume_flickertimer = 0.125f;
				}

				if(display_score != score)
				{
					int delta = score - display_score;
					int dir = 1;
					if(delta < 0)
						dir = -1;

					if(Mathf.Abs(delta) <100)
						display_score += 1 * dir;
					else if(Mathf.Abs(delta) <1000)
						display_score += 10 * dir;
					else if(Mathf.Abs(delta) <10000)
						display_score += 100 * dir;
				}
				if(difficultybumptimer >= 48.0f)
				{
					difficultybumptimer -= 48.0f;
					difficulty++;
				}

			}

			if (checkintimer >= 7.0f && musicState == 2 && !gameover)
			{
				AAS_ref.Stop ();

				if(volume_health >= 50.0f)
				{
					AAS_ref.clip = Praises [Random.Range (0,4)];
				}
				else
				{
					AAS_ref.clip = Warnings [Random.Range (0,4)];
				}

				/*
				if(volume_health >= 87.5f)
					AAS_ref.clip = Praises [3];
				else if(volume_health >= 75.0f)
					AAS_ref.clip = Praises [2];
				else if(volume_health >= 62.5f)
					AAS_ref.clip = Praises [1];
				else if(volume_health >= 50.0f)
					AAS_ref.clip = Praises [0];
				else if(volume_health >= 37.5f)
					AAS_ref.clip = Warnings [0];
				else if(volume_health >= 25.0f)
					AAS_ref.clip = Warnings [1];
				else if(volume_health >= 12.5f)
					AAS_ref.clip = Warnings [2];
				else // (volume_health <= 12.5f)
					AAS_ref.clip = Warnings [3];
					*/

				AAS_ref.Play ();
				checkintimer = -1.0f;
			}
			if(gameover)
			{
				if(gameovertimer > 4.0f && gameoverplayed == false)
				{
					AAS_ref.Stop ();
					if(score < 10000)
						AAS_ref.clip = Gameovers[0];
					else if(score < 100000)
						AAS_ref.clip = Gameovers[1];
					else if(score < 500000)
						AAS_ref.clip = Gameovers[2];
					else
						AAS_ref.clip = Gameovers[3];

					AAS_ref.Play ();
					gameoverplayed = true;
				}
			}

			if(volume_health <= 0.0f)
			{
				//game over
				if(gameover ==false)
					gameovertimer =0.0f;
				gameover = true;
				if(MAS_ref.pitch > 0.0f)
					MAS_ref.pitch -= 0.003125f;

			}
		}
	}

	public void Heal(bool shining)
	{
		if(shining)
			volume_health += 10.0f;
		else
			volume_health += 5.0f;
		if (volume_health > 100.0f)
			volume_health = 100.0f;
	}

	public void Damage()
	{
		volume_health -= 5.0f;
		if (volume_health < 0.0f)
			volume_health = 0.0f;
		volume_flickertimer = 0.25f;
	}

	public void Score(int points, bool combo)
	{
		if(combo)
			++combocounter;
		else
			combocounter = 1;

		score += (points * combocounter);
	}

	void InitializeAllTheThings()
	{
		score = 0;
		display_score = 0;
		combocounter = 0;
		difficultybumptimer = 0.0f;
		volume_display_health = 0.0f;
		volume_flickertimer = 0.0f;
		beat_timer = 0.0f;
		checkintimer = 0.0f;

		volume_health = 100.0f;
		gameTime = 0.0f;
		musicState = 0;
		gameover = false;
		gameoverplayed = false;
		MAS_ref.pitch = 1.0f;

		playMusic = true;

		for(int i=0; i<NUM_VFX_OBJS; ++i)
		{
			ParticleSystem temppsref = (ParticleSystem)VFXObjs[i].gameObject.GetComponent("ParticleSystem");
			temppsref.Stop();
			temppsref.Play ();
		}

	}
	void StartGame()
	{
		InitializeAllTheThings ();

		AAS_ref.Stop ();
		MAS_ref.Stop ();
		AAS_ref.clip = MusicQueue [0];
		AAS_ref.Play ();
		MAS_ref.loop = false;

		menu = false;
		tutorial = false;
	}

	void StartTutorial()
	{
		InitializeAllTheThings ();
		
		AAS_ref.Stop ();
		MAS_ref.Stop ();
		MAS_ref.clip = MusicQueue [3];
		MAS_ref.Play ();
		MAS_ref.loop = true;

		AAS_ref.clip = TutorialMsgs [0];
		AAS_ref.Play ();

		menu = false;
		tutorial = true;
		tutorialstate = 0;
	}

	void OnGUI()
	{
		float edge_left  	= (Screen.width / 2.0f) - 256;
		float edge_right 	= (Screen.width / 2.0f) + 256;
		float edge_top 		= (Screen.height) - 128 - 16;
		float edge_bot		= (Screen.height) - 16;

		if(!menu && !tutorial)
		{
			// volume (health) display
			if(gameTime > 8.5f && volume_display_health > 0.0f)
			{
				if(volume_flickertimer > 0.0f)
					GUI.color = new Color(Random.Range (0.0f, 1.0f), Random.Range (0.0f, 1.0f), Random.Range (0.0f, 1.0f));
				else
					GUI.color = new Color(1.0f - (volume_display_health/100.0f), (volume_display_health/100.0f), 0.2f);
				GUI.DrawTexture(new Rect(edge_left + 8,edge_top + 58, 4.96f * volume_display_health, 40), fill);
				GUI.color = Color.white;
			}

			if(gameTime > 6.5f) // volume bar overlay
			{
				if(gameTime < 7.5f)
				{
					GUI.DrawTexture(new Rect(edge_left,edge_top + (300 * (gameTime - 7.5f) * (gameTime - 7.5f)), 512, 128), bothud);
				}
				else
				{
					GUI.DrawTexture(new Rect(edge_left,edge_top, 512, 128), bothud);
				}
			}

			if(gameover) // blackout
			{
				GUI.color = new Color(1.0f, 1.0f, 1.0f, gameovertimer / 4.0f);
				GUI.DrawTexture(new Rect(0,0, Screen.width, Screen.height), fill2);
				GUI.color = Color.white;
				if(gameovertimer > 4.0f)
				{
					if(GUI.Button(new Rect(Screen.width/2 -150, (Screen.height/2) -25, 300, 50), "Try Again?", scoreStyle))
					{
						StartGame();
					}
				}			
			}

			if(gameTime > 6.5f) // score
			{
				if(gameTime < 7.5f)
				{
					GUI.Label (new Rect (edge_left, edge_top + (300 * (gameTime - 7.5f) * (gameTime - 7.5f)), 512, 64), "Score: " + display_score.ToString(), scoreStyle);
					GUI.DrawTexture(new Rect(edge_left,edge_top + (300 * (gameTime - 7.5f) * (gameTime - 7.5f)), 512, 128), bothud);
				}
				else
				{
					if(gameTime < 8.5f || gameTime > 16.5)
						GUI.Label (new Rect (edge_left, edge_top, 512, 64), "Score: " + display_score.ToString(), scoreStyle);
					else
					{
						GUI.color = new Color(Random.Range (0.0f, 1.0f), Random.Range (0.0f, 1.0f), Random.Range (0.0f, 1.0f));	
						GUI.Label (new Rect (edge_left, edge_top, 512, 64), "TAP SPACE TO  THE BEAT TO BUILD COMBO!!!", scoreStyle);
						GUI.color = Color.white;
					}
				}
			}

			
			if(combocounter > 1) // combo counter render
			{
				GUI.color = new Color(Random.Range (0.0f, 1.0f), Random.Range (0.0f, 1.0f), Random.Range (0.0f, 1.0f));	
				GUI.Label (new Rect (edge_left, edge_top, 512, 24), "COMBO x" + combocounter.ToString(), scoreStyle);
				GUI.color = Color.white;
			}

			if (gameTime > 0.5f && gameTime < 3.5f)// warmup 
			{
				GUI.color = new Color(Random.Range (0.0f, 1.0f), Random.Range (0.0f, 1.0f), Random.Range (0.0f, 1.0f));	
				GUI.DrawTexture(new Rect(edge_left+128, (Screen.height / 2) - 32, 256, 64), TexWarmup);
				GUI.color = Color.white;
			}

			if (gameTime > 16.5f && gameTime < 17.5f) //start!
			{
				GUI.color = new Color(Random.Range (0.0f, 1.0f), Random.Range (0.0f, 1.0f), Random.Range (0.0f, 1.0f));	
				GUI.DrawTexture(new Rect(edge_left+128, (Screen.height / 2) - 32, 256, 64), TexStart);
				GUI.color = Color.white;
			}
		}

		if(menu)
		{

			GUI.DrawTexture(new Rect(Screen.width/2 -256, (Screen.height/2) -128, 512, 64), TexLogo);


			if(GUI.Button(new Rect(Screen.width/2 -150, (Screen.height/2) +25, 300, 50), "Start Game", scoreStyle))
			{
				StartGame();
			}

			if(GUI.Button(new Rect(Screen.width/2 -150, (Screen.height/2) +125, 300, 50), "How To Play", scoreStyle))
			{
				StartTutorial();
			}
		}

		if(tutorial)
		{
			switch(tutorialstate)
			{
			case 0:
				GUI.Label (new Rect(edge_left+128, (Screen.height / 2) - 32, 256, 64), "You're on the left!", scoreStyle);
				GUI.Label (new Rect(edge_left, (Screen.height / 2) - 128, 128, 64), "<-- YOU!", scoreStyle);
				break;
			case 1:
				GUI.Label (new Rect(edge_left+128, (Screen.height / 2) - 32, 256, 64), "Q, A, and Z:\nChange your color.", scoreStyle);
				break;
			case 2:
				GUI.Label (new Rect(edge_left+128, (Screen.height / 2) - 32, 256, 64), "W, S, and X:\nLaunch a Catcher of your color!", scoreStyle);
				break;
			case 3:
				break;
			case 4:
				break;
			case 5:
				break;
			case 6:
				break;
			case 7:
				if(GUI.Button (new Rect(edge_left, (Screen.height / 2) + 64, 512, 32), "Ready? Click Here to Begin!", scoreStyle))
				{
					StartGame ();
				}
				break;


			}
			if(tutorialstate >= 4)
			{
				// volume (health) display
				if(volume_display_health > 0.0f)
				{
					if(volume_flickertimer > 0.0f)
						GUI.color = new Color(Random.Range (0.0f, 1.0f), Random.Range (0.0f, 1.0f), Random.Range (0.0f, 1.0f));
					else
						GUI.color = new Color(1.0f - (volume_display_health/100.0f), (volume_display_health/100.0f), 0.2f);
					GUI.DrawTexture(new Rect(edge_left + 8,edge_top + 58, 4.96f * volume_display_health, 40), fill);
					GUI.color = Color.white;
				}

				GUI.DrawTexture(new Rect(edge_left,edge_top, 512, 128), bothud);

			}

		}


	}
}
