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

[CreateAssetMenu(menuName = "Major Project/Inventory", order = 150)]
public class Inventory : ScriptableObject
{

    public List<Data> BagItems { get { if (_bagItems == null) _bagItems = new List<Data>(); return _bagItems; } }

    [SerializeField]
    protected List<Data> _bagItems = new List<Data>();

    public void Add(LogicBlock element,int count = 1, bool isInfinit = false)
    {

        Data data = new Data {
            element = element,
            Count = count,
            isInfinit = isInfinit };

        Add(data);    
    }

    public void Add(Data data)
    {

        if (_bagItems.Contains(data))
        {
            Data bagData = BagItems.Find(p => p.Equals(data));

            bagData.Count += data.Count;
            bagData.isInfinit = (data.isInfinit || bagData.isInfinit);
        }
        else
            _bagItems.Add(data);
    }

    public void Remove(LogicBlock element) {

        if (!_bagItems.Exists(p => p.element == element))
            return;

        Data data = _bagItems.First(p => p.element == element);

        if (!data.isInfinit)
            data.Count--;
    }

    public void Clone(Inventory inventory)
    {

        _bagItems = new List<Data>(inventory._bagItems);

    }



    [System.Serializable]
    public class Data
    {
        public LogicBlock element;
        public int Count;
        public bool isInfinit = false;
    }
}
