using Serilog;
using System.IO;
using System.Reflection;
using System.Text;

namespace TINK.ViewModel
{
    public static class ViewModelResourceHelper
    {
        /// <summary> Get ressource prefix depending on platform.</summary>
        public static string RessourcePrefix
        {
            get
            {
#if __IOS__
                return "TINK.iOS.";
#endif
#if __ANDROID__
                return "TINK.Droid.";
#endif
#if WINDOWS_UWP
                return "TINK.WinPhone.";
#endif
            }
        }

        /// <summary> Gets an an embedded html ressource.</summary>
        /// <param name="resrouceName">Name of resource to get.</param>
        /// <returns></returns>
        public static string GetSource(string resrouceName)
        {
            var l_oRessourceName = RessourcePrefix + resrouceName;
            Log.Verbose($"Using this resource prefix { RessourcePrefix}.");
            // note that the prefix includes the trailing period '.' that is required
            var assembly = typeof(ViewModelResourceHelper).GetTypeInfo().Assembly;
            var stream = assembly.GetManifestResourceStream(l_oRessourceName);
            return stream != null
                ? (new StreamReader(stream, Encoding.UTF8)).ReadToEnd()
                 : string.Format("<!DOCTYPE html><html lang=\"de\"><body>An error occurred loading html- ressource {0}.</body>", l_oRessourceName);
        }
    }
}