using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.EventSystems;

public class DragDropThing : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler
{
    public bool _resetPositionOnRelease = true;
    
    Vector3 _startPosition;

    public void OnDrag(PointerEventData eventData)
    {
        Debug.Log($"Dragging {eventData.position}");
        transform.position = eventData.position;
    }

    public void OnBeginDrag(PointerEventData eventData)
    {
        Debug.Log($"Begin Drag {eventData.position}");
 
        if (_resetPositionOnRelease)
            _startPosition = transform.position;
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        Debug.Log($"End Drag {eventData.position}");
        
        var hits = new List<RaycastResult>();
        EventSystem.current.RaycastAll(eventData, hits);

        var hit = hits.FirstOrDefault(t => t.gameObject.CompareTag("Droppable"));
        if (hit.isValid)
        {
            Debug.Log($"Dropped {gameObject} on {hit.gameObject}");
            return;
        }

        if (_resetPositionOnRelease)
            transform.position = _startPosition;
    }
}
