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

[assembly: ExportRenderer(typeof(TintImageButton), typeof(TintImageButtonRenderer))]
namespace Billing.Droid.Renderers
{
    public class TintImageButtonRenderer : ImageButtonRenderer
    {
        public TintImageButtonRenderer(Context context) : base(context)
        {
        }

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

            if (e.PropertyName == TintHelper.TintColor ||
                e.PropertyName == nameof(Image.Source))
            {
                if (Drawable != null && Element is TintImageButton image)
                {
                    Drawable.SetTint(TintHelper.GetTintColor(image)?.ToAndroid() ?? 0);
                }
            }
        }

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

            if (e.NewElement is TintImageButton image)
            {
                SetBackgroundColor(Android.Graphics.Color.Transparent);
                if (Drawable != null)
                {
                    Drawable.SetTint(TintHelper.GetTintColor(image)?.ToAndroid() ?? 0);
                }
            }
        }
    }
}
