using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

[System.Serializable]
public class Shop
{
    [NonSerialized]
    public string Npc;

    [NonSerialized]
    public string Map;

    [SerializeField]
    public List<IndexValuePair<int, EquippableItem>> Content = new List<IndexValuePair<int, EquippableItem>>();

    public Shop(string _map)
    {
        Map = _map;
    }

    public Shop(string _map, string _npc)
    {
        Map = _map;
        Npc = _npc;
    }

    public Shop(string _map, string _npc, List<IndexValuePair<int, EquippableItem>> _content)
    {
        Map = _map;
        Npc = _npc;
        Content = _content;
    }

    public void SetContent(List<IndexValuePair<int, EquippableItem>> _content)
    {
        Content = _content;
    }

    public List<IndexValuePair<int, EquippableItem>> GetContent()
    {
        return Content;
    }

    #region item
    public void AddItemToShop(Item _item)
    {
        int firstEmptyPosition = FindFirstEmptyPosition();

        Content.Add(new IndexValuePair<int, EquippableItem>(firstEmptyPosition, (EquippableItem)_item));
    }

    public void AddItemToShopOnPosition(int _position, Item _item)
    {
        Content.Add(new IndexValuePair<int, EquippableItem>(_position, (EquippableItem)_item));
    }
    #endregion

    #region equippable item
    public void AddItemToShop(EquippableItem _item)
    {
        int firstEmptyPosition = FindFirstEmptyPosition();

        Content.Add(new IndexValuePair<int, EquippableItem>(firstEmptyPosition, _item));
    }

    public void AddItemToShopOnPosition(int _position, EquippableItem _item)
    {
        Content.Add(new IndexValuePair<int, EquippableItem>(_position, _item));
    }
    #endregion

    public int FindFirstEmptyPosition()
    {
        if (Content.Any())
        {
            for (int i = 0; i < Content.Count(); i++)
            {
                if (Content.Count(el => el.Key == i && el.Value != null) != 0)
                    continue;

                return i;
            }
        }

        return 0;
    }
}