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

public class Inventory : MonoBehaviour
{
    public Slots[] playerSlots;

    public void AddItem(GameObject pickUpToGive)
    {
        int num = 0;
        for (int i = 0; i < playerSlots.Length; i++)
        {
            if (playerSlots[i].isFull == false)
            {
                //Item Can be added to the inventory
                playerSlots[i].isFull = true;
                playerSlots[i].itemPrefab = pickUpToGive.GetComponent<PickUp>().itemScriptable.itemToSpawn;
                GameObject newGo = Instantiate(pickUpToGive.GetComponent<PickUp>().itemButton,
                    playerSlots[i].slot.transform, false);
                newGo.transform.GetChild(0).GetComponent<Image>().sprite = pickUpToGive.GetComponent<SpriteRenderer>().sprite;
                //Add a -- got item -- animation here
                break;
            }
            else
            {
                num++;
            }
        }

        if (num >= playerSlots.Length)
        {
            Debug.Log("Iventory Full, can't accept item");
        }
    }
}

[System.Serializable]
public class Slots
{
    public bool isFull;
    public GameObject slot;
    public GameObject itemPrefab;

    public void DropItem()
    {
        foreach (Transform child in slot.transform)
        {
            SpawnDroppedItem();
            GameObject.Destroy(child.gameObject);
            itemPrefab = null;
            isFull = false;
        }
    }

    public void DestroyItem()
    {
        foreach (Transform child in slot.transform)
        {
            GameObject.Destroy(child.gameObject);
            itemPrefab = null;
            isFull = false;
        }
    }

    public void SpawnDroppedItem()
    {
        Transform player = GameObject.FindGameObjectWithTag("Player").transform;
        Vector2 playerPos = new Vector2(player.position.x, player.position.y + 2f);
        GameObject.Instantiate(itemPrefab, playerPos, Quaternion.identity);
    }
}
