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

public class ItemInventoryPanel : MonoBehaviour
{

	public bool craftItems = false;
	public List<InventorySlot> slots;
	public GridLayoutGroup slotGrid;

	
	public void Sync ()
	{
		List<Item> items = new List<Item>();
		foreach (var i in GameManager.instance.player.inventory.InventoryItems)
		{
			if (craftItems)
			{
				if (i.IsCraftable)
				{
					items.Add(i);
				}
			}
			else
			{
				if (!i.IsCraftable)
				{
					items.Add(i);
				}
			}
		}
		for(int i = 0; i < 8; i++)
		{
			InventorySlot slot = Instantiate(GameManager.instance.inventorySlot);
			slot.transform.parent = slotGrid.transform;
			slot.transform.localScale = Vector3.one;
			slot.Sync();

			if (items.Count - 1 >= i)
			{
				slot.Sync(items[i]);
			}
			slots.Add(slot);
		}
		
		
		
	}
	
	
	void Update () {
		
	}

	public void Dispose()
	{
		foreach (var s in slots)
		{
			if (s != null)
			{
				Destroy(s);
			}
		}
		slots = null;
	}
}
