using UnityEngine;
using UnityEngine.UI;
using System.Collections;

namespace UnityStandardAssets._2D
{
	public class VIda : MonoBehaviour {

		public Sprite inactivos;
		public Sprite activos;
		private Image[] iconos;  
		private RectTransform[] iconosRect; 
		//public int tipo;
		//public SelectorPersonajes p;
		int vidaMemoria = 4;
		private int cuenta = 5;
		public bool enable = false;
		// Use this for initialization
		void Start () {
			//inicializar ();
		}

		public void inicializar(){
			//p = GameObject.FindGameObjectWithTag("jugadorGGJ").GetComponent<SelectorPersonajes> ();
			//vidaMemoria = p.vida;
			//tipo = p.GetComponent<PlatformerCharacter2D>().tipo;
			iconos = GetComponentsInChildren<Image> ();
			iconosRect = GetComponentsInChildren<RectTransform> ();
			float divisor = 12;
			float h = Screen.width / divisor;
			float H = iconosRect[1].sizeDelta.y;
			float W = iconosRect[1].sizeDelta.x;
			Debug.Log (iconosRect [0].sizeDelta.y + " qwerqe " + iconosRect [0].sizeDelta.x);
			float w = (W*h)/H;
			for (int i = 0; i <cuenta; i++) {
				iconosRect[i+1].sizeDelta = new Vector2(w,h);
				iconosRect[i+1].anchoredPosition = new Vector2 (-iconosRect[i+1].sizeDelta.x/2-(Screen.width/divisor+10)*i,-iconosRect[i+1].sizeDelta.y/2-20);
				iconos[i].sprite = activos;
			}
		}


		/*public void inicializar2(){
			iconos = GetComponentsInChildren<Image> ();
			iconosRect = GetComponentsInChildren<RectTransform> ();
			for (int i = 0; i <cuenta; i++) {
				iconosRect[i+1].sizeDelta = new Vector2(Screen.width/20f,Screen.width/20f);
				iconosRect[i+1].anchoredPosition = new Vector2 (Screen.width/40f+20+(Screen.width/20f+10)*i,-20);
				iconos[i].sprite = activos[i];
			}
		}*/

		/*public void setSimbolo(int cual){
			iconos[cual].sprite = activos[cual];
			//if (tipo == 2) {
			Debug.Log ("estoy cambiendo el "+cual);
			Debug.Log ("tipo :"+tipo);
			//}
		}

		public void seleccionar(int cual){
			if (tipo == cual) {
				for (int i = 0; i < cuenta; i++) {
					iconos [i].enabled = true;
				}
				enable = true;
			} else {
				for (int i = 0; i < cuenta; i++) {
					iconos [i].enabled = false;
				}
				enable = false;
			}

		}*/
		// Update is called once per frame
		public void bajarVida () {
			iconos[vidaMemoria].sprite = inactivos;
			vidaMemoria--;
			if (vidaMemoria < 0) {
				vidaMemoria = 4;	
				for (int i = 0; i < cuenta; i++) {
					iconos [i].sprite = activos;
				}
			}
		}
	}
}
