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

public class InventoryUI : MonoBehaviour
{

    public Inventory inventory;

    [SerializeField]
    private BagItem ItemPrefab;

    [SerializeField]
    private Transform Parent;

    public List<BagItem> content { get; private set; }


    private void Start()
    {
        UpdateUI();
    }

    private void OnEnable()
    {
        inventory.OnItemsUpdated += UpdateUI;
    }

    private void OnDisable()
    {
        inventory.OnItemsUpdated -= UpdateUI;
    }

    [ContextMenu("Update UI")]
    public void UpdateUI()
    {
        foreach(Transform child in Parent)
            Destroy(child.gameObject);

        content = new List<BagItem>();

        foreach (Inventory.Data data in inventory.BagItems)
        {
            BagItem item = Instantiate(ItemPrefab.gameObject, Parent).GetComponent<BagItem>();
            item.Initialise(data);
            content.Add(item);
        }
    }


}
