using System;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace AlicizaX.UI
{
    public class ScrollbarEx : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IPointerEnterHandler, IPointerExitHandler
    {
        private RectTransform handle;
        private Scrollbar scrollbar;

        public Action OnDragEnd;

        private bool dragging;
        private bool hovering;

        private void Awake()
        {
            scrollbar = GetComponent<Scrollbar>();
            handle = scrollbar.handleRect;
        }

        public void OnBeginDrag(PointerEventData eventData)
        {
            dragging = true;
        }

        public void OnEndDrag(PointerEventData eventData)
        {
            dragging = false;
            if (!hovering)
            {
                if (scrollbar.direction == Scrollbar.Direction.TopToBottom ||
                    scrollbar.direction == Scrollbar.Direction.BottomToTop)
                {
#if PRIMETWEEN_SUPPORT
                    PrimeTween.Tween.ScaleX(handle, 1f, 0.2f);
#else
                    handle.localScale = new Vector3(1,handle.localScale.y,handle.localScale.z);
#endif
                }
                else
                {
#if PRIMETWEEN_SUPPORT
                    PrimeTween.Tween.ScaleY(handle, 1f, 0.2f);
#else
                    handle.localScale = new Vector3(handle.localScale.x,1,handle.localScale.z);
#endif
                }
            }

            OnDragEnd?.Invoke();
        }

        public void OnPointerEnter(PointerEventData eventData)
        {
            hovering = true;
            if (scrollbar.direction == Scrollbar.Direction.TopToBottom ||
                scrollbar.direction == Scrollbar.Direction.BottomToTop)
            {
#if PRIMETWEEN_SUPPORT
                PrimeTween.Tween.ScaleX(handle, 2f, 0.2f);
#else
                handle.localScale = new Vector3(2,handle.localScale.y,handle.localScale.z);
#endif
            }
            else
            {
#if PRIMETWEEN_SUPPORT
                PrimeTween.Tween.ScaleY(handle, 2f, 0.2f);
#else
                handle.localScale = new Vector3(handle.localScale.x, 2, handle.localScale.z);
#endif

            }
        }

        public void OnPointerExit(PointerEventData eventData)
        {
            hovering = false;
            if (!dragging)
            {
                if (scrollbar.direction == Scrollbar.Direction.TopToBottom ||
                    scrollbar.direction == Scrollbar.Direction.BottomToTop)
                {
#if PRIMETWEEN_SUPPORT
                    PrimeTween.Tween.ScaleX(handle, 1f, 0.2f);
#else
                    handle.localScale = new Vector3(1,handle.localScale.y,handle.localScale.z);
#endif
                }
                else
                {
#if PRIMETWEEN_SUPPORT
                    PrimeTween.Tween.ScaleY(handle, 1f, 0.2f);
#else
                    handle.localScale = new Vector3(handle.localScale.x,1,handle.localScale.z);
#endif
                }
            }
        }
    }
}
