using Android.Content;
using Billing.Droid.Renderers;
using Billing.UI;
using System.ComponentModel;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;

[assembly: ExportRenderer(typeof(TintImage), typeof(TintImageRenderer))]
namespace Billing.Droid.Renderers
{
    public class TintImageRenderer : ImageRenderer
    {
        public TintImageRenderer(Context context) : base(context)
        {
        }

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

            if (e.PropertyName == nameof(TintImage.PrimaryColor) && Control?.Drawable != null && Element is TintImage image)
            {
                Control.Drawable.SetTint(image.PrimaryColor?.ToAndroid() ?? 0);
            }
        }

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

            if (Control?.Drawable != null && Element is TintImage image)
            {
                Control.Drawable.SetTint(image.PrimaryColor?.ToAndroid() ?? 0);
            }
        }
    }
}