using UnityEngine;
using System.Collections;

public class DragPowerUp : MonoBehaviour
{
	
	private Vector3 aux;
	private bool colidiu = false;
	public float distanciaMinima;
	public Transform bain;
	private Vector2 origem;

	/* v5 */
	public float defaultCoolDownZumbi = 2;
	private static float coolDownZumbi;
	private static bool DragDroped = false;
	private Sprite spriteEnabled;
	public Sprite spriteDisable;

	
	// Use this for initialization
	void Start () {
		origem = new Vector3 (transform.position.x,transform.position.y);

		coolDownZumbi = defaultCoolDownZumbi;
		spriteEnabled = this.gameObject.GetComponent<SpriteRenderer> ().sprite;
	}
	
	// Update is called once per frame
	void Update () {
		
		
		if (DragDroped) {
			
			if(coolDownZumbi>0){
				
				coolDownZumbi -=0.1f * Time.deltaTime;
				print("coolDownDrag "+coolDownZumbi);
				
				if(this.gameObject.GetComponent<SpriteRenderer> ().sprite != spriteDisable)
					this.gameObject.GetComponent<SpriteRenderer> ().sprite = spriteDisable;
				
			}else{
				coolDownZumbi = defaultCoolDownZumbi;
				DragDroped = false;
			}
			
		}else if(this.gameObject.GetComponent<SpriteRenderer> ().sprite != spriteEnabled){
			this.gameObject.GetComponent<SpriteRenderer> ().sprite = spriteEnabled;
		}
	}
	
	void OnMouseDrag(){
		
		if(!DragDroped){
			//	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);
		
		
			/**  V5 **/
			//muda a cor do caminho, indicando o local de input
			GameObject[] ArrWays = GameObject.FindGameObjectsWithTag ("way");
			
			foreach (GameObject gbWay in ArrWays) {
				gbWay.GetComponent<SpriteRenderer>().color = new Color(255, 255, 0);
			}
		
		}
		
	}
	
	void OnMouseUp(){
		
		if(colidiu && Vector3.Distance(this.transform.position,aux)<distanciaMinima){
			/*v5*/
			DragDroped = true;

			Instantiate(bain,aux,Quaternion.identity);
			Destroy(this.gameObject);
		}
		else{
			print("largou no lugar errado");
			transform.position = origem;
		}

		/**  V5 **/
		
		GameObject[] ArrWays = GameObject.FindGameObjectsWithTag ("way");
		
		foreach (GameObject gbWay in ArrWays) {
			gbWay.GetComponent<SpriteRenderer>().color = new Color(255, 255, 255);
		}
		
	}
	
	
	
	void  OnTriggerEnter2D(Collider2D collider) {
		if (collider.tag == "way") {
			colidiu = true;
			aux = collider.transform.position;
			print("colidiu");
		}
	}
}

