using TFramework;
using UnityEngine;
using UnityEngine.EventSystems;

public class DragIcon : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler, IPointerClickHandler
{
    public TAction onClick;
    private bool _isDrag;

    private RectTransform _selfRect;
    private Canvas _canvas;

    private void Start()
    {
        _selfRect = GetComponent<RectTransform>();
        _canvas = GetComponentInParent<Canvas>();
    }

    public void OnBeginDrag(PointerEventData eventData)
    {
        
    }

    public void OnDrag(PointerEventData eventData)
    {
        _isDrag = true;
        _selfRect.anchoredPosition += eventData.delta / _canvas.scaleFactor;
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        
    }

    public void OnPointerClick(PointerEventData eventData)
    {
        if (!_isDrag)
            onClick?.Invoke();
        else
            _isDrag = false;
    }
}
