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

[assembly: ExportRenderer(typeof(TintImage), typeof(TintImageRenderer))]
namespace Billing.iOS.Renderers
{
    public class TintImageRenderer : ImageRenderer
    {

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

            if (Control == null)
            {
                return;
            }

            if (e.PropertyName == nameof(Image.Source))
            {
                Control.Image = Control.Image.ImageWithRenderingMode(UIKit.UIImageRenderingMode.AlwaysTemplate);
            }
            else if (e.PropertyName == nameof(TintImage.PrimaryColor) && Element is TintImage image)
            {
                Control.TintColor = image.PrimaryColor?.ToUIColor();
            }
        }
        protected override void OnElementChanged(ElementChangedEventArgs<Image> e)
        {
            base.OnElementChanged(e);

            if (Control != null && Element is TintImage image)
            {
                if (Control.Image != null)
                {
                    Control.Image = Control.Image.ImageWithRenderingMode(UIKit.UIImageRenderingMode.AlwaysTemplate);
                    Control.TintColor = image.PrimaryColor?.ToUIColor();
                }
            }
        }
    }
}