using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class GameController : MonoBehaviour {

	public Animation waveAnim;
	public GameObject phone;
	public int peopleToCreate;
	public float xMax;
	public float zMax;

	public Text timeText;
	public Text scoreText;
	public Text scoreText2;
	public Text hintText;

    public AudioSource[] backgroundMusic;
    public AudioSource correctSound;
    public AudioSource incorrectSound;
	public GameObject prefab;
	public GameObject[] leftArm;
	public GameObject[] rightArm;
	public GameObject[] leftLeg;
	public GameObject[] rightLeg;
	public GameObject[] head;
	public GameObject[] torso;
	public GameObject heart;

	private GameObject[] crowd;
	private GameObject[] allItems;
	private GameObject thingToKnow;
	private bool phoneHidden;

	// Use this for initialization
	void Start () 
	{
		scoreText2.text = "Score: ";
		scoreText.text = "0";
		timeText.text = "60";
		var temp1 = head.Concat (torso).ToArray ();
		var temp2 = temp1.Concat (leftArm).ToArray ();
		var temp3 = temp2.Concat (rightArm).ToArray ();
		var temp4 = temp3.Concat (leftLeg).ToArray ();
		allItems = temp4.Concat (rightLeg).ToArray ();
		crowd = new GameObject[GameInfo.getCrowdSize()+1];
		CreateNumberOfPeople (GameInfo.getCrowdSize(), xMax, zMax);
		InvokeRepeating ("countDown", 1.0f, 1.0f);
	}

	// Update is called once per frame
	void Update () 
	{

		if (Input.GetMouseButtonDown (0) && !waveAnim.isPlaying) 
		{
			waveAnim.Play ();
			RaycastHit hit; 
			Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 
			if ( Physics.Raycast (ray,out hit,100.0f)) {
				if (hit.transform.name == "Correct") 
				{
                    correctSound.Play();
					GameInfo.addCorrect ();
					Destroy (hit.transform.gameObject);
					CreateNumberOfPeople (1, xMax, zMax);
					int newScore = int.Parse (scoreText.text) + 1;
					scoreText.text = newScore + "";
				}
				else 
				{
                    incorrectSound.Play();
					GameInfo.addWaved ();
				}
			}
		}

		if (Input.GetMouseButtonDown (1)) 
		{
			if (phoneHidden) 
			{
				var pos = new Vector3 (195.4f, 77.2f);
				phone.transform.position = pos;
				phoneHidden = false;
			}
			else 
			{
				phoneHidden = true;
				var pos = new Vector3 (-316.6f, -800.0f);
				phone.transform.position = pos;
			}
		}
	}

	void countDown()
	{
		int oldTime = int.Parse (timeText.text);
		//if(oldTime > 30){
            //backgroundMusic[0].Play(); 
        //}
		if(oldTime == 40){
            backgroundMusic[0].Stop(); 
            backgroundMusic[1].Play(); 
        }
		if(oldTime == 20){
            backgroundMusic[1].Stop(); 
            backgroundMusic[2].Play(); 
        }
		if(oldTime == 10){
            backgroundMusic[2].Stop(); 
            backgroundMusic[3].Play(); 
        }
		if(oldTime == 6){
            backgroundMusic[3].Stop(); 
            backgroundMusic[4].Play(); 
        }
		if(oldTime == 3){
            backgroundMusic[4].Stop(); 
            backgroundMusic[5].Play(); 
        }
		if(oldTime == 0)
		{
            backgroundMusic[3].Stop(); 
			SceneManager.LoadScene ("End");
		}
		int newTime = oldTime - 1;
		timeText.text = newTime+"";
	}

	string CreateHint(string theName)
	{
		if (theName.Contains ("Clone")) 
		{
			theName = theName.Substring (0, theName.Length - 7);
		}
		switch (theName) 
		{
		case "tentacle":
			return "You might've seen enough hentai to know where this is going.\n";
		case "microwave":
			return "I'm wearing a Micro. Wave at me.\n";
		case "handbag":
			return "You'd be surprised how much I can fit into this bag!\n";
		case "baguette":
			return "Don't you just love baguettes? Hon hon, oui oui!\n";
		case "chair":
			return "If you feel like sitting down, well, you're in good company!\n";
		case "lady_leg":
			return "I'm wearing something fishy.\n";
		case "fire_hydrant":
			return "I hope there aren't any dogs nearby!\n";
		case "flower_pot":
			return "Potty Squatty!\n";
		case "Toast":
			return "I'm feeling really toasty!\n";
		case "eggs":
			return "I had a really nice english breakfast earlier.\n";
		case "lady_body":
			return "I'm thin as air, just so you know.\n";
		case "Cactus":
			return "Careful of you might get stung.\n";
		case "Lamp":
			return "I'm full of bright ideas.\n";
		default:
			return "Didn't find anything with the name " + theName;
		}
	}

	void CreateNumberOfPeople(int x, float xMax, float zMax)
	{
		int RightPerson = Random.Range (0, x-1);
		int whichArray;
		thingToKnow = allItems[Random.Range (0, allItems.Length-1)];
		Debug.Log ("The specific item: " + thingToKnow.name);

		hintText.text = CreateHint (thingToKnow.name);

		if (torso.Contains (thingToKnow))
			whichArray = 0;

		if (rightArm.Contains (thingToKnow))
			whichArray = 1;

		if (leftArm.Contains (thingToKnow))
			whichArray = 2;

		if (rightLeg.Contains (thingToKnow))
			whichArray = 3;

		if (leftLeg.Contains (thingToKnow))
			whichArray = 4;

		if (head.Contains (thingToKnow))
			whichArray = 5;

		for (int i = 0; i < x; i++) 
		{
			int randBodyPart = Random.Range (0, 6);
			while (randBodyPart == whichArray) 
			{
				//Debug.Log ("RandPart: " + randBodyPart + " - And whichArray: " + whichArray);
				randBodyPart = Random.Range (0, 6);
			}
			//Debug.Log ("After while.\nRandPart: " + randBodyPart + " - And whichArray: " + whichArray);
			float randX = (Random.value + 0.5f) * xMax - xMax;
			float randZ = (Random.value + 0.5f) * zMax - zMax;

			var obj1 = Instantiate(prefab);

			var obj1Armature = obj1.transform.Find ("Armature");

			var obj1Bone = obj1Armature.transform.Find ("Bone");


			//Torso
			if (randBodyPart != 0) 
			{

				//We make the cube body part invisible
				var rend1 = obj1.transform.Find ("Cube").gameObject.GetComponent<Renderer> ();
				rend1.enabled = false;

				//We attach the new body part
				var obj1TorsoTrans = obj1Bone.transform;

				GameObject obj1TorsoObj;
				if (whichArray == 0 && i == RightPerson) 
				{
					obj1TorsoObj = Instantiate (thingToKnow);

				}
				else if(whichArray == 0)
				{
					obj1TorsoObj = Instantiate (torso [Random.Range (0, torso.Length - 1)]);
					while (obj1TorsoObj == thingToKnow) 
					{
						obj1TorsoObj = Instantiate (torso [Random.Range (0, torso.Length - 1)]);
					}
				}
				else
				{
					obj1TorsoObj = Instantiate (torso [Random.Range (0, torso.Length - 1)]);
				}

				if(i == RightPerson && whichArray != 0) 
				{
					hintText.text = hintText.text + "\n" + CreateHint (obj1TorsoObj.name);
				}
				obj1TorsoObj.transform.SetParent (obj1TorsoTrans);
			}

			//Right arm
			if (randBodyPart != 1) 
			{
				//We make the cube body part invisible
				var rend2 = obj1.transform.Find ("Cube.002").gameObject.GetComponent<Renderer>();
				rend2.enabled = false;

				//We attach the new body part
				var obj1RightArmTrans = obj1Bone.transform.Find ("Bone.003");

				GameObject obj1RightArmObj;
				if (whichArray == 1 && i == RightPerson) 
				{
					obj1RightArmObj = Instantiate (thingToKnow);
				}
				else if(whichArray == 1)
				{
					obj1RightArmObj = Instantiate (rightArm[Random.Range(0, rightArm.Length-1)]);
					while (obj1RightArmObj == thingToKnow) 
					{
						obj1RightArmObj = Instantiate (rightArm[Random.Range(0, rightArm.Length-1)]);
					}

				}
				else
				{
					obj1RightArmObj = Instantiate (rightArm[Random.Range(0, rightArm.Length-1)]);
				}

				obj1RightArmObj.transform.SetParent (obj1RightArmTrans);
			}

			//Left arm
			if (randBodyPart != 2) 
			{
				//We make the cube body part invisible
				var rend3 = obj1.transform.Find ("Cube.003").gameObject.GetComponent<Renderer>();
				rend3.enabled = false;

				//We attach the new body part
				var obj1LeftArmTrans = obj1Bone.transform.Find ("Bone.002");

				GameObject obj1LeftArmObj;
				if (whichArray == 2 && i == RightPerson) 
				{
					obj1LeftArmObj = Instantiate (thingToKnow);
				}
				else if(whichArray == 2)
				{
					obj1LeftArmObj = Instantiate (leftArm[Random.Range(0, leftArm.Length-1)]);
					while (obj1LeftArmObj == thingToKnow) 
					{
						obj1LeftArmObj = Instantiate (leftArm[Random.Range(0, leftArm.Length-1)]);
					}
				}
				else
				{
					obj1LeftArmObj = Instantiate (leftArm[Random.Range(0, leftArm.Length-1)]);
				}

				obj1LeftArmObj.transform.SetParent (obj1LeftArmTrans);
			}

			//Right leg
			if (randBodyPart != 3) 
			{
				//We make the cube body part invisible
				var rend4 = obj1.transform.Find ("Cube.004").gameObject.GetComponent<Renderer>();
				rend4.enabled = false;

				//We attach the new body part
				var obj1RightLegTrans = obj1Bone.transform.Find ("Bone.005");

				GameObject obj1RightLegObj;
				if (whichArray == 3 && i == RightPerson) 
				{
					obj1RightLegObj = Instantiate (thingToKnow);
				}
				else if(whichArray == 3)
				{
					obj1RightLegObj = Instantiate (rightLeg[Random.Range(0, rightLeg.Length-1)]);
					while (obj1RightLegObj == thingToKnow) 
					{
						obj1RightLegObj = Instantiate (rightLeg[Random.Range(0, rightLeg.Length-1)]);
					}
				}
				else
				{
					obj1RightLegObj = Instantiate (rightLeg[Random.Range(0, rightLeg.Length-1)]);
				}
				obj1RightLegObj.transform.SetParent (obj1RightLegTrans);
			}

			//Left leg
			if (randBodyPart != 4) 
			{
				//We make the cube body part invisible
				var rend5 = obj1.transform.Find ("Cube.005").gameObject.GetComponent<Renderer>();
				rend5.enabled = false;

				//We attach the new body part
				var obj1LeftLegTrans = obj1Bone.transform.Find ("Bone.004");

				GameObject obj1LeftLegObj;
				if (whichArray == 4 && i == RightPerson) 
				{
					obj1LeftLegObj = Instantiate (thingToKnow);
				}
				else if(whichArray == 4)
				{
					obj1LeftLegObj = Instantiate (leftLeg[Random.Range(0, leftLeg.Length-1)]);
					while (obj1LeftLegObj == thingToKnow) 
					{
						obj1LeftLegObj = Instantiate (leftLeg[Random.Range(0, leftLeg.Length-1)]);
					}
				}
				else
				{
					obj1LeftLegObj = Instantiate (leftLeg[Random.Range(0, leftLeg.Length-1)]);
				}
				obj1LeftLegObj.transform.SetParent (obj1LeftLegTrans);
			}

			//Head
			if (randBodyPart != 5) 
			{
				//We make the cube body part invisible
				var rend6 = obj1.transform.Find ("Cube.001").gameObject.GetComponent<Renderer>();
				rend6.enabled = false;

				//We attach the new body part
				var obj1HeadTrans = obj1Bone.transform.Find ("Bone.001");

				GameObject obj1HeadObj;
				if (whichArray == 5 && i == RightPerson) 
				{
					obj1HeadObj = Instantiate (thingToKnow);
				}
				else if(whichArray == 5)
				{
					obj1HeadObj = Instantiate (head[Random.Range(0, head.Length-1)]);
					while (obj1HeadObj == thingToKnow) 
					{
						obj1HeadObj = Instantiate (head[Random.Range(0, head.Length-1)]);
					}
				}
				else
				{
					obj1HeadObj = Instantiate (head[Random.Range(0, head.Length-1)]);
				}

				if (i == RightPerson && whichArray != 5) 
				{
					hintText.text = hintText.text + "\n" + CreateHint (obj1HeadObj.name);
				}
				obj1HeadObj.transform.SetParent (obj1HeadTrans);
			}

			var obj1Heart = Instantiate (heart);
			obj1Heart.transform.SetParent (obj1.transform);
			var pos = new Vector3 (randX, 2.1f, randZ);
			obj1.transform.position = pos;
			obj1.AddComponent<move> ();
			CapsuleCollider coll = obj1.AddComponent<CapsuleCollider> ();
			obj1.AddComponent<MouseOver> ();
			coll.height = 5.0f;
			coll.radius = 2.5f;
			coll.center = new Vector3 (0, 1, 0);

			if (i == RightPerson) 
			{
				obj1.name = "Correct";
			} 
			else
			{
				obj1.name = "False" + i;
			}

			crowd [i] = obj1;
		}
	}
}
