using UnityEngine;
using System.Collections;

public class ZombiesBehaviour : MonoBehaviour {

	//kind of Zombie (01, 02, 03)
	public int kind;
	//values Direction.UP, DOWN, LEFT, RIGHT
	private int currentDirection;
	public float speed;
	private int state;
	//public float resistenciaTiro;
		
	public float timeWaitChangeDirection;

	/*Components*/
	private Animator animator;

	/*lineCast*/
	public bool naMiraL,naMiraR,naMiraD,naMiraU;
	public Transform alvoL,alvoR,alvoD,alvoU;

	/*buttons*/
	public GameObject btL,btR,btD,btU;

	//posiçao onde os zubi dos palmares aparecera
	//public Transform posicaoInicial;

	// Use this for initialization
	void Start () {
		naMiraL = false;
		naMiraR = false;
		naMiraD = false;
		naMiraU = false;
		//teste
		//this.transform.position = posicaoInicial.position;
		state = State.WALKING;
		animator = gameObject.GetComponent<Animator> ();
		currentDirection = Direction.RIGHT;
		//speed = 1;
		//life = 5;
		timeWaitChangeDirection = 5;

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



		if (state == State.CHANGING) {
			if(timeWaitChangeDirection <= 0){
			//	print ("chage");
				/*
				
				ArrayList a = new ArrayList();
				
				if(!naMiraD && currentDirection != Direction.UP){
					a.Add(Direction.DOWN);
				}
				if(!naMiraU && currentDirection != Direction.DOWN){
					a.Add(Direction.UP);
				}
				if(!naMiraL && currentDirection != Direction.RIGHT){
					a.Add(Direction.LEFT);
				}
				if(!naMiraR && currentDirection != Direction.LEFT){
					a.Add( Direction.RIGHT);
				}
				
				int rand;
				int index = 0;
				
				if(a.Count == 4){
					rand = Random.Range(0,20);
					
					if(rand <= 5)
						index=0;
					else if(rand <= 10)
						index=1;
					else if(rand <= 15)
						index=2;
					else
						index=3;
					
				}else if(a.Count == 3){
					rand = Random.Range(0,21);
					
					if(rand <= 7)
						index=0;
					else if(rand <= 14)
						index=1;
					else
						index=2;
					
				}else if(a.Count == 2){
					rand = Random.Range(0,20);
					if(rand <= 10)
						index=0;
					else 
						index=1;
					
				}
				
				changeDirection(index);

				
				setState(State.WALKING);*/
			
			}else{

				timeWaitChangeDirection -= 1 *Time.deltaTime;
			
				if((naMiraL==false) && (currentDirection != Direction.RIGHT) ){
				//	btL.transform.position = alvoL.position;
					btL.SetActive(true);
				}
				if((naMiraR==false)&& (currentDirection != Direction.LEFT) ){
				//	btR.transform.position = alvoR.position;
					btR.SetActive(true);
				}
				if((naMiraD==false)&& (currentDirection != Direction.UP) ){
			//		btD.transform.position = alvoD.position;
					btD.SetActive(true);
				}
				if((naMiraU==false)&& (currentDirection != Direction.DOWN) ){
			//		btU.transform.position = alvoU.position;
					btU.SetActive(true);
				}
			
			}
		
		}else{

			float speedLocal;

			if (state == State.THINKING) {		

				speedLocal = speed/2;
			
		    //State.WALKING
			}else{
			
				speedLocal = speed;
			
			}

		   //moving
			
		   if (currentDirection == Direction.RIGHT) {

				this.transform.position = new Vector3(this.transform.position.x + (speedLocal * Time.deltaTime), this.transform.position.y, this.transform.position.z) ;

			}else if (currentDirection == Direction.LEFT) {

				this.transform.position = new Vector3(this.transform.position.x - speedLocal * Time.deltaTime, this.transform.position.y, this.transform.position.z);
			
			}else if (currentDirection == Direction.UP) {
				
				this.transform.position = new Vector3(this.transform.position.x,this.transform.position.y + speedLocal * Time.deltaTime,this.transform.position.z);
				
			}else if (currentDirection == Direction.DOWN) {
			
				this.transform.position = new Vector3(this.transform.position.x, this.transform.position.y -speedLocal * Time.deltaTime, this.transform.position.z);
			
			}
		
		}

	}


	public void changeDirection(int direction){

		if ((direction== Direction.UP)|| 
			(direction== Direction.DOWN)|| 
			(direction== Direction.LEFT)|| 
			(direction== Direction.RIGHT))
			currentDirection = direction;
		if (((direction == Direction.UP) || (direction == Direction.DOWN) || (direction == Direction.RIGHT)))
						this.transform.eulerAngles = new Vector3 (0,0,0);
		else			this.transform.eulerAngles = new Vector3 (0,180,0);

		if (state == State.CHANGING)
		   setState (State.WALKING);
	}
	 
	void  OnTriggerEnter2D(Collider2D collider) {
		if (collider.tag == "doubleWay") {
			setState(State.THINKING);
		}else if(collider.tag == "powerup"){
			
			PowerUP pu = collider.GetComponent<PowerUP>();
			
			//life
			if(pu.kind == 0){
				this.GetComponentInChildren<BarraLife>().resertLife();		
			}else if(pu.kind == 1){			
		
				//+2 speed
				speed *= 4;	
			
			}
			
			Destroy(collider.gameObject);
			
		}
	}

	void OnTriggerExit2D (Collider2D collider) {



		if (collider.tag == "doubleWay" && (collider.GetComponent<Way>().direct==currentDirection)) {

			if (currentDirection == Direction.RIGHT || currentDirection == Direction.LEFT)
				this.transform.position = new Vector2 (collider.GetComponent<Way> ().center.position.x,this.transform.position.y);
			else if(currentDirection==Direction.UP || currentDirection==Direction.DOWN )
				this.transform.position = new Vector2 (this.transform.position.x,collider.GetComponent<Way> ().center.position.y);

			naMiraD = collider.GetComponent<Way>().down;
			naMiraU = collider.GetComponent<Way>().up;
			naMiraR = collider.GetComponent<Way>().right;
			naMiraL = collider.GetComponent<Way>().left;
			setState(State.CHANGING);
		}else if(collider.tag == "uniqueWay"&& (collider.GetComponent<Way>().direct==currentDirection)){

			if (currentDirection == Direction.RIGHT || currentDirection == Direction.LEFT)
				this.transform.position = new Vector2 (collider.GetComponent<Way> ().center.position.x,this.transform.position.y);
			else if(currentDirection==Direction.UP || currentDirection==Direction.DOWN )
				this.transform.position = new Vector2 (this.transform.position.x,collider.GetComponent<Way> ().center.position.y);

			naMiraD = collider.GetComponent<Way>().down;
			naMiraU = collider.GetComponent<Way>().up;
			naMiraR = collider.GetComponent<Way>().right;
			naMiraL = collider.GetComponent<Way>().left;
			print ("exit");
			
			if (currentDirection == Direction.UP && naMiraU) {
				
				if(!naMiraD && naMiraL && naMiraR)
					revertDirection ();
				else if(!naMiraL && naMiraR){
					changeDirection(Direction.LEFT);
				}else if(naMiraL && !naMiraR){
					changeDirection(Direction.RIGHT);
				}
				
				
			}else if (currentDirection == Direction.DOWN && naMiraD) {
				
				if(!naMiraU && naMiraL && naMiraR)
					revertDirection ();
				else if(!naMiraL && naMiraR){
					changeDirection(Direction.LEFT);
				}else if(naMiraL && !naMiraR){
					changeDirection(Direction.RIGHT);
				}
				
				
				
			}else if (currentDirection == Direction.LEFT && naMiraL) {
				if(!naMiraR && naMiraD && naMiraU)
					revertDirection ();
				else if(!naMiraD && naMiraU){
					changeDirection(Direction.DOWN);
				}else if(naMiraD && !naMiraU){
					changeDirection(Direction.UP);
				}
			}if (currentDirection == Direction.RIGHT && naMiraR) {
				if(naMiraL && naMiraU && naMiraD)
					revertDirection ();
				else if(!naMiraD && naMiraU){
					changeDirection(Direction.DOWN);
				}else if(naMiraD && !naMiraU){
					changeDirection(Direction.UP);
				}
			}
			
			
			
			
		}


	}

	void setState(int state){

		this.state = state;

		if(state == State.CHANGING){

			animator.Play ("default"+kind);
			timeWaitChangeDirection = 5;
		}else if(state == State.WALKING){
			animator.Play ("default"+kind);
			btL.SetActive(false);
			btD.SetActive(false);
			btR.SetActive(false);
			btU.SetActive(false);
		}else if (state == State.THINKING){
			print ("pensando");
			//animator.Play ("Thinking");
		}
	}
	public void revertDirection(){
		
		if(currentDirection == Direction.DOWN)
			currentDirection = Direction.UP;
		else if(currentDirection == Direction.UP)
			currentDirection = Direction.DOWN;
		else if(currentDirection == Direction.LEFT)
			currentDirection = Direction.RIGHT;
		else if(currentDirection == Direction.RIGHT)
			currentDirection = Direction.LEFT;
		
	}

	public void mataZombie(){

		Destroy (this.gameObject);
	}

}
