using Billing.iOS.Renderers;
using Billing.UI;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

[assembly: ExportRenderer(typeof(LongPressButton), typeof(LongPressButtonRenderer))]
namespace Billing.iOS.Renderers
{
    public class LongPressButtonRenderer : ButtonRenderer
    {
        private UILongPressGestureRecognizer longGesture;

        protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
        {
            base.OnElementChanged(e);

            if (Element is LongPressButton)
            {
                longGesture = new UILongPressGestureRecognizer(OnLongPressed);
                AddGestureRecognizer(longGesture);
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (longGesture != null)
            {
                RemoveGestureRecognizer(longGesture);
                longGesture = null;
            }

            base.Dispose(disposing);
        }

        private void OnLongPressed(UILongPressGestureRecognizer e)
        {
            if (Element is LongPressButton button)
            {
                button.TriggerLongPress();
            }
        }
    }
}