﻿using UnityEngine;
using System.Collections;
 
	
public class Pulse : MonoBehaviour {
	
	static int STATE_START = 0;
	static int STATE_PREGAME = 1;
	static int STATE_COUNTDOWN = 2;
	static int STATE_GAME = 3;
	static int STATE_POSTGAME = 4;
	static int STATE_GAMEOVER = 99;
	int STATE = STATE_START;
	
	const float interval = 1;	// pulse interval in seconds
	float scale = 0;			// scale between -1 and 1 based on the interval
	float scaleNorm = 0;		// scale normalized between 0 and 1
	float overallTimer = 0;		// counter since the round began
	
	float lastKey = -1;			// position of scaleNorm of last keypress
	float lastKeyDiff = 0;		// difference between lastKey and the pulse max
	float lastKeyTime = 0;		// how long ago the lastKey was pressed
	float aggregateDiff = 0;	// total diff of all lastKeyDiff
	
	float secondTimer = 0;
	float countdownTimer = 0;
	
	float timeLimit = 10;
	
	int mode = 0;				// round type
	
	Rect buttonSizeRect;
	Rect buttonBoxRect;
	Rect countdownSizeRect;
	Rect finalReportRect;
	Rect creditsRect;
	
	public GUIStyle MyStyleBox;
	public GUIStyle MyStyleLabel;
	
	AudioSource audioSource;
	AudioSource audioSourcePing;
	public AudioClip sndPing;
	
	// Use this for initialization
	void Start () {
		
		scale = 0;
		scaleNorm = 0;
		overallTimer = 0;
		lastKey = -1;
		lastKeyDiff = 0;
		lastKeyTime = 0;
		aggregateDiff = 0;
	
		secondTimer = 0;
		
		buttonBoxRect =  new Rect(0, 0, 300, 220);
		buttonBoxRect.x = (Screen.width / 2) - (buttonBoxRect.width / 2);
		buttonBoxRect.y = (Screen.height / 2) - (buttonBoxRect.height / 2);

		buttonSizeRect = new Rect(10, 0, buttonBoxRect.width - 20, 20);
		buttonSizeRect.x = (Screen.width / 2) - (buttonSizeRect.width / 2);	
		
		finalReportRect = new Rect(0,0,150,150);
		finalReportRect.x = Screen.width /2 - finalReportRect.width/2;
		finalReportRect.y = Screen.height /2 - finalReportRect.height/2;
		
		creditsRect = new Rect(150,Screen.height - 74,Screen.width - 300, 30);
		//creditsRect.x = creditsRect.width / 2;
		
		countdownSizeRect = new Rect(10,0,buttonBoxRect.width - 20, 20);
		//countdownSizeRect.x = countdownSizeRect.x = (Screen.width / 2) - (countdownSizeRect.width / 2);	
		//countdownSizeRect.y = buttonBoxRect.y + (buttonBoxRect.height /2);
		
		
		GameObject gameObj = GameObject.FindGameObjectWithTag("MainCamera");
		Camera c = gameObj.camera;
		Debug.Log (c);
		audioSource = c.audio;
				
		
		// set up ping
		audioSourcePing = gameObject.AddComponent<AudioSource>();
		audioSourcePing.clip = sndPing;
		
		// turn off ping
		audioSourcePing.enabled = false;				

		
		// turn off orb
		gameObject.renderer.enabled = false;
		gameObject.light.enabled = false;
		
		
		
	}
	
	// Update is called once per frame
	void Update () {
	
		if (STATE == STATE_START)
		{
			//STATE = STATE_PREGAME;
		
			// OnGUI should get us out of here
		}
		else if (STATE == STATE_PREGAME)
		{
			
			// handle the mode here.
			
			Debug.Log ("Mode: " + mode);
			
			// ping
			if (mode == 1)
			{
				audioSourcePing.enabled = true;				
				
			}
			// aural prompt
			if (mode == 1 || mode == 2)
			{
				audioSource.Play();
				audioSource.pitch = 1.0f;
			}	
			// orb
			if (mode == 1 || mode == 2 || mode == 3)
			{					
				gameObject.renderer.enabled = true;
				gameObject.light.enabled = true;
				
			}
			//if (mode == 3)
			//{
			//	gameObject.renderer.enabled = true;
			//	gameObject.light.enabled = true;
			//}
						
			
			STATE = STATE_GAME;
		}
		else if (STATE == STATE_COUNTDOWN)
		{
			
		}
		else if (STATE == STATE_GAME)
		{
			// increment the overall timer since the round began
			overallTimer += Time.deltaTime;

			secondTimer += Time.deltaTime;
			if (secondTimer > 1)
			{
				if (audioSourcePing.enabled)
					audioSourcePing.Play();
				secondTimer -= 1;
			}
			
			// scale will be between -1 and 1
			scale = Mathf.Cos(overallTimer * (60 / 60) * Mathf.PI * 2);		
		
			// set scale between 0 and 1
			scaleNorm = (scale + 1) * .5f;
			//Debug.Log (scaleNorm);

			//audioSource.volume = scaleNorm / 2 + .5f;
			audioSource.pitch = scaleNorm /2 + .5f;
			
			// update the light effect with the current scale
			light.range = (1 - scale) *.5f + 1;
			light.intensity = 1 - scale;
			
			
			// check for user input
			if (Input.GetButtonDown("Jump"))
			{
				lastKey = scaleNorm;
				lastKeyDiff = 1 - lastKey;				
				lastKeyTime  = overallTimer;
				aggregateDiff += lastKeyDiff;
				if (audioSourcePing.enabled)
					audioSourcePing.Play();
			}
			
			if ( (overallTimer - lastKeyTime) > 1.5)
			{
				aggregateDiff += 1;//(overallTimer - lastKeyTime);
				lastKeyTime = (int)overallTimer;
				Debug.Log ("missed");
			}
			
			if (overallTimer > (timeLimit + .5f) )
			{
				STATE = STATE_POSTGAME;
			}
		}
		
		
		
	}
	
	void OnGUI() {
	
		if (STATE == STATE_START)
		{
			GUI.Label(new Rect(400-250,40,500,100),"LoneStranger's PULSE\n\nClick the screen or hit space when the pulse is at it's minimum.\nIt will pulse for ten seconds. The closer you are to the minimum,\nthe better the score.", MyStyleBox);
			
			
			GUI.Label(creditsRect,"Made for Ludum Dare 48 #17 and One Game A Month: August", MyStyleBox);
			
			
			GUI.Box(buttonBoxRect, "Level Select", MyStyleBox);
			
			buttonSizeRect.y = buttonBoxRect.y + 30 * 1;
			if (GUI.Button(buttonSizeRect,"Aural Orb w/Metronome"))
				mode = 1; 

			buttonSizeRect.y = buttonBoxRect.y + 30 * 2;
			if (GUI.Button(buttonSizeRect,"Aural Orb"))
				mode = 2; 

			buttonSizeRect.y = buttonBoxRect.y + 30 * 3;
			if (GUI.Button(buttonSizeRect,"Orb"))
				mode = 3; 			
			
			buttonSizeRect.y = buttonBoxRect.y + 30 * 4;
			if (GUI.Button(buttonSizeRect,"Darkness"))
				mode = 4; 			

			if (mode > 0)
				STATE = STATE_COUNTDOWN;
			
			countdownTimer = 5;
			
		}
		else if (STATE == STATE_COUNTDOWN)
		{
			countdownTimer -= Time.deltaTime;
			if (countdownTimer < 0)
				STATE = STATE_PREGAME;
			
			GUI.BeginGroup(new Rect(400 - 75, 300 / 2 - 75, 150, 150),"READY", MyStyleBox);
			//	GUI.Box(new Rect(Screen.width /2 - 75, Screen.height /2 - 75, 150, 150),"READY", MyStyleBox);
				GUI.Label(new Rect(25, 25, 110, 100), "" + (int)countdownTimer, MyStyleLabel);
			GUI.EndGroup ();

		}
		else if (STATE == STATE_GAME)
		{
			
			
			string s = "scaleNorm:" + scaleNorm;
				s += "\nlast key: " + lastKey;
			    //s += "\ntimer:    " + timer;
			    s += "\ndiff:     " + lastKeyDiff;
			    s += "\nagg. diff:" + aggregateDiff;
				if (scale < 0)
					s += "\n-";
				if (scale == 0)
					s += "\n ";
				if (scale > 0)
					s += "\n+";
			
			//GUI.Box(new Rect(10,10,200,90), s);
		}
		else if (STATE == STATE_POSTGAME)
		{
			
			GUI.Box(finalReportRect, "G-A-M-E O-V-E-R\nYou were off by\na total of\n" + aggregateDiff + " seconds.");
			
			buttonSizeRect.y = finalReportRect.y + 80;
			if (GUI.Button(new Rect(finalReportRect.x + 10,finalReportRect.y + 80,60,25),"Restart"))
				Application.LoadLevel(0);	
			
			audioSource.Stop ();
			
		}
		
		
		
	}
	
	
}
