using UnityEngine;
using System.Collections;

public class animalMove : MonoBehaviour {

	private bool stopped;
	public float stopInterval;
	private float stopTime;
	public BoxCollider2D moveZone;
	private bool moving;
	public float speed;
	public float radius;
	private Vector3 moveCorn;
	private GameObject[] players;
	public float runVel;
	private Animator anim;
	private bool clamped = true;
	private bool repenting;
	private float repNum;

	// Use this for initialization
	void Start () {
		players = GameObject.FindGameObjectsWithTag ("Player");
		anim = gameObject.GetComponent<Animator> ();
	}
	
	// Update is called once per frame
	void Update () {

		if (moveZone == null)
			return;
		/*
		corn1 = new Vector3(moveZone.bounds.min.x, moveZone.bounds.min.y, 0);
		corn2 = new Vector3(moveZone.bounds.max.x, moveZone.bounds.min.y, 0);
		corn3 = new Vector3(moveZone.bounds.min.x, moveZone.bounds.max.y, 0);
		corn4 = new Vector3(moveZone.bounds.max.x, moveZone.bounds.max.y, 0);
*/		
		if (repenting) {
			anim.SetBool ("Moving", true);
			anim.SetBool ("Panic", false);
			clamped = false;

			if (repNum == 1) {

				BoxCollider2D b = GameObject.Find ("RepentenceBlue").GetComponent<BoxCollider2D> ();

				gameObject.transform.position = Vector3.MoveTowards (transform.position, new Vector3 (Random.Range (b.bounds.min.x, b.bounds.max.x), 
					Random.Range (b.bounds.min.y, b.bounds.max.y)), speed * Time.deltaTime);

				return;
			} else {
				BoxCollider2D b = GameObject.Find ("RepentenceRed").GetComponent<BoxCollider2D> ();

				gameObject.transform.position = Vector3.MoveTowards (transform.position, new Vector3 (Random.Range (b.bounds.min.x, b.bounds.max.x), 
					Random.Range (b.bounds.min.y, b.bounds.max.y)), speed * Time.deltaTime);
				return;
			}



			gameObject.transform.position = Vector3.MoveTowards (transform.position, moveCorn, speed * Time.deltaTime);
		}

		if (Vector3.Distance (gameObject.transform.position, players [0].transform.position) < radius) {
			moving = false;
			MoveAway (0);
		} else if (Vector3.Distance (gameObject.transform.position, players [1].transform.position) < radius) {
			MoveAway (1);
			moving = false;
		} else {
			anim.SetBool ("Panic", false);
		}

		stopTime += Time.deltaTime;

		//Debug.Log (stopTime);

		if (stopTime >= stopInterval) {

			stopTime = 0;
			moving = !moving;
			int r = Random.Range(1, 4);
			moveCorn = new Vector3 (Random.Range (moveZone.bounds.min.x, moveZone.bounds.max.x),
				Random.Range (moveZone.bounds.min.y, moveZone.bounds.max.y));

			/*
			if (r == 1){
				moveCorn = corn1;
			}else if(r == 2){
				moveCorn = corn2;
			}else if(r == 3){
				moveCorn = corn3;
			}else if(r == 4){
				moveCorn = corn4;
			}*/
		}

		if (moving == true) {
			//	Debug.Log ("gonna move");
			Move ();
		} else {
			anim.SetBool ("Moving", false);
			//anim.SetBool ("Panic", false);
		}

		if (transform.parent != null){
			anim.SetBool ("Panic", true);
		}

	}
	/*
	void OnTriggerEnter2D (Collider2D col){
		Debug.Log ("oh shite, a trigger!");

		if (col.gameObject.tag == "AnimalZone" && moveZone == null) {
			moveZone = col.gameObject.GetComponent<BoxCollider2D> ();


			Debug.Log ("found the zone");
		}
	}*/

	void Move(){
		anim.SetBool ("Moving", true);
		anim.SetBool ("Panic", false);
		gameObject.transform.position = Vector3.MoveTowards (transform.position, moveCorn, speed * Time.deltaTime);
	}


	void MoveAway(int p){
		if (transform.parent == null) {
			anim.SetBool ("Panic", true);
			anim.SetBool ("Moving", false);
			Vector3 v = (transform.position - players [p].transform.position).normalized;
			gameObject.transform.Translate (v * runVel * Time.deltaTime/*transform.TransformPoint(v), speed * Time.deltaTime*/);

			if(clamped)
			gameObject.transform.position = new Vector3 (Mathf.Clamp (transform.position.x, moveZone.bounds.min.x, moveZone.bounds.max.x), 
				Mathf.Clamp (transform.position.y, moveZone.bounds.min.y, moveZone.bounds.max.y));
		}
	}

	void Repent(float f){
		repenting = true;
		repNum = f;
	}
}
