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

public class PlayerInventory : Inventory
{

	ItemInventoryPanel inventoryPanel;
	ItemInventoryPanel craftingPanel;

	public override void Start()
	{
        Player.LocatedNearAnItem += OnItemPickup;
		/*InventoryItems.Add(Instantiate(GameManager.instance.itemRegister[0]));
		InventoryItems.Add(Instantiate(GameManager.instance.itemRegister[1]));
		InventoryItems.Add(Instantiate(GameManager.instance.itemRegister[2]));*/
	}

    public void OnItemPickup(GameObject go)
    {
        InteractableItem iitem = go.GetComponent<InteractableItem>();
        if(iitem != null && iitem.CanPutInInventory)
        {
            AddItem(iitem.InventoryItem.Name);
            iitem.PickUpItem();
            if(!iitem.DontDestroyOnPickup)
            {
                Destroy(go);
            }
        }
    }

	public override void TriggerInventoryPanel(bool crafting)
	{

		if (crafting)
		{
			if(craftingPanel == null)
			{
				craftingPanel = Instantiate(GameManager.instance.inventoryPanel, GameManager.instance.canvas.transform);
				craftingPanel.craftItems = true;
				craftingPanel.Sync();
				if(inventoryPanel != null)
				{
					DisposeI();
				}
			}
			else
			{
				DisposeC();
			}
		}
		else
		{
			if(inventoryPanel == null)
			{
				inventoryPanel = Instantiate(GameManager.instance.inventoryPanel, GameManager.instance.canvas.transform);
				inventoryPanel.craftItems = false;
				inventoryPanel.Sync();
				if(craftingPanel != null)
				{
					DisposeC();
				}
			}
			else
			{
				DisposeI();
			}
		}




		
	}

	
	void DisposeI()
	{
		inventoryPanel.Dispose();
		Destroy(inventoryPanel.transform.gameObject);
		inventoryPanel = null;
	}

	void DisposeC()
	{
		craftingPanel.Dispose();
		Destroy(craftingPanel.transform.gameObject);
		craftingPanel = null;
	}

}
