using System.ComponentModel;
using Pixiview.iOS.Renderers;
using Pixiview.UI;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

[assembly: ExportRenderer(typeof(RoundLabel), typeof(RoundLabelRenderer))]
namespace Pixiview.iOS.Renderers
{
    public class RoundLabelRenderer : LabelRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
        {
            base.OnElementChanged(e);

            var layer = Layer;
            if (layer != null && e.NewElement is RoundLabel label)
            {
                var radius = label.CornerRadius;
                if (radius > 0)
                {
                    layer.CornerRadius = radius;
                    //layer.MasksToBounds = true;
                }
                else
                {
                    layer.CornerRadius = 0;
                }
                if (layer.BackgroundColor != default)
                {
                    layer.BackgroundColor = label.BackgroundColor.ToCGColor();
                }
            }
        }

        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);

            if (e.PropertyName == RoundLabel.CornerRadiusProperty.PropertyName)
            {
                var layer = Layer;
                if (layer != null && Element is RoundLabel label)
                {
                    var radius = label.CornerRadius;
                    if (radius > 0)
                    {
                        layer.CornerRadius = radius;
                        layer.BackgroundColor = label.BackgroundColor.ToCGColor();
                        //layer.MasksToBounds = true;
                    }
                }
            }
        }
    }
}
