using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEditor;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;



public class EquippableItem : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler, IPointerClickHandler
{
    [HideInInspector] public Image slotBorder;

    [SerializeField] private Image image;
    
    public int index;
    public ItemScriptableObject itemScriptObj;
    public Transform parentAfterDrag;
    public Transform swappingVariable;
    public EquippableItemUI EquippableItemUI;
    public ItemType itemType;
    public bool isTheEquippedItem;

    public enum ItemType
    {
        Potion,
        Weapon,
        Food
    }

    public void Start()
    {

        slotBorder = transform.GetChild(0).GetComponent<Image>();

        if (itemScriptObj.itemPrefab.activeSelf)
        {
            slotBorder.gameObject.SetActive(true);
            isTheEquippedItem = true;

           EquippableItemUI.indexContainer = index;

        }
        else
        {
            slotBorder.gameObject.SetActive(false);
            isTheEquippedItem = false;
        }
    }
    public void OnBeginDrag(PointerEventData eventData)
    {
        //i need this variable in the script "inventorySlot" to swap 2 item in the inventory
        swappingVariable = transform.parent;
        parentAfterDrag = transform.parent;
        //this is to make the item above everything else in the hierarchy
        transform.SetParent(transform.root);
        transform.SetAsLastSibling();
        
        //without this the item can not be placed in other slots
        image.raycastTarget = false;
        
    }
    public void OnDrag(PointerEventData eventData)
    {

        transform.position = Input.mousePosition;
    }
    public void OnEndDrag(PointerEventData eventData)
    {
       //this is to assign to the item a new parent object (that is the slot in wich he was dragged)
        transform.SetParent(parentAfterDrag);
        transform.position = transform.parent.position;

        image.raycastTarget = true;
    }

    //Detect if a right click occurs on the item, if yes then it activate the item UI
    public void OnPointerClick(PointerEventData pointerEventData)
    {
       
        if (pointerEventData.button == PointerEventData.InputButton.Left)
        {
                
                EquippableItemUI.ClickedEquippableItem = GetComponent<EquippableItem>();
                EquippableItemUI.gameObject.transform.position = transform.position + itemScriptObj.UIPosition;
                EquippableItemUI.gameObject.SetActive(true);
                EquippableItemUI.ItemWasClicked(itemScriptObj.itemName, itemScriptObj.itemDescription, itemScriptObj.itemInfoValue, itemScriptObj.itemArtwork, itemScriptObj.artworkScale);
            
        }
    }
}