//单击按钮
using System.Diagnostics;
using UnityEngine.EventSystems;

public class ClickButton : ClickEvent.ClickListener
{
    protected override void OnPointerDown(PointerEventData eventData)
    {
        isRuning = true;
    }

    protected override void OnPointerUp(PointerEventData eventData)
    {
        if (isRuning)
        {
            Reset();
            callBack?.Invoke(true);
        }
    }

    protected override void Reset()
    {
        isRuning = false;
    }
}

//双击按钮
public class DoubleClickButton : ClickEvent.ClickListener
{
    public int maxSpaceTime = 250;
    private int clickCount = 0;
    private Stopwatch stopWatch;
    private float lastPointTime = 0;

    public DoubleClickButton()
    {
        stopWatch = new Stopwatch();
        Reset();
    }

    protected override void OnPointerDown(PointerEventData eventData)
    {
        isRuning = true;
    }

    protected override void OnPointerClick(PointerEventData eventData)
    {
        stopWatch.Start();
        clickCount++;
        if (clickCount == 2)
        {
            Reset();
            callBack?.Invoke(true);
        }
    }

    protected override void Update()
    {
        if (!stopWatch.IsRunning) return;
        if (stopWatch.ElapsedMilliseconds > maxSpaceTime)
        {
            Reset();
            callBack?.Invoke(false);
        }
    }

    protected override void Reset()
    {
        isRuning = false;
        if (stopWatch.IsRunning) stopWatch.Stop();
        stopWatch.Reset();
        clickCount = 0;
    }
}

//长按按钮
public class PressButton : ClickEvent.ClickListener
{
    public int pressMinTime = 2000;
    private Stopwatch stopWatch;

    public PressButton()
    {
        stopWatch = new Stopwatch();
    }

    protected override void OnPointerDown(PointerEventData eventData)
    {
        isRuning = true;
        stopWatch.Start();
    }

    protected override void OnPointerUp(PointerEventData eventData)
    {
        Reset();
        callBack?.Invoke(false);
    }


    protected override void Update()
    {
        if (!stopWatch.IsRunning) return;
        if (stopWatch.ElapsedMilliseconds > pressMinTime)
        {
            Reset();
            callBack?.Invoke(true);
        }
    }

    protected override void Reset()
    {
        isRuning = false;
        if (stopWatch.IsRunning) stopWatch.Stop();
        stopWatch.Reset();
    }
}