using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Movement : MonoBehaviour {

	// Use this for initialization
	void Start () {
		//pickChar ();
	}

	public GameObject startPoint;
	
	public GameObject showFear;
	public GameObject showHung;
	public GameObject showAttr;
	public GameObject showPois;
	
	public GameObject dogChar;
	public GameObject antChar;
	public GameObject rabbitChar;
	public GameObject snakeChar;
	public GameObject humanChar;

	public Texture conceptPage;
	public Texture directionPage;

	string charGuess = "";
	bool isGuess = false;
	bool isEnd = false;
	bool isConcept = false;
	bool isDirect = false;
	string isChar = "";
	string topComm = "";

	bool isStart = true;
	
	string[] charList = new string[]{"dog", "ant", "rabbit", "snake", "person"};
	
	// Update is called once per frame
	void Update () {
		if(!isGuess){
			transform.position += transform.forward*10*Input.GetAxis ("Vertical")*Time.deltaTime;
			transform.Rotate (Vector3.up*Input.GetAxis ("Horizontal")*2);
				
			if (Input.GetButtonDown ("trigger")) {
				isGuess = true;
			}
		} else if (Input.GetButtonDown ("trigger") && isGuess) {
			Debug.Log(charGuess);
			charGuess = "";
			isGuess = false;
		}
	}
	
	void pickChar(){
		string[] fearList = new string[]{};
		string[] hungList = new string[]{};
		string[] attrList = new string[]{};
		string[] poisList = new string[]{};
		
		GameObject thisChar = new GameObject();
		
		isChar = charList[Random.Range (0, charList.Length)];
		
		switch(isChar){
		case "dog":
			thisChar = dogChar;
			
			fearList = new string[]{"snake"};
			hungList = new string[]{"rabbit", "pizza"};
			attrList = new string[]{"person", "dog", "frisbee"};
			poisList = new string[]{"chocolate"};
			break;
		case "ant":
			thisChar = antChar;
			
			fearList = new string[]{};
			hungList = new string[]{"chocolate", "fruit", "pizza", "bread"};
			attrList = new string[]{"anthill"};
			poisList = new string[]{};
			break;
		case "rabbit":
			thisChar = rabbitChar;
			
			fearList = new string[]{"snake", "person", "dog"};
			hungList = new string[]{"vegetable"};
			attrList = new string[]{"rabbit"};
			poisList = new string[]{};
			break;
		case "snake":
			thisChar = snakeChar;
			
			fearList = new string[]{"dog", "person"};
			hungList = new string[]{"rabbit"};
			attrList = new string[]{};
			poisList = new string[]{};
			break;
		case "person":
			thisChar = humanChar;
			
			fearList = new string[]{"snake"};
			hungList = new string[]{"pizza", "fruit", "bread", "chocolate"};
			attrList = new string[]{"dog", "rabbit", "person"};
			poisList = new string[]{};
			break;
		default:
			break;
		}
		
		makePart (fearList, showFear);
		makePart (hungList, showHung);
		makePart (attrList, showAttr);
		makePart (poisList, showPois);

		GameObject newChar = Instantiate (thisChar, startPoint.transform.position, Quaternion.identity) as GameObject;
		transform.position = newChar.GetComponent <ViewFind>().viewPoint.transform.position;
		transform.rotation = newChar.GetComponent <ViewFind> ().viewPoint.transform.rotation;

		newChar.transform.parent = transform;
	}
	
	void makePart(string[] targList, GameObject thisPart){
		List<GameObject> objList = new List<GameObject>();
		
		foreach (string target in targList) {
			objList.AddRange (GameObject.FindGameObjectsWithTag (target));
		}
		
		foreach (GameObject obj in objList) {
			Instantiate (thisPart, obj.transform.position, Quaternion.identity);
		}
	}

	void OnGUI(){
		GUI.Box (new Rect(Screen.width*0.5f-75, Screen.height*0.1f, 150, 30), topComm);
		if(GUI.Button (new Rect (Screen.width * 0.8f, Screen.height * 0.1f, 100, 30), "Exit")){
			Application.Quit();
		}

		if (isConcept) {
			GUI.DrawTexture (new Rect (0, 0, Screen.width, Screen.height), conceptPage);
			if (GUI.Button (new Rect (Screen.width * 0.9f - 50, Screen.height * 0.9f, 100, 20), "Back")) {
					isConcept = false;
			}
		} else if (isDirect) {
			GUI.DrawTexture (new Rect (0, 0, Screen.width, Screen.height), directionPage);
			if (GUI.Button (new Rect (Screen.width * 0.9f - 50, Screen.height * 0.9f, 100, 20), "Back")) {
				isDirect = false;
			}
		}else if (isStart) {
			if (GUI.Button (new Rect (Screen.width * 0.5f - 50, Screen.height * 0.2f, 100, 20), "Concept")) {
				isConcept = true;
			}
			if (GUI.Button (new Rect (Screen.width * 0.5f - 50, Screen.height * 0.4f, 100, 20), "Directions")) {
				isDirect = true;
			}
			if (GUI.Button (new Rect (Screen.width * 0.5f - 50, Screen.height * 0.6f, 100, 50), "Start")) {
				isStart = false;
				pickChar ();
			}
		} else if(isGuess){
			topComm = "Who are you?";
			if(GUI.Button (new Rect(Screen.width*0.3f-27.5f, Screen.height*0.9f, 55, 30), "Ant")){
				winTest ("ant");
			}
			if(GUI.Button (new Rect(Screen.width*0.4f-27.5f, Screen.height*0.9f, 55, 30), "Rabbit")){
				winTest ("rabbit");
			}
			if(GUI.Button (new Rect(Screen.width*0.5f-27.5f, Screen.height*0.9f, 55, 30), "Human")){
				winTest ("person");
			}
			if(GUI.Button (new Rect(Screen.width*0.6f-27.5f, Screen.height*0.9f, 55, 30), "Snake")){
				winTest ("snake");
			}
			if(GUI.Button (new Rect(Screen.width*0.7f-27.5f, Screen.height*0.9f, 55, 30), "Dog")){
				winTest ("dog");
			}
		}else if(isEnd){
			if(GUI.Button (new Rect(Screen.width*0.5f-50, Screen.height*0.6f, 100, 100), "Restart?")){
				Application.LoadLevel (0);
			}
		}
	}

	void winTest(string guess){
		if (guess == isChar) {
			isEnd = true;
			topComm = "I believe you're correct";
		} else {
			topComm = "Hmm, I don't think so";
		}

		isGuess = false;
	}
}
