using Pixiview.Droid.Effects;
using Pixiview.Utils;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using static Android.Views.View;

[assembly: ResolutionGroupName("Pixiview")]
[assembly: ExportEffect(typeof(LongPressEffectImplement), "LongPressEffect")]
namespace Pixiview.Droid.Effects
{
    public class LongPressEffectImplement : PlatformEffect
    {
        private bool attached;

        protected override void OnAttached()
        {
            if (!attached)
            {
                attached = true;
                Control.LongClick += Container_LongClick;
            }
        }

        protected override void OnDetached()
        {
            if (attached)
            {
                attached = false;
                Control.LongClick -= Container_LongClick;
            }
        }

        private void Container_LongClick(object sender, LongClickEventArgs e)
        {
            var element = Element;
            if (element != null)
            {
                var command = LongPressEffect.GetCommand(element);
                if (command != null)
                {
                    e.Handled = true;

                    var o = LongPressEffect.GetCommandParameter(element);
                    command.Execute(o);
                }
            }
        }
    }
}