using System;
using System.Linq;
using System.Reflection;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace Aurora.Design.Extensions
{
    [ContentProperty(nameof(Source))]
    public class ImageResourceExtension : IMarkupExtension
    {
        public string Source { get; set; }

        public object ProvideValue(IServiceProvider serviceProvider)
        {
            if (Source == null)
            {
                return null;
            }
            Assembly[] referencedAssemblies = AppDomain.CurrentDomain.GetAssemblies();
            var gtkAsm = referencedAssemblies.FirstOrDefault((e) =>
            {
                return e.FullName.Contains("Aurora.gtk");
            });

            foreach (var res in gtkAsm.GetManifestResourceNames())
            {
                System.Diagnostics.Debug.WriteLine("found resource: " + res);
            }

            if (gtkAsm == null && gtkAsm is Assembly)
            {
                return null;
            }

            // Do your translation lookup here, using whatever method you require
            var imageSource = ImageSource.FromResource(Source, gtkAsm);

            return imageSource;
        }
    }
}
