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

public class ItemManager : MonoBehaviour
{
    [SerializeField]
    private GameObject[] m_itemBoxes;

    public Texture2D m_normalCursor;

    public Texture2D m_selectedCursor;


    public string _currentlyHolding = "";

    private int _currentItemBoxIndex = 0;

    public int ItemBoxIndex => _currentItemBoxIndex;

    public bool _hoveringOverItem = false;

    public bool inventoryFull = false;

    private GameObject hudManager;

    // In hingsight I don't know why this is here cause we never need the item itself as a reference
    // but it's nice to have I guess
    public GameObject _itemHoveredOver = null;

    private void Start()
    {
        hudManager = GameObject.FindGameObjectWithTag("HudManager");
    }


    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            DeselectItem();
        }
    }

    public void SelectItem(Button item)
    {
        Debug.Log(item.name);
        _currentlyHolding = item.name;
        hudManager.GetComponent<HUDManager>().DisplayText("Click what you want to use the " + item.name + " on.");

        foreach (GameObject i in m_itemBoxes)
        {
            if (i.name == _currentlyHolding)
            {
                i.GetComponent<Image>().color = Color.red;
            }
        }
    }

    public void DeselectItem()
    {
        foreach(GameObject i in m_itemBoxes)
        {
            if (i.name == _currentlyHolding)
            {
                i.GetComponent<Image>().color = Color.white;
            }
        }

        _currentlyHolding = "";
    }

    // Horrible nightmare system that under any other cicumstances
    // would've been done with a hash map, but this is a game jam
    // game so God will forgive this crime I hope
    public void PickUpItem(GameObject item)
    {
        foreach (GameObject i in m_itemBoxes)
        {
            i.GetComponent<Image>().color = Color.white;

            if (i.name == "None") 
            {
                i.SetActive(true);
                i.name = item.GetComponent<Item>().itemName;
                i.GetComponent<Image>().sprite = item.GetComponent<Item>().itemSprite;
                break;
            }

            if (i.name == "metal sheet" || i.name == "hot metal sheet")
            {
                return;
            }
        }

        foreach (GameObject i in m_itemBoxes)
        {
            if (i.name == "None")
            {
                return;
            }
        }

        inventoryFull = true;
        /*
        if (_currentItemBoxIndex >= 3) { return; }

        item.gameObject.SetActive(false);
        m_itemBoxes[_currentItemBoxIndex].SetActive(true);
        m_itemBoxes[_currentItemBoxIndex].name = item.GetComponent<Item>().itemName;
        m_itemBoxes[_currentItemBoxIndex].GetComponent<Image>().sprite = item.GetComponent<Item>().itemSprite;

        if (_currentItemBoxIndex + 1 <= 3)
        {
            _currentItemBoxIndex++;
        } */
    }

    public void RemoveItem(string item)
    {
        foreach (GameObject i in m_itemBoxes)
        {
            if (i.name == item)
            {
                i.name = "None";
                i.GetComponent<Image>().sprite = null;
                i.SetActive(false);
                break;
            }
        }

        inventoryFull = false;
    }

    public void ResetInventory()
    {
        foreach (GameObject i in m_itemBoxes)
        {
            i.name = "None";
            i.GetComponent<Image>().sprite = null;
        }
    }
}
