using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using TMPro;

public class BagItem : LogicElementHolder
{
    #region Inspector Elements
    [SerializeField]
    private LogicElementUI LogicElementPrefab;

    [SerializeField]
    private Transform LogicElementLocation;
    #endregion Inspector Elements

    #region Private Variables
    private int Count;
    protected Inventory.Data InventoryData;
    private TextMeshProUGUI CountText;
    #endregion Private Variables

    #region Class Functionality

    public void Initialise(Inventory.Data data)
    {
        InventoryData = data;

        if (CountText == null)
            CountText = GetComponentInChildren<TextMeshProUGUI>();
        Count = data.Count;


        LogicElementUI logicElement = Instantiate(LogicElementPrefab.gameObject, LogicElementLocation).GetComponent<LogicElementUI>();
        logicElement.Initialise(data.element, this);

        UpdateUI();
    }

    public void UpdateUI()
    {
        if (!InventoryData.isInfinit)
            CountText.text = Count.ToString();
        else
            CountText.text = "∞";
    }

    #endregion Class Functionality


    #region LogicElementHolder Implementation
    /// <summary>
    /// When a logicElement needs to be added back to this holder
    /// </summary>
    /// <param name="element">Element which is being Added</param>
    public override void OnAdd(LogicElementUI element)
    {
        Destroy(element.gameObject);

        if (!InventoryData.isInfinit)
            Count++;

        UpdateUI();
    }

    /// <summary>
    /// When a LogicElement has started being dragged from this holder
    /// </summary>
    /// <param name="element">Element which is being dragged</param>
    public override void OnRemove(LogicElementUI element)
    {
        if (!InventoryData.isInfinit && Count > 0)
            Count--;
        else if (!InventoryData.isInfinit)
            Destroy(element.gameObject);

        LogicElementUI logicElement = Instantiate(LogicElementPrefab.gameObject, LogicElementLocation).GetComponent<LogicElementUI>();
        logicElement.Initialise(InventoryData.element, this);


        UpdateUI();

    }
    #endregion LogicElementHolder Implementation

}
