using Billing.iOS.Effects;
using Billing.UI;
using System;
using System.Linq;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

[assembly: ResolutionGroupName("Org.Tsanie")]
[assembly: ExportEffect(typeof(ShadowEffectPlatform), nameof(ShadowEffect))]
namespace Billing.iOS.Effects
{
    public class ShadowEffectPlatform : PlatformEffect
    {
        protected override void OnAttached()
        {
            try
            {
                var effect = (ShadowEffect)Element.Effects.FirstOrDefault(e => e is ShadowEffect);
                if (effect != null)
                {
                    var layer = Control.Layer;
                    layer.ShadowRadius = effect.Radius;
                    layer.ShadowColor = effect.Color.ToCGColor();
                    layer.ShadowOffset = effect.Offect.ToSizeF();
                    layer.ShadowOpacity = effect.Opacity;
                }
            }
            catch (Exception ex)
            {
                Helper.Error("shadow.effect.attached", $"Cannot set property on attached control, error: {ex.Message}");
            }
        }

        protected override void OnDetached()
        {
            var layer = Control?.Layer;
            if (layer != null)
            {
                layer.ShadowColor = Color.Transparent.ToCGColor();
                layer.ShadowOpacity = 0;
            }
        }
    }
}