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

namespace Kryz.CharacterStats.Examples
{
    public class Inventory : MonoBehaviour
    {
        [SerializeField] List<Item> items;
        [SerializeField] Transform itemsParent;
        [SerializeField] ItemSlot[] itemSlots;

        public UnityEvent_Item OnItemRightClickedEvent = new UnityEvent_Item();

        private void Awake()
        {
            for (int i = 0; i < itemSlots.Length; i++)
            {
                itemSlots[i].OnRightClickEvent.AddListener((X) => OnItemRightClickedEvent.Invoke(X));
            }
        }

        private void Start()
        {
            RefreshUI();
        }

        private void OnValidate()
        {
            if (itemsParent != null)
            {
                itemSlots = itemsParent.GetComponentsInChildren<ItemSlot>();
                RefreshUI();

            }

        }

        private void RefreshUI()
        {
            int i = 0;
            for (; i < items.Count && i < itemSlots.Length; i++)
            {
                itemSlots[i].Item = items[i];
            }

            for (; i < itemSlots.Length; i++)
            {
                itemSlots[i].Item = null;
            }
        }

        public bool AddItem(Item item)
        {
            if (IsFull())
                return false;

            items.Add(item);
            RefreshUI();
            return true;
        }

        public bool RemoveItem(Item item)
        {
            if (items.Remove(item))
            {
                RefreshUI();
                return true;
            }
            return false;
        }

        public bool IsFull()
        {
            return items.Count >= itemSlots.Length;
        }
    }
}
