using UnityEngine;
using System.Collections.Generic;

[System.Serializable]
public class Inventory {

    private List<Item> items;
    private List<int> indexTmp;

    // Slots of equipment used. Null = nothing equiped.
    public Item weaponE = null;
    public Item glovesE = null;
    public Item helmetE = null;
    public Item bootsE = null;
    public Item armorE = null;

    private string weaponSlotString = "Armature|arm_right_4"; 

    // Constructor. Create a empty list of items that later will be created in fixInventory().
    public Inventory(List<int> ids) {
        this.items = new List<Item>();
        this.indexTmp = ids;
    }

    // Auxiliar method that creates the items that the player has in the inventory.
    public void fixInventory() {
        foreach (int i in indexTmp) this.items.Add(ItemDictionary.items[i]);
        //indexTmp = null;
    }

    // Returns an item from an id. This is not from the player inventory. 
    public Item getItem(int id) { return ItemDictionary.items[id]; }

    // Add an item from an id to the player inventory.
    public void addItem(int id) { this.addItem(ItemDictionary.items[id]); }

    // Add an item to the player inventory.
    public void addItem(Item i) { this.items.Add(i); }

    // Checks if player has some item from its id.
    public bool containsItem(int i) {
        foreach (Item it in this.items) {
            if (it.id == i) return true;
        }
        return false;
    }

    // Getter.
    public List<Item> getItems() { return this.items; }

    // ToString.
    public override string ToString() {
        string output = "";
        foreach (Item i in this.items) output += i.ToString() + "\n";

        return output;
    }

    // Equip a weapon. Find the point in the armature that will be the parent of the
    // GameObject, creates the weapon collider and set the values to it from the true
    // values of the weapon.
    public void setWeapon(Weapon id, GameObject weapon) {

        // Finding the armature point and setting it as parent.
        GameObject weaponSlot = GameObject.Find(weaponSlotString);
        weapon.name = "Weapon";
        weapon.transform.parent = weaponSlot.transform;
        weapon.transform.localPosition = new Vector3(0, 0, 0);
        weapon.transform.localRotation = weaponSlot.transform.rotation;

        // Creating colliderScript and updating its values.
        WeaponCollider weaponCollider = weapon.AddComponent<WeaponCollider>();
        weaponCollider.setAttacks(id.attack1, id.attack2);
        weaponCollider.updateStats();

        // Updating slot.
        this.weaponE = id;
    }

    // Equip an Armor.
    public void setArmor(Armor id) {
        this.armorE = id;
    }

    // Equip boots.
    public void setBoots(Armor id) {
        this.bootsE = id;
    }
    
    // Equip gloves.
    public void setGloves(Armor id) {
        this.glovesE = id;
    }
    
    // Equip helmet.
    public void setHelmet(Armor id) {
        this.helmetE = id;
    }
}
