using UnityEngine;
using System.Collections;

public class ChooseZombie : MonoBehaviour {
	
	private Vector3 aux;
	private bool colidiu = false;
	public float distanciaMinima;
	public Transform zombie;
	private Vector2 origem;
	
	/* V5 */ 
	public int kind;
	
	private float defaultCoolDownZumbiKind0 = 2;
	private float defaultCoolDownZumbiKind1 = 2;
	private float defaultCoolDownZumbiKind2 = 2;
	
	private static float coolDownZumbiKind0;
	private static float coolDownZumbiKind1;
	private static float coolDownZumbiKind2;
	
	private static bool DragDropedKind0 = false;
	private static bool DragDropedKind1 = false;
	private static bool DragDropedKind2 = false;
	
	private Sprite spriteEnabled;
	public Sprite spriteDisable;


	// Use this for initialization
	
	void Start () {
		origem = new Vector3 (transform.position.x,transform.position.y, transform.position.z);
		
		//getValue default of Zombie Manager
		GameObject zombieMag = GameObject.Find ("ZombieManager");
		ZombieManager ZMscript = zombieMag.GetComponent<ZombieManager> ();
		
		defaultCoolDownZumbiKind0 = ZMscript.defaultCoolDownZumbiKind0;
		defaultCoolDownZumbiKind1 = ZMscript.defaultCoolDownZumbiKind1;
		defaultCoolDownZumbiKind2 = ZMscript.defaultCoolDownZumbiKind2;


		if(kind == 0)
			coolDownZumbiKind0 = defaultCoolDownZumbiKind0;
		else if(kind == 1)
			coolDownZumbiKind1 = defaultCoolDownZumbiKind1;
		else if(kind == 2)
			coolDownZumbiKind2 = defaultCoolDownZumbiKind2;
		
		spriteEnabled = this.gameObject.GetComponent<SpriteRenderer> ().sprite;
	}
	
	// Update is called once per frame
	void Update () {
		
		if(kind == 0){
			
			if(DragDropedKind0){
				
				if(coolDownZumbiKind0>0){
					
					coolDownZumbiKind0 -=0.1f * Time.deltaTime;
					print("coolDownZombie "+coolDownZumbiKind0);
					
					if(this.gameObject.GetComponent<SpriteRenderer> ().sprite != spriteDisable)
						this.gameObject.GetComponent<SpriteRenderer> ().sprite = spriteDisable;
					
				}else{
					coolDownZumbiKind0 = defaultCoolDownZumbiKind0;
					DragDropedKind0 = false;
				}
				
				
			}else if(this.gameObject.GetComponent<SpriteRenderer> ().sprite != spriteEnabled){
				this.gameObject.GetComponent<SpriteRenderer> ().sprite = spriteEnabled;
			}
			
		}else if(kind == 1){

			if(DragDropedKind1){

				if(coolDownZumbiKind1>0){
					print("in");
					coolDownZumbiKind1 -=0.1f * Time.deltaTime;
					print("coolDownZombie "+coolDownZumbiKind1);

					if(this.gameObject.GetComponent<SpriteRenderer> ().sprite != spriteDisable)
						this.gameObject.GetComponent<SpriteRenderer> ().sprite = spriteDisable;
					
				}else{
					coolDownZumbiKind1 = defaultCoolDownZumbiKind1;
					DragDropedKind1 = false;
				}
				
				
			}else if(this.gameObject.GetComponent<SpriteRenderer> ().sprite != spriteEnabled){
				this.gameObject.GetComponent<SpriteRenderer> ().sprite = spriteEnabled;
			}
		}else if(kind == 2){

			if(DragDropedKind2){
				
				if(coolDownZumbiKind2>0){
					
					coolDownZumbiKind2 -=0.1f * Time.deltaTime;
					print("coolDownZombie "+coolDownZumbiKind2);
					
					if(this.gameObject.GetComponent<SpriteRenderer> ().sprite != spriteDisable)
						this.gameObject.GetComponent<SpriteRenderer> ().sprite = spriteDisable;
					
				}else{
					coolDownZumbiKind2 = defaultCoolDownZumbiKind2;
					DragDropedKind2 = false;
				}
				
				
			}else if(this.gameObject.GetComponent<SpriteRenderer> ().sprite != spriteEnabled){
				this.gameObject.GetComponent<SpriteRenderer> ().sprite = spriteEnabled;
			}
			
		}
		
	}
	
	void OnMouseDrag(){
		/* V5 */
		if(kind == 0 && !DragDropedKind0){
			//	movimentando = false;
			//print(Camera.main.ScreenToWorldPoint(Input.mousePosition));
			Vector3 vet = Camera.main.ScreenToWorldPoint(Input.mousePosition);
			float x = vet.x;
			float y = vet.y;
			transform.position = new Vector3(x,y,0);
			
		}else if(kind == 1 && !DragDropedKind1){
			//	movimentando = false;
			//print(Camera.main.ScreenToWorldPoint(Input.mousePosition));
			Vector3 vet = Camera.main.ScreenToWorldPoint(Input.mousePosition);
			float x = vet.x;
			float y = vet.y;
			transform.position = new Vector3(x,y,0);
			
		}else if(kind == 2 && !DragDropedKind2){
			//	movimentando = false;
			//print(Camera.main.ScreenToWorldPoint(Input.mousePosition));
			Vector3 vet = Camera.main.ScreenToWorldPoint(Input.mousePosition);
			float x = vet.x;
			float y = vet.y;
			transform.position = new Vector3(x,y,0);
			
		}
		
		
	}
	
	void OnMouseUp(){

			
		if(colidiu && Vector3.Distance(this.transform.position,aux)<distanciaMinima){	

			ZombieManager ZManager = GameObject.Find ("ZombieManager").GetComponent<ZombieManager> ();

			/*v5*/
			if(kind == 0){
				DragDropedKind0 = true;	
				ZManager.removeCountZombies(0);
			}else if(kind == 1){
				DragDropedKind1 = true;
				ZManager.removeCountZombies(1);
			}else if(kind == 2){
				DragDropedKind2 = true;
				ZManager.removeCountZombies(2);
			}
			
			Instantiate(zombie,aux,Quaternion.identity);
			Destroy(this.gameObject);
			
		}
		else{
			print("largou no lugar errado");
			transform.position = origem;
		}
		
	}
	
	
	
	void  OnTriggerEnter2D(Collider2D collider) {
		if (collider.tag == "posInicial") {
			colidiu = true;
			aux = collider.transform.position;
			print("colidiu");
		}
	}
	
	
}
