using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class Mayor : MonoBehaviour {
	public bool doneWalking = true;
	public Vector3 waypoint;
	private float speed =  4;
	public bool oob = false;
	public bool busy = false;
	public int SocialValue;
	private float timer = 900;
	private bool waveable = true;
	GameObject player = GameObject.Find("tempChar");
	private float dist;
	public Vector3 screenPos;
	private float busytimer = 80;
	public Text valueText; 
	private float timerr = 600;
	Animator anim;
	bool ihatethatihavetodothis = false;
	bool win = false;
	public float wintimer = 160;
	public GameObject Winscreen;
	// Use this for initialization
	void Start () {
		anim = GetComponent<Animator> ();
		wintimer = 240;

	}

	// Update is called once per frame
	void Update () {
		if (win) {
			wintimer--;
		}
		if (wintimer < 0) {
			Instantiate (Winscreen, new Vector3 (0, 0, 0), Quaternion.identity);
			win = false;
			wintimer = 1;
		}

		timerr--;
		speed = 0;
		anim.SetBool ("waving", busy);
		player = GameObject.Find ("tempChar");
		float step = speed * Time.deltaTime;
		Vector3 targetdir = waypoint - transform.position;
		Vector3 newdir = Vector3.RotateTowards (transform.forward, targetdir, step, 0.0f);
		if (timerr <= 0) {
			doneWalking = true;
			timerr = 600;
		}
		if (doneWalking) {
			waypoint = Random.insideUnitSphere * 1000;
			waypoint.y = transform.position.y;
			doneWalking = false;
			oob = false;
			if (ihatethatihavetodothis) {
				waypoint.x = 90;
				ihatethatihavetodothis = false;
			}
		} else {
			if (oob) {
				doneWalking = true;
			} else {
				if(!busy){
					transform.position = Vector3.MoveTowards (transform.position, waypoint, step);
					transform.rotation = Quaternion.LookRotation (newdir);
					if (transform.position.x == waypoint.x && transform.position.z == waypoint.z) {
						doneWalking = true;
					}
				}
			}
		}

		if (oob) {
			doneWalking = true;
		}

		dist = Vector3.Distance (transform.position, player.transform.position);
		if (!waveable) {
			timer--;
			valueText.color = Color.red;
		} else {

		}
		if (timer <= 0) {
			waveable = true;
			valueText.color = Color.white;
		}
		if(Mathf.Abs((SocialValue - GameObject.Find ("GameManager").GetComponent<GameManager> ().points)) > 10){
			waveable = false;
			timer = 60;
		}
		if (dist <= 10) {
			if (Input.GetKeyDown (KeyCode.Space)) {
				waveable = true;
				if (waveable && Mathf.Abs((SocialValue - GameObject.Find ("GameManager").GetComponent<GameManager> ().points)) <= 10)  {
					GameObject.Find ("GameManager").GetComponent<GameManager> ().points++;
					waveable = false;
					timer = 900;
					busy = true;
					win = true;

					Debug.Log ("I did it");
				} else {
					GameObject.Find ("GameManager").GetComponent<GameManager> ().points--;
				}

			}
		}
		if (busy) {
			Vector3 targetdir2 = GameObject.Find ("tempChar").GetComponent<Transform> ().position - transform.position;
			Vector3 newdir2 = Vector3.RotateTowards (transform.forward, targetdir2, step, 0.0f);
			transform.rotation = Quaternion.LookRotation (newdir2);
			busytimer--;
		}
		if (busytimer <= 0) {
			busy = false;
			busytimer = 80;
		}

	}

	// When collide with anything, turn around
	void OnCollisionEnter (Collision col)
	{
		oob = true;
		doneWalking = true;
	}

	void OnTriggerEnter (Collider col)
	{
		oob = true;
		doneWalking = true;
		if (col.gameObject.name == "invisiblewall" || col.gameObject.name == "invisiblewall") {
			transform.position += transform.right * 5;
			ihatethatihavetodothis = true;

		}
	}
}

