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

[AddComponentMenu("按钮上")]
[RequireComponent(typeof(UnityEngine.EventSystems.EventTrigger))]
public class OnChlickDrag : MonoBehaviour, IPointerDownHandler, IPointerExitHandler,IPointerEnterHandler
{
    public DeviceType deviceType;
    public GameObject prefab;
    //private GameObject prefab;
    public GameObject InstantiatePos;
    /// <summary>
    /// 货架父物体
    /// </summary>
    public GameObject framesParent;
    public List<GameObject> framesList = new List<GameObject>();
    private bool isDragging = false;

    public Transform ct;
    public bool isClicked = false; // 是否点击了图片
    public bool isExit = false;


    public void OnPointerDown(PointerEventData eventData)
    {
        isClicked = true;
        isExit = false;
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        if (isClicked)
        {
            isExit = true;
            isClicked = false;
            ct = Instantiate(prefab).transform;
            ct.GetComponent<DragTest>().listTargets = framesList;
            Vector3 curScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Vector3.Distance(Camera.main.transform.position, InstantiatePos.transform.position));
            var curPosition = Camera.main.ScreenToWorldPoint(curScreenSpace)/* + offset*/;
            ct.position = curPosition;

            var dt = ct.GetComponent<DragTest>();
            if (dt)
            {
                dt.isExit = this.isExit;
                dt.isClicked = this.isClicked;
            }
        }
    }

    void Update()
    {
        //if (ct)
        //{
        //    var dt = ct.GetComponent<DragTest>();
        //    if (dt)
        //    {
        //        dt.isExit = this.isExit;
        //        dt.isClicked = this.isClicked;
        //    }
        //}
        //else
        //{
        //    isExit = false;
        //}
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        isExit = true;
    }

    public enum DeviceType
    {
        dev1, dev2, dev3
    }
}
