using UnityEngine;
using System.Collections;

public class Operario : Inventario {
	GameObject colidindo;
	Maquina consertando;
	public Texture retrato;
	public Texture pa, solda;
	private Texture display;
	public Texture[] vidas;
	public int hp = 3;
	public int maxHp = 3;
	// Use this for initialization
	void Start () {
		this.adicionaOpcao ("Consertar");
		this.adicionaOpcao ("Pa");
		
		this.setInformative (this.gameObject);
		display = pa;
	}

	public override void Acao() {

		string s = this.getAtual ();
		switch (s) {
		case "Pa":
			Debug.Log("Açao da Pa");
			if(colidindo != null && colidindo.tag == "terra") {
				colidindo.SendMessage("Pa");
			}
			break;
		case "Consertar":

			if(colidindo != null && colidindo.tag == "maquina") {
				Debug.Log("Consertando");
				InvokeRepeating("Conserto",1,.07f);
				consertando = colidindo.GetComponent<Maquina>();
				consertando.Inicia();
			}
			break;
		default:
			break;
		}
	}

	void OnCollisionEnter(Collision c) {
		base.OnCollisionEnter (c);
		colidindo = c.gameObject;
		if (c.gameObject.tag == "inimigo") {
			this.hp--;
			if(hp < 0) this.gameObject.SetActive(false);
		}
	}

	void OnCollisionExit(Collision c) {
		if(c.gameObject.tag == "maquina")
			Invoke ("Limpa", 3f);
	}

	void Limpa() {
		colidindo = null;
		if(consertando != null)
		consertando.para();
		CancelInvoke("Conserto");
	}

	void Conserto() {
		if (consertando.consertaUnidade ()) {
			CancelInvoke("Conserto");
		}
	}

	void Vodka() {
		hp += (2 * maxHp) / 3;
		if (hp > maxHp)
						hp = maxHp;
	}

	void Inventario(string s) {
		if (s == "Pa")
			display = pa;
		else
			display = solda;
		Debug.Log (s);
	}
	
	void OnGUI() {
		GUI.Label (new Rect (10, 120+10, 50, 50), retrato);
		GUI.Label (new Rect (19, 120 + 55, 30, 19), display);
		
		GUI.Label (new Rect (16, 120+46, 32, 70), vidas [hp]);
	}
}
