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

public class ManagerUI : MonoBehaviour {

    // Attributes.
    private PlayerStats stats;
    private Player player;
    private GameObject inventory;
    
    // Menu labels and company...
    private GameObject GUI;
    private GameObject health;
    private GameObject level;
    private GameObject exp;
    private GameObject currentW;
    private GameObject currentA;
    private GameObject currentG;
    private GameObject currentB;
    private GameObject currentH;

    // Menu manage.
    private bool inventory_enabled = false;
    private Text statsInventory;
    private GameObject itemsInventory;

    private bool menu_enabled = false;
    private GameObject menuInventory;

    private string s_health = "Health: ";
    private string s_exp = "XP: ";
    private string s_level = "Level: ";

    private List<GameObject> items;
    public bool reload = false;

    private string menu = "startingMenuScene";

    private List<Button> buttons;

    // Getting all the references of the menu labels...
    void Awake() {
        this.GUI = GameObject.Find("GUI");
        this.health = GameObject.Find("Health");
        this.level = GameObject.Find("Level");
        this.exp = GameObject.Find("XP");
        this.items = new List<GameObject>();
        this.currentW = GameObject.Find("CurrentWeaponL");
        this.currentA = GameObject.Find("CurrentArmorL");
        this.currentG = GameObject.Find("CurrentGlovesL");
        this.currentB = GameObject.Find("CurrentBootsL");
        this.currentH = GameObject.Find("CurrentHelmL");
        this.inventory = GameObject.Find("Inventory");
        this.statsInventory = GameObject.Find("Stats").GetComponent<Text>();
        this.itemsInventory = GameObject.Find("Items").gameObject;
        this.menuInventory = GameObject.Find("Options").gameObject;

        this.inventory_enabled = false;
        this.inventory.SetActive(this.inventory_enabled);

        this.menu_enabled = false;
        this.menuInventory.SetActive(this.menu_enabled);        
    }

    // Getting the references of the PlayerStats.
	void Start () {
        this.stats = GameObject.Find("LoadPlayer").GetComponent<LoadPlayer>().stats;
        this.player = GameObject.Find("LoadPlayer").GetComponent<LoadPlayer>().player;
    }

    // Listener to know when the inventory button is clicked.
    public void clickInventory() {
        if (!this.inventory_enabled) loadButtons();
        this.inventory.SetActive(!this.inventory_enabled);
        this.inventory_enabled = !this.inventory_enabled;
    }

    // Closes the inventory.
    public void closeInventory() {
        this.inventory.SetActive(false);
        this.inventory_enabled = false;
    }

    // Listener to know when the menu button is clicked.
    public void clickMenu() {        
        this.menuInventory.SetActive(!this.menu_enabled);
        this.menu_enabled = !this.menu_enabled;
    }
    
    // Closes the menu.
    public void closeMenu() {
        this.menuInventory.SetActive(false);
        this.menu_enabled = false;    
    }

    // Load the inventory of the player in button sytle.
    public void loadButtons() {
        // Refresh!
        emptyInventoryMenu();  
 
        // For every item of the inventory...
        // Instantiate a prefab and setups its text and function.
        foreach (Item i in stats.getInventory().getItems()) {

            GameObject b = (GameObject)Instantiate(Resources.Load("GUI/ItemInventory"), Vector3.zero, Quaternion.identity);
            RectTransform r = b.GetComponent<RectTransform>();
            r.SetParent(itemsInventory.transform);
            r.localScale = (Vector3.one);
            b.transform.Find("Text").GetComponent<Text>().text = i.name;

            // Listener. Change of equipment. The value is copied 'cause scope nasty things.
            int value = i.id;
            b.GetComponent<Button>().onClick.AddListener(() => {
                    this.player.equipItem(value);
                }); 
        }

        // Refresh needed!
        reload = true;
    }

    // Remove old buttons.
    public void emptyInventoryMenu() {
        foreach (Transform child in itemsInventory.transform) Destroy(child.gameObject);
    }


    // Save game in Slot1.
    // Later I discovered a function in the Inpsector than make this more... elegant. Zorry.
    public void InGamesaveGame1() {
        InGamesaveGame(0);
    }

    // Save game in Slot2.
    // Later I discovered a function in the Inpsector than make this more... elegant. Zorry.
    public void InGamesaveGame2() {
        InGamesaveGame(1);
    }

    // Save game in Slot3.
    // Later I discovered a function in the Inpsector than make this more... elegant. Zorry.
    public void InGamesaveGame3() {
        InGamesaveGame(2);
    }

    // Save game in the selected slot.
    public void InGamesaveGame(int pos) {
        LoadSavePlayerStats.Save(this.stats, pos);
    }

    // Exit game.
    public void InGameReturnToMenu() {
        Application.LoadLevel(menu);
    }

	// Update is called once per frame
	void Update () {
        // Escape activates the Options menu.
        if (Input.GetKeyDown(KeyCode.Escape)) {
            this.clickMenu();
        }

        // Updating life and company.
        this.level.GetComponent<Text>().text = this.s_level + this.stats.getLevel();
        this.health.GetComponent<Text>().text = this.s_health + this.stats.getHealth();
        this.exp.GetComponent<Text>().text = this.s_exp + this.stats.getExp();


        // If the inventory menu is enabled, update the info of the player.
        if (!this.menu_enabled && this.inventory_enabled) {
            
            this.statsInventory.text = "Stats" +
                "\nLevel: " + this.stats.getLevel() +
                "\nHealth: " + this.stats.getHealth() + " / " + this.stats.getMaxHealth() +
                "\nMagic: " + this.stats.getMagic() + " / " + this.stats.getMaxMagic() +
                "\nExperience: " + this.stats.getExp() +
                "\nLevel up: ";

            // Updates all the equipment slots.
            Item i = null;
            string auxtxt = "";
            
            i = stats.getInventory().weaponE;
            auxtxt = (i != null) ? i.name : "None";
            this.currentW.GetComponent<Text>().text = auxtxt;
            
            i = stats.getInventory().armorE;
            auxtxt = (i != null) ? i.name : "None";
            this.currentA.GetComponent<Text>().text = auxtxt;
            
            i = stats.getInventory().bootsE;
            auxtxt = (i != null) ? i.name : "None";
            this.currentB.GetComponent<Text>().text = auxtxt;
            
            i = stats.getInventory().glovesE;
            auxtxt = (i != null) ? i.name : "None";
            this.currentG.GetComponent<Text>().text = auxtxt;
            
            i = stats.getInventory().helmetE;
            auxtxt = (i != null) ? i.name : "None";
            this.currentH.GetComponent<Text>().text = auxtxt;
        }        
	}
}
