using UnityEngine;
using System.Collections;

public class PlayerControllerScript : MonoBehaviour {

	public bool DirIsRight;
	public bool AI;
	public int AILevel;
	public int selectedLane;
	public int selectedColor;

	public GameObject GIZMO_REF;
	GizmoScript GS_REF;
	public GameObject HEART_REF;
	MeshRenderer HEARTMR_REF;
	public GameObject MinibotPrefab;
	public GameObject MinibotPrefab2;
	public GameObject[] spawners;



	const int HEART = 0;
	const int RED = 1;
	const int BLU = 2;
	const int GRN = 3;
	const int NUM_LANES = 3;

	bool AI_swapcolor = false;
	bool AI_acted_this_beat = false;

	float beattimer;

	// Use this for initialization
	void Start () {

		selectedLane = 1;
		selectedColor = RED;
		HEARTMR_REF = (MeshRenderer)HEART_REF.GetComponent ("MeshRenderer");
		GS_REF = (GizmoScript)GIZMO_REF.GetComponent ("GizmoScript");
	
	}
	
	// Update is called once per frame
	void Update () {

		switch(selectedColor)
		{
		case RED:
			HEARTMR_REF.material.SetColor("_Color", new Color(0.84f, GS_REF.beat_timer, 0.84f));
			break;
		case GRN:
			HEARTMR_REF.material.SetColor("_Color", new Color(0.84f, 0.84f, GS_REF.beat_timer));
			break;
		case BLU:
			HEARTMR_REF.material.SetColor("_Color", new Color(GS_REF.beat_timer, 0.84f, 0.84f));
			break;
		default:
			HEARTMR_REF.material.SetColor("_Color", new Color(GS_REF.beat_timer + 0.5f, GS_REF.beat_timer+ 0.5f, GS_REF.beat_timer+ 0.5f));
			break;
		}

		if(!AI)
		{
			if(DirIsRight)
			{
				if(Input.GetKeyDown ("q"))
				{
					selectedColor = RED;
				}
				if(Input.GetKeyDown ("a"))
				{
					selectedColor = GRN;
				}
				if(Input.GetKeyDown ("z"))
				{
					selectedColor = BLU;
				}
				if(Input.GetKeyDown ("w"))
				{
					selectedLane = 0;
					SpawnMinibot();
				}
				if(Input.GetKeyDown ("s"))
				{
					selectedLane = 1;
					SpawnMinibot();
				}
				if(Input.GetKeyDown ("x"))
				{
					selectedLane = 2;
					SpawnMinibot();
				}
				if(GS_REF.gameTime > 8.5f && GS_REF.gameTime < 16.5f && Input.GetKeyDown (KeyCode.Space))
				{
					if (GS_REF.beat_timer < 0.15f || GS_REF.beat_timer > 0.45f)
					{
						GS_REF.Score (0, true);
					}
				}

			}
			else
			{
				if(Input.GetKeyDown ("o"))
				{
					selectedColor = RED;					
				}
				if(Input.GetKeyDown ("l"))
				{
					selectedColor = GRN;					
				}
				if(Input.GetKeyDown ("."))
				{
					selectedColor = BLU;					
				}
				if(Input.GetKeyDown ("i"))
				{
					selectedLane = 0;
					SpawnMinibot();
				}
				if(Input.GetKeyDown ("k"))
				{
					selectedLane = 1;
					SpawnMinibot ();
				}
				if(Input.GetKeyDown (","))
				{
					selectedLane = 2;
					SpawnMinibot ();
				}
			}
		}
		else
		{
			if(GS_REF.tutorial)
			{
				if(GS_REF.gameTime > 16.0f)
				{
					int locrng = Random.Range (0, 2);
					if(GS_REF.beat_timer < 0.1f && !AI_acted_this_beat)
					{
						AI_acted_this_beat = true;
						if(AI_swapcolor)
						{
							switch(selectedColor)
							{
							case RED:
								if(locrng == 0)
									selectedColor = BLU;
								else
									selectedColor = GRN;
								break;
							case BLU:
								if(locrng == 0)
									selectedColor = RED;
								else
									selectedColor = GRN;
								break;
							case GRN:
								if(locrng == 0)
									selectedColor = RED;
								else
									selectedColor = BLU;
								break;
							}
							AI_swapcolor = !AI_swapcolor;
						}
						else
						{
							locrng += Random.Range (0,2);
							selectedLane = locrng;
							SpawnMinibot();
							AI_swapcolor = !AI_swapcolor;
						}
					}
					if(GS_REF.beat_timer > 0.4f)
					{
						AI_acted_this_beat = false;
					}
				}

			}
			else
			{
				int locrng = Random.Range (0, 2);
				if(GS_REF.beat_timer < 0.1f && !AI_acted_this_beat)
				{
					AI_acted_this_beat = true;
					if(AI_swapcolor)
					{
						switch(selectedColor)
						{
						case RED:
							if(locrng == 0)
								selectedColor = BLU;
							else
								selectedColor = GRN;
							break;
						case BLU:
							if(locrng == 0)
								selectedColor = RED;
							else
								selectedColor = GRN;
							break;
						case GRN:
							if(locrng == 0)
								selectedColor = RED;
							else
								selectedColor = BLU;
							break;
						}
						AI_swapcolor = !AI_swapcolor;
					}
					else
					{
						locrng += Random.Range (0,2);
						selectedLane = locrng;
						SpawnMinibot();
						AI_swapcolor = !AI_swapcolor;
					}
				}
				if(GS_REF.beat_timer > 0.4f)
				{
					AI_acted_this_beat = false;
				}
			}

		}
	}

	void SpawnMinibot()
	{
		if((GS_REF.gameTime > 16.5f && !GS_REF.gameover) || GS_REF.tutorial)
		{
			GameObject mbspawn_ref;
			if(DirIsRight)
				mbspawn_ref = (GameObject)Instantiate (MinibotPrefab,
				                                       spawners[selectedLane].transform.position,
				                                       MinibotPrefab.transform.rotation);
			else
				mbspawn_ref = (GameObject)Instantiate (MinibotPrefab2,
				                                       spawners[selectedLane].transform.position,
				                                       MinibotPrefab.transform.rotation);
			MinibotScript mbscript_ref = (MinibotScript)mbspawn_ref.GetComponent("MinibotScript");
			mbscript_ref.bottype = selectedColor;
			mbscript_ref.DirIsRight = DirIsRight;
			mbscript_ref.GS_REF = GS_REF;

			if (DirIsRight && (GS_REF.beat_timer < 0.10f || GS_REF.beat_timer > 0.4f))
			{
				mbscript_ref.shining = true;
			}
		}


	}
}
