using UnityEngine;
using System.Collections;

public class GameInterface : MonoBehaviour {
	
	static GameInterface instance;
	
	Button inventoryButton;
	
	public Level level;
	public Hero hero;
	Inventory inventory;
	MonotypeText life;
	
	void CreateInterfaceButton(){
		Vector2 size = Control.worldSpace;
		size.x = Mathf.Floor(size.x / 0.1f) * 0.1f;
		size.y = Mathf.Floor(size.y / 0.1f) * 0.1f;
		inventoryButton = Utils.Create<Button>(transform, new Vector3(-size.x + 0.8f, size.y - 0.8f , -2));
		
		life = Utils.Create<MonotypeText>(transform, new Vector3(size.x - 0.8f, size.y - 0.8f, -2));
		life.SetMaterial(Resources.Load("Material/Font") as Material);
		life.text = "100";
		life.SetPivot(1, 0);
		inventoryButton.SetSize(Vector3.one * Tile.TILE_SIZE);
		inventoryButton.activeSprite = Tile.CreateChar(inventoryButton.transform, new Coordinate(31,0), Vector3.zero);
		inventoryButton.backSprite   = Tile.CreateChar(inventoryButton.transform, new Coordinate(31,0), Vector3.zero);
		inventoryButton.backSprite.visible = false;
		inventoryButton.SetReceiver( delegate() {
			if (inventory != null) {
				inventory.Show(!inventory.visible);
			}
		});
	}

	void Start () {
		instance = this;
		inventory = Utils.Create<Inventory>(transform);
		inventory.Show(false);
		CreateInterfaceButton();
	}
	
	public static void UpdateLife(int life){
		if (instance != null) {
			instance.life.text = life.ToString();
		}
	}
	
	void Update () {	
		if (Control.activeTouchCount > 0 && !inventory.visible && !inventoryButton.hover){
			if (Control.activeTouches[0].state == TouchState.Touch && level != null && hero != null) {
				Coordinate cursor = level.GetCursorPosition(Control.activeTouches[0].position);
				if (level.InLevelRect(cursor.x, cursor.y)) {
					hero.MoveToPath(level.FindPath(hero.position, cursor));
					level.MakeTurn();
				}
			}
		}
	}
}
