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

public class Player : MonoBehaviour {

    // Attributes
    private Rigidbody body;
    private float speed;
    private float drag;

    //Scripts
    public CameraMovement cameraScript;
    public PlayerMovement playerMovement;
    public PlayerStats playerStats;
    public ManagerUI ui;

    private GameObject go;

    // Start. 
    void Start() {
        this.ui = GameObject.Find("GUI").GetComponent<ManagerUI>();
        this.playerStats.getInventory().fixInventory();
    }

    // Setter.
    public void setPlayerStats(PlayerStats p) {
        this.playerStats = p;
    }

    // Setter.
    public void setBody(Rigidbody r) {
        this.body = r;
    }

    // Stops the player velocity. Needed to cross doors/scenes.
    public void stopMovement() {
        this.body.velocity = Vector3.zero;
        this.body.angularVelocity = Vector3.zero;
    }

    // Receives damage from something. 
    // It pass the values to playerStats, who manages all about this kind of things.
    public void receiveDamage(int damage, Vector3 direction) {
        this.playerStats.receiveDamage(damage);

        if (this.playerStats.isDead()) {
            Application.LoadLevel("deadMenu");

        }
    }

    // Checks if the player contains an item.
    // It pass the values to playerStats, who manages all about this kind of things.
    public bool containsItem(int id)
    {
        return this.playerStats.containsItem(id);
    }

    // Adds an item to the player's inventory.
    // It pass the values to playerStats, who manages all about this kind of things.
    public void receiveItem(int id) {
        this.playerStats.addItem(id);
        if (this.ui != null) this.ui.loadButtons();
    }

    // Add experience to the player.
    // It pass the values to playerStats, who manages all about this kind of things.
    public void setExperience(int exp) {
        this.playerStats.addExperience(exp);
    }

    // Method that manages the change of equipment.
    public void equipItem(int id) {
        //Debug.Log("Changing weapon: " + id);
        if ( containsItem(id) ) {
            Item i = (Item)ItemDictionary.getItemById(id);

            if (i.type == 1) {
                this.setWeapon((Weapon)(i));
            } else if (i.type == 3) {
                this.setArmor((Armor)(i));
            } else if (i.type == 2) {
                this.useItem(i);
            }
        }
    }

    // Equips a weapon and show it ingame.
    // This is something that must not be done here, but the playerStats and the other
    // GO that manages this kind of things are Serializable to make possible the Save/Load
    // system, and the GameObject class is not Serializable, so...
    // Bad design, maybe.
    private void setWeapon(Weapon w) {
        Debug.Log("Destroying previous weapon...");
        foreach (Transform child in GameObject.Find("Armature|arm_right_4").transform) Destroy(child.gameObject);
        GameObject weapon = (GameObject)Instantiate(Resources.Load(w.pathToPrefab), Vector3.zero, Quaternion.identity);
        this.playerStats.getInventory().setWeapon(w, weapon);
    }

    // Same as the setWeapon method, but armors doesn't show ingame, so there's no problem with the 
    // GameObjects. It pass the value to playerStats, who manages all about this kind of things.
    private void setArmor(Armor a) {
        this.playerStats.getInventory().setArmor(a);
    }

    // Same as the setWeapon method, but armors doesn't show ingame, so there's no problem with the 
    // GameObjects. It pass the value to playerStats, who manages all about this kind of things.
    private void setHelmet(Armor a) {
        this.playerStats.getInventory().setHelmet(a);
    }

    // Same as the setWeapon method, but armors doesn't show ingame, so there's no problem with the 
    // GameObjects. It pass the value to playerStats, who manages all about this kind of things.
    private void setBoots(Armor a) {
        this.playerStats.getInventory().setBoots(a);
    }

    // Same as the setWeapon method, but armors doesn't show ingame, so there's no problem with the 
    // GameObjects. It pass the value to playerStats, who manages all about this kind of things.
    private void setGloves(Armor a) {
        this.playerStats.getInventory().setGloves(a);
    }

    // Method not implemented.
    // Should use an item after check if the player has it.
    private void useItem(Item i) {
        //TODO  
    }
}