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

public class PickableController : MonoBehaviour
{
    public Item item;
    public string itemName;
    //public string name2;  //TODO Clear here
    public bool triggered;
    public int isPicked;

    public int HandleIndexInScelenObjectList;

    public void Awake()
    {

        //item.Name = gameObject.name;

        // IT DONT WORK - equippable item with the same name isn't finding
/*        Debug.Log("Items/" + itemName);
        item = Resources.Load<Item>("Items/" + itemName);

        if (item = null)
        {
            item = Resources.Load<EquippableItem>("Items/" + itemName);
        }*/
    }

    public void Start()
    {
        // if object after beeing created by Instaniate() gets "Clone)" postfix, EquippableItem.name gets this

        // if (PlayerPrefs.GetInt("continued") == 1)
        // {
        //     isPicked = PlayerPrefs.GetInt(name2);
        // }
        // else
        // {
        //     isPicked = PlayerPrefs.GetInt(name);
        // }
        //name = item.ItemModel.name;



        if (isPicked == 1)
        {
            //gameObject.SetActive(false);
            Destroy(gameObject);
        }
    }

    void Update()
    {
        if(triggered)
        {
            if (Input.GetKeyDown(KeyCode.E))
            {
                if(!InventoryUIManager.Instance.IsFull())
                {
                    var castedItem = this.item as EquippableItem;

                    if(castedItem == null)
                        InventoryUIManager.Instance.Add(new EquippableItem(this.item));
                    else
                        InventoryUIManager.Instance.Add(new EquippableItem(castedItem));

                    if (InventoryUIManager.Instance.GetPanelStatus())
                        GameObject.FindObjectOfType<InventoryPanelController>().BuildPanelContent(InventoryUIManager.Instance.GetList());

                    isPicked = 1;
                    //PlayerPrefs.SetInt(name, isPicked);
                    //gameObject.SetActive(false);

                    SceneEquippableItemManager.Instance.RemoveDynamicItem(item.name); // remove 

                    Destroy(gameObject);
                }else
                {
                    Debug.LogError("Can't pick item - Your inventory is full");
                }
            }
        }
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag == "Player")
        {
            triggered = true;
        }
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.tag == "Player")
        {
            triggered = false;
        }
    }

    // public void SaveCheckpoint()
    // {
    //     PlayerPrefs.SetInt(name2, isPicked);
    // }

}