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

public class InventoryButtonBehavior : MonoBehaviour
{

    public GameObject Inventory;
    public GameObject InventoryIcon;
    public GameObject Level;

    void Start()
    {
    }


    void Update()
    {
        var inventory = Inventory.GetComponent<Inventory>();
        transform.Find("Inventory Button Text").GetComponent<TextMeshProUGUI>().text = inventory.ItemCounts[inventory.CurrentItemID].ToString(CultureInfo.InvariantCulture);
    }

    public void Prev()
    {
        Apply(-1);
    }

    public void Next()
    {
        Apply(1);
    }

    private void Apply(int offset)
    {
        var inventory = Inventory.GetComponent<Inventory>();
        // navigate
        var itemID = inventory.CurrentItemID;
        if (offset > 0)
        {
            for (var i = 0; i < Global.ITEM_COUNT; i++)
            {
                itemID++;
                if (itemID >= Global.ITEM_COUNT)
                    itemID = 0;
                if (inventory.ItemCounts[itemID] > 0)
                    break;
            }
        }
        else if (offset < 0)
        {
            for (var i = 0; i < Global.ITEM_COUNT; i++)
            {
                itemID--;
                if (itemID < 0)
                    itemID = Global.ITEM_COUNT - 1;
                if (inventory.ItemCounts[itemID] > 0)
                    break;
            }
        }
        // apply
        inventory.CurrentItemID = itemID;
        transform.Find("Inventory Button Text").GetComponent<TextMeshProUGUI>().text = inventory.ItemCounts[itemID].ToString(CultureInfo.InvariantCulture);
        InventoryIcon.GetComponent<Image>().sprite = Level.GetComponent<Level>().ItemTextures[itemID];
    }

}
