using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GFATask
{
    /// <summary>
    /// Statische Klasse zum Auslesen der Registrierungseinträge für die installierten Internet Browser
    /// </summary>
    class AlternateWebbrowser
    {
        /// <summary>
        /// Gibt das Symbol des Internet Browsers aus
        /// </summary>
        /// <param name="browsername">Name des Internet Browsers</param>
        /// <returns></returns>
        public static Bitmap GetImage(string browsername = "default")
        {
            browsername = browsername == "default" ? Config.Main.DefaultWebBrowser : browsername;
            Bitmap icon = new Bitmap(1, 1);
            if (XObjects.WebBrowsers.Exists(browser => browser.Name == browsername))
            {
                var webbrowser = XObjects.WebBrowsers.Find(browser => browser.Name == browsername);
                try
                {
                    using (FileStream fs = new FileStream(webbrowser.IconPath, FileMode.Open, FileAccess.Read, FileShare.Delete | FileShare.ReadWrite))
                    {
                        icon = Icon.FromHandle(Icon.ExtractAssociatedIcon(fs.Name).Handle).ToBitmap();
                        //icon = ((Icon)Icon.FromHandle(Icon.ExtractAssociatedIcon(fs.Name).Handle).Clone()).ToBitmap();
                    }
                }
                catch (Exception ex)
                {
                    Log.EventLog.Write("Webbrowser Icon nicht gefunden", LogClassification.LowError, ex);
                }
            }
            return icon;
        }


        /// <summary>
        /// Findet den Namen anhand des ausführenden Pfades des Internet Browsers. Die Informationen werden aus der Windows-Registrierung ausgelesen
        /// </summary>
        /// <param name="browserexec">Auszuführender Pfad des Internet Browsers</param>
        /// <returns></returns>
        public static string GetName(string browserexec)
        {
            try
            {
                if(XObjects.WebBrowsers.Any(browser => browser.Path != "unknown" && browser.Path == browserexec))
                {
                    return XObjects.WebBrowsers.Find(browser => browserexec == browser.Path).Name;
                }
                else
                {
                    return "";
                }
            }
            catch (Exception ex)
            {
                Log.EventLog.Write("Ungültiger Registrierungseintrag für Browsernamen", LogClassification.CriticalError, ex);
                return "";
            }
        }


        /// <summary>
        /// Gibt den auszuführenden Pfad des Internet Browsers anhand des Namens aus. Die Informationen werden aus der Windows-Registrierung ausgelesen
        /// </summary>
        /// <param name="browsername">Name des Internet Browsers</param>
        /// <returns></returns>
        public static string GetExec(string browsername = "default")
        {
            browsername = browsername == "default" ? Config.Main.DefaultWebBrowser : browsername;
            string browserexec = "";
            if(XObjects.WebBrowsers.Exists(browser => browser.Name == browsername))
            {
                try
                {
                    browserexec = XObjects.WebBrowsers.Find(browser => browser.Name == browsername).Path;
                    if(browserexec == "unknown")
                        Log.EventLog.Write("Webbrowser nicht gefunden", LogClassification.LowError);
                }
                catch (Exception ex)
                {
                    Log.EventLog.Write("Webbrowser nicht gefunden", LogClassification.LowError, ex);
                }
            }
            return browserexec;
        }


        /// <summary>
        /// Zeichnet das Internet Browser Symbol des ausgewählten Browsers in die untere rechte Ecke des AppButton-Symbols
        /// </summary>
        /// <param name="appImage">Das AppButton-Symbol</param>
        /// <param name="browsername">Name des Internet Browsers</param>
        public static void DrawWebBrowserIcon(Image appImage, string browsername = "default", string appname = "")
        {
            browsername = browsername == "default" ? Config.Main.DefaultWebBrowser : browsername;
            if(XObjects.WebBrowsers.Exists(browser => browser.Name == browsername))
            {
                try
                {
                    using (Graphics g = Graphics.FromImage(appImage))
                    {
                        g.DrawImage(GetImage(browsername), new Rectangle(appImage.Width / 2, appImage.Height / 2, appImage.Width / 2, appImage.Height / 2));
                        g.Save();
                    }
                }
                catch (Exception ex)
                {
                    Log.EventLog.Write($"Konnte Webbrowser Icon nicht zeichnen [{appname}]", LogClassification.LowError, ex);
                }
            }
        }
    }
}
