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

public class PickUp : MonoBehaviour
{
    private Inventory playerInventory;
    public GameObject itemButton;
    public ItemScriptable itemScriptable;

    // Start is called before the first frame update
    void Start()
    {
        playerInventory = GameObject.FindGameObjectWithTag("Player").GetComponent<Inventory>();
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.CompareTag("Player"))
        {
            for (int i = 0; i < playerInventory.playerSlots.Length; i++)
            {
                if (playerInventory.playerSlots[i].isFull == false)
                {
                    //Item Can be added to the inventory
                    playerInventory.playerSlots[i].isFull = true;
                    playerInventory.playerSlots[i].itemPrefab = itemScriptable.itemToSpawn;
                    GameObject newGo = Instantiate(itemButton, playerInventory.playerSlots[i].slot.transform, false);
                    newGo.transform.GetChild(0).GetComponent<Image>().sprite = this.GetComponent<SpriteRenderer>().sprite;
                    Destroy(gameObject);
                    break;
                }
            }
        }
    }
}
