using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class ItemOnnDrag : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
public Transform originatParent;
public Inventory myBag;
private int currentItemID;

public void OnBeginDrag(PointerEventData eventData)
{
    originatParent = transform.parent;
    currentItemID = originatParent.GetComponent<Slot>().slotID;
    transform.SetParent(transform.parent.parent);
    transform.position = eventData.position;
    GetComponent<CanvasGroup>().blocksRaycasts = false;
}

public void OnDrag(PointerEventData eventData)
{
    transform.position = eventData.position;
    //print(eventData.pointerCurrentRaycast.gameObject.name);
}

public void OnEndDrag(PointerEventData eventData)
{
    if (eventData.pointerCurrentRaycast.gameObject != null)
    {
        if (eventData.pointerCurrentRaycast.gameObject.name == "Item Image")//物品栏有物品时
        {
            transform.SetParent(eventData.pointerCurrentRaycast.gameObject.transform.parent.parent);
            transform.position = eventData.pointerCurrentRaycast.gameObject.transform.parent.parent.position;

            //交换存储位置
            var temp = myBag.itemList[currentItemID];
            myBag.itemList[currentItemID] = myBag.itemList[eventData.pointerCurrentRaycast.gameObject.GetComponentInParent<Slot>().slotID];
            myBag.itemList[eventData.pointerCurrentRaycast.gameObject.GetComponentInParent<Slot>().slotID] = temp;
            //交换存储位置

            eventData.pointerCurrentRaycast.gameObject.transform.parent.SetParent(originatParent);
            eventData.pointerCurrentRaycast.gameObject.transform.parent.position = originatParent.position;
            GetComponent<CanvasGroup>().blocksRaycasts = true;
            return;
        }
        if (eventData.pointerCurrentRaycast.gameObject.name == "Slot(Clone)")
        {
            //物品栏为空时
            transform.SetParent(eventData.pointerCurrentRaycast.gameObject.transform);
            transform.position = eventData.pointerCurrentRaycast.gameObject.transform.position;

            myBag.itemList[eventData.pointerCurrentRaycast.gameObject.GetComponentInParent<Slot>().slotID] = myBag.itemList[currentItemID];
            if (eventData.pointerCurrentRaycast.gameObject.GetComponent<Slot>().slotID != currentItemID)
                myBag.itemList[currentItemID] = null;

            GetComponent<CanvasGroup>().blocksRaycasts = true;
            return;
        }
    }
    //其他任何位置返回原位
    transform.SetParent(originatParent);
    transform.position = originatParent.position;
    GetComponent<CanvasGroup>().blocksRaycasts = true;
}

}