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

[System.Serializable]
public class InvenParent
{
    public List<InvenSlot> slotList = new List<InvenSlot>();

    public InvenSlot AddEmpty()
    {
        InvenSlot newSlot = new InvenSlot();
        slotList.Add(newSlot);
        return newSlot;
    }
    public void AddItem(ItemInfo itemInfo)
    {
        bool check(InvenSlot slot)
        {
            if (slot != null && slot.itemInfo == null)
            {
                return true;
            }
            return false;
        }
        InvenSlot findSlot = Find(check);
        if (findSlot == null)
        {
            findSlot = AddEmpty();
        }
        findSlot.itemInfo = itemInfo;
    }
    public void RemoveItem(ItemInfo itemInfo)
    {
        bool check(InvenSlot slot)
        {
            if (slot != null && slot.itemInfo == itemInfo)
            {
                return true;
            }
            return false;
        }
        InvenSlot findSlot = Find(check);
        if (findSlot != null)
        {
            findSlot.itemInfo = null;
        }
    }
    public void RemoveEmpty()
    {
        slotList.RemoveAll(x => x == null || x.itemInfo == null);
    }
    public InvenSlot Find(Func<InvenSlot, bool> check)
    {
        for (int i = 0; i < slotList.Count; i++)
        {
            if (check(slotList[i]))
            {
                return slotList[i];
            }
        }
        return null;
    }
}
