using C4IT.Logging;
using Microsoft.Win32;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using static C4IT.Logging.cLogManager;

namespace FasdDesktopUi.Basics
{
    public class cBrowsers
    {
        List<cBrowser> browsers { get; set; }

        public cBrowsers()
        {
            this.browsers = GetBrowsers();
        }

        private List<cBrowser> GetBrowsers()
        {
            List<cBrowser> browsers = new List<cBrowser>();

            try
            {
                RegistryKey browserKeys;
                browserKeys = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\WOW6432Node\Clients\StartMenuInternet");
                if (browserKeys is null)
                    browserKeys = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Clients\StartMenuInternet");
                string[] browserNames = browserKeys.GetSubKeyNames();

                foreach (string browserName in browserNames)
                {
                    try
                    {
                        cBrowser browser = new cBrowser();
                        RegistryKey browserKey = browserKeys.OpenSubKey(browserName);
                        browser.Name = (string)browserKey.GetValue(null);
                        RegistryKey browserKeyPath = browserKey.OpenSubKey(@"shell\open\command");
                        browser.Path = browserKeyPath.GetValue(null).ToString().StripQuotes();
                        if (File.Exists(browser.Path))
                        {
                            browsers.Add(browser);
                            if (browser.Path != null)
                                try
                                {
                                    browser.Version = FileVersionInfo.GetVersionInfo(browser.Path).FileVersion;
                                }
                                catch
                                {
                                    browser.Version = "unknown";
                                }
                            else
                                browser.Version = "unknown";
                        }
                    }
                    catch (Exception E)
                    {
                        LogException(E);
                    }
                }
            }
            catch (Exception E)
            {
                LogException(E);
            }

            return browsers;
        }

        public cBrowser GetBrowserByName(string browserName)
        {
            foreach (cBrowser browser in this.browsers)
            {
                if (browser.Name.Equals(browserName, StringComparison.InvariantCultureIgnoreCase))
                    return browser;
            }
            return new cBrowser("Internet Explorer", "iexplorer.exe");
        }

        public void Start(string browserName, string Url)
        {
            try
            {
                if (browserName is null || browserName.ToLowerInvariant() == "default")
                {
                    Process.Start(Url);
                }
                else
                {
                    Url = "--single-argument " + Url;

                    var browser = GetBrowserByName(browserName);
                    if (!string.IsNullOrEmpty(browser.Path))
                        Process.Start(browser.Path, Url);
                    else
                        Process.Start(Url);
                }
            }
            catch (Exception E)
            {
                LogException(E);
            }
        }
    }

    public class cBrowser
    {
        public string Name { get; set; }
        public string Path { get; set; }
        public string Version { get; set; }

        public cBrowser(string name = "", string path = "", string version = "")
        {
            Name = name;
            Path = path;
            Version = version;
        }
    }
    internal static class Extensions
    {
        ///
        /// if string begins and ends with quotes, they are removed
        ///
        internal static String StripQuotes(this String s)
        {
            if (s.EndsWith("\"") && s.StartsWith("\""))
            {
                return s.Substring(1, s.Length - 2);
            }
            else
            {
                return s;
            }
        }
    }

}
