﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class InventoryManager : MonoBehaviour, IGameManager {

    // A dictionary is a key-value pair collection
    // It will be used to store the items collected
    // The key (string) will be the name of the item
    // The value (int) will be the number of items in inventory
    private Dictionary<string, int> items;

    // Current item player is equipped with. Can only be set within this class
    public string equippedItem { get; private set; }

    public ManagerStatus status
    {
        get; private set;
    }

    public void StartUp()
    {
        status = ManagerStatus.Started;
        items = new Dictionary<string, int>();
    }

    // Use this for initialization
    void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		
	}

    // Adds a new item of the specified name and updates the inventory
    public void AddItem(string name)
    {
        // First check if item already exist in inventory
        if (items.ContainsKey(name))
        {
            // Yes, item already exists - increase value by one
            items[name] += 1;
        }
        else
        {
            // No, item does not exist - add and set value to 1
            items.Add(name, 1);
        }
    }

    // Consumes (uses) a specified item and adjusts the inventory for that item
    public bool ConsumeItem(string name)
    {
        // First check if item to consume exist in inventory
        if (items.ContainsKey(name))
        {
            // Yes, item exists - decrease value by one
            items[name]--;
            
            // Check if item count is now zero and if yes, remove from inventory dictionary
            if (items[name] == 0)
            {
                items.Remove(name);
            }

            // Item successfully consumed
            return true;
        }
        else
        {
            Debug.Log("Cannot consume " + name);
            return false;
        }
    }

    // Returns a list of all item types stored in the inventory
    public List<string> GetItemList()
    {
        // Retrieve the item keys as a list of strings
        List<string> list = new List<string>(items.Keys);
        return list;
    }

    // Returns the quantity of a particular item type.
    public int GetItemCount(string name)
    {
        if (items.ContainsKey(name))
        {
            // Return the value of the specified item using its key (name)
            return items[name];
        }
        return 0;
    }

    // Equips/unequips the player with the specified item
    public bool EquipItem(string name)
    {
        if (items.ContainsKey(name) && equippedItem != name)
        {
            equippedItem = name;
            Debug.Log("Equipped " + name);
            return true;
        }

        equippedItem = null;
        Debug.Log("Unequipped");
        return false;
    }

}
