// <copyright file="Config.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>

namespace FSI.BT.Tools.SystemTrayMenu
{
    using System;
    using System.Drawing;
    using System.IO;
    using System.Text;
    using System.Windows.Forms;
    using FSI.BT.Tools.Global.UserInterface.FolderBrowseDialog;
    using FSI.BT.Tools.Global.Utilities;
    using FSI.BT.Tools.SystemTrayMenu.Properties;
    using FSI.BT.Tools.SystemTrayMenu.Utilities;
    using Microsoft.Win32;
    using Svg;
    using static FSI.BT.Tools.SystemTrayMenu.Utilities.IconReader;

    public static class Config
    {
        private static readonly Icon SystemTrayMenu = new Icon(Properties.Resources.SystemTrayMenu, SystemInformation.SmallIconSize);
        private static readonly Icon IconRootFolder = GetIconSTA(Path, Path, false, IconSize.Small, true);

        private static bool readDarkModeDone;
        private static bool isDarkMode;
        private static bool readHideFileExtdone;
        private static bool isHideFileExtension;

        
        public static string Path =>Global.Vars.SystemTrayMenuSettings.PathDirectory;

        public static string SearchPattern => Global.Vars.SystemTrayMenuSettings.SearchPattern;

        public static bool ShowDirectoryTitleAtTop => Global.Vars.SystemTrayMenuSettings.ShowDirectoryTitleAtTop;

        public static bool ShowSearchBar => Global.Vars.SystemTrayMenuSettings.ShowSearchBar;

        public static bool ShowCountOfElementsBelow => Global.Vars.SystemTrayMenuSettings.ShowCountOfElementsBelow;

        public static bool ShowFunctionKeyOpenFolder => Global.Vars.SystemTrayMenuSettings.ShowFunctionKeyOpenFolder;

        public static bool ShowFunctionKeyPinMenu => Global.Vars.SystemTrayMenuSettings.ShowFunctionKeyPinMenu;

        public static bool ShowFunctionKeySettings => Global.Vars.SystemTrayMenuSettings.ShowFunctionKeySettings;

        public static bool ShowFunctionKeyRestart => Global.Vars.SystemTrayMenuSettings.ShowFunctionKeyRestart;

        public static bool AlwaysOpenByPin { get; internal set; }

        public static void Initialize()
        {
            UpgradeIfNotUpgraded();
            InitializeColors();
            if (string.IsNullOrEmpty(Global.Vars.SystemTrayMenuSettings.PathIcoDirectory))
            {
                Global.Vars.SystemTrayMenuSettings.PathIcoDirectory = System.IO.Path.Combine(
                    System.IO.Path.Combine(
                        Environment.GetFolderPath(
                            Environment.SpecialFolder.ApplicationData), $"FSI.BT.Tools.SystemTrayMenu"), "ico");
                if (!Directory.Exists(Global.Vars.SystemTrayMenuSettings.PathIcoDirectory))
                {
                    Directory.CreateDirectory(Global.Vars.SystemTrayMenuSettings.PathIcoDirectory);
                }
            }
        }

        public static void Dispose()
        {
            AppColors.BitmapOpenFolder.Dispose();
            AppColors.BitmapPin.Dispose();
            AppColors.BitmapPinActive.Dispose();
            AppColors.BitmapSettings.Dispose();
            AppColors.BitmapRestart.Dispose();
            AppColors.BitmapSearch.Dispose();
        }

        public static Icon GetAppIcon()
        {
            if (Global.Vars.SystemTrayMenuSettings.UseIconFromRootFolder)
            {
                return IconRootFolder;
            }
            else
            {
                return SystemTrayMenu;
            }
        }

        public static void SetFolderByWindowsContextMenu(string[] args)
        {
            if (args != null && args.Length > 0 && args[0] != "-r")
            {
                string path = args[0];
                Log.Info($"SetFolderByWindowsContextMenu() path: {path}");
                Global.Vars.SystemTrayMenuSettings.PathDirectory = path;
                //Global.Vars.SystemTrayMenuSettings.Save();
            }
        }

        public static void LoadOrSetByUser()
        {
            if (string.IsNullOrEmpty(Path))
            {
                string textFirstStart = Global.Utilities.Translator.GetText(
                    "Read the FAQ and then choose a root directory for SystemTrayMenu.");
                MessageBox.Show(
                    textFirstStart,
                    "SystemTrayMenu",
                    MessageBoxButtons.OK);
                ShowHelpFAQ();
                SetFolderByUser();
            }
        }

        public static void SetFolderByUser(bool save = true)
        {
            using FolderDialog dialog = new();
            dialog.InitialFolder = Path;

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                Global.Vars.SystemTrayMenuSettings.PathDirectory = dialog.Folder;
                //if (save)
                //{
                //    Global.Vars.SystemTrayMenuSettings.Save();
                //}
            }
        }

        public static void SetFolderIcoByUser()
        {
            using FolderDialog dialog = new();
            dialog.InitialFolder = Global.Vars.SystemTrayMenuSettings.PathIcoDirectory;

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                Global.Vars.SystemTrayMenuSettings.PathIcoDirectory = dialog.Folder;
            }
        }

        internal static void ShowHelpFAQ()
        {
            Log.ProcessStart("https://github.com/Hofknecht/SystemTrayMenu#FAQ");
        }

        internal static void ShowSupportSystemTrayMenu()
        {
            Log.ProcessStart("https://github.com/Hofknecht/SystemTrayMenu#donations");
        }

        /// <summary>
        /// Read the OS setting whether dark mode is enabled.
        /// </summary>
        /// <returns>true = Dark mode; false = Light mode.</returns>
        internal static bool IsDarkMode()
        {
            if (!readDarkModeDone)
            {
                // 0 = Dark mode, 1 = Light mode
                if (Global.Vars.SystemTrayMenuSettings.IsDarkModeAlwaysOn ||
                    IsRegistryValueThisValue(
                    @"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize",
                    "AppsUseLightTheme",
                    "0"))
                {
                    isDarkMode = true;
                }

                readDarkModeDone = true;
            }

            return isDarkMode;
        }

        internal static void ResetReadDarkModeDone()
        {
            isDarkMode = false;
            readDarkModeDone = false;
        }

        /// <summary>
        /// Read the OS setting whether HideFileExt enabled.
        /// </summary>
        /// <returns>isHideFileExtension.</returns>
        internal static bool IsHideFileExtension()
        {
            if (!readHideFileExtdone)
            {
                // 0 = To show extensions, 1 = To hide extensions
                if (IsRegistryValueThisValue(
                    @"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced",
                    "HideFileExt",
                    "1"))
                {
                    isHideFileExtension = true;
                }

                readHideFileExtdone = true;
            }

            return isHideFileExtension;
        }

        internal static void InitializeColors(bool save = true)
        {
            ColorConverter converter = new();
            ColorAndCode colorAndCode = default;
            bool changed = false;

            colorAndCode.HtmlColorCode = Global.Vars.SystemTrayMenuSettings.ColorSelectedItem;
            colorAndCode.Color = Color.FromArgb(204, 232, 255);
            colorAndCode = ProcessColorAndCode(converter, colorAndCode, ref changed);
            Global.Vars.SystemTrayMenuSettings.ColorSelectedItem = colorAndCode.HtmlColorCode;
            AppColors.SelectedItem = colorAndCode.Color;

            colorAndCode.HtmlColorCode = Global.Vars.SystemTrayMenuSettings.ColorDarkModeSelecetedItem;
            colorAndCode.Color = Color.FromArgb(51, 51, 51);
            colorAndCode = ProcessColorAndCode(converter, colorAndCode, ref changed);
            Global.Vars.SystemTrayMenuSettings.ColorDarkModeSelecetedItem = colorAndCode.HtmlColorCode;
            AppColors.DarkModeSelecetedItem = colorAndCode.Color;

            colorAndCode.HtmlColorCode = Global.Vars.SystemTrayMenuSettings.ColorSelectedItemBorder;
            colorAndCode.Color = Color.FromArgb(153, 209, 255);
            colorAndCode = ProcessColorAndCode(converter, colorAndCode, ref changed);
            Global.Vars.SystemTrayMenuSettings.ColorSelectedItemBorder = colorAndCode.HtmlColorCode;
            AppColors.SelectedItemBorder = colorAndCode.Color;

            colorAndCode.HtmlColorCode = Global.Vars.SystemTrayMenuSettings.ColorDarkModeSelectedItemBorder;
            colorAndCode.Color = Color.FromArgb(20, 29, 75);
            colorAndCode = ProcessColorAndCode(converter, colorAndCode, ref changed);
            Global.Vars.SystemTrayMenuSettings.ColorDarkModeSelectedItemBorder = colorAndCode.HtmlColorCode;
            AppColors.DarkModeSelectedItemBorder = colorAndCode.Color;

            colorAndCode.HtmlColorCode = Global.Vars.SystemTrayMenuSettings.ColorOpenFolder;
            colorAndCode.Color = Color.FromArgb(194, 245, 222);
            colorAndCode = ProcessColorAndCode(converter, colorAndCode, ref changed);
            Global.Vars.SystemTrayMenuSettings.ColorOpenFolder = colorAndCode.HtmlColorCode;
            AppColors.OpenFolder = colorAndCode.Color;

            colorAndCode.HtmlColorCode = Global.Vars.SystemTrayMenuSettings.ColorDarkModeOpenFolder;
            colorAndCode.Color = Color.FromArgb(20, 65, 42);
            colorAndCode = ProcessColorAndCode(converter, colorAndCode, ref changed);
            Global.Vars.SystemTrayMenuSettings.ColorDarkModeOpenFolder = colorAndCode.HtmlColorCode;
            AppColors.DarkModeOpenFolder = colorAndCode.Color;

            colorAndCode.HtmlColorCode = Global.Vars.SystemTrayMenuSettings.ColorOpenFolderBorder;
            colorAndCode.Color = Color.FromArgb(153, 255, 165);
            colorAndCode = ProcessColorAndCode(converter, colorAndCode, ref changed);
            Global.Vars.SystemTrayMenuSettings.ColorOpenFolderBorder = colorAndCode.HtmlColorCode;
            AppColors.OpenFolderBorder = colorAndCode.Color;

            colorAndCode.HtmlColorCode = Global.Vars.SystemTrayMenuSettings.ColorDarkModeOpenFolderBorder;
            colorAndCode.Color = Color.FromArgb(20, 75, 85);
            colorAndCode = ProcessColorAndCode(converter, colorAndCode, ref changed);
            Global.Vars.SystemTrayMenuSettings.ColorDarkModeOpenFolderBorder = colorAndCode.HtmlColorCode;
            AppColors.DarkModeOpenFolderBorder = colorAndCode.Color;

            colorAndCode.HtmlColorCode = Global.Vars.SystemTrayMenuSettings.ColorIcons;
            colorAndCode.Color = Color.FromArgb(149, 160, 166);
            colorAndCode = ProcessColorAndCode(converter, colorAndCode, ref changed);
            Global.Vars.SystemTrayMenuSettings.ColorIcons = colorAndCode.HtmlColorCode;
            AppColors.Icons = colorAndCode.Color;

            colorAndCode.HtmlColorCode = Global.Vars.SystemTrayMenuSettings.ColorDarkModeIcons;
            colorAndCode.Color = Color.FromArgb(149, 160, 166);
            colorAndCode = ProcessColorAndCode(converter, colorAndCode, ref changed);
            Global.Vars.SystemTrayMenuSettings.ColorDarkModeIcons = colorAndCode.HtmlColorCode;
            AppColors.DarkModeIcons = colorAndCode.Color;

            string htmlColorCodeIcons;
            if (IsDarkMode())
            {
                htmlColorCodeIcons = Global.Vars.SystemTrayMenuSettings.ColorDarkModeIcons;
            }
            else
            {
                htmlColorCodeIcons = Global.Vars.SystemTrayMenuSettings.ColorIcons;
            }

            AppColors.BitmapOpenFolder =
                ReadSvg(Properties.Resources.ic_fluent_folder_arrow_right_48_regular, htmlColorCodeIcons);
            AppColors.BitmapPin =
                ReadSvg(Properties.Resources.ic_fluent_pin_48_regular, htmlColorCodeIcons);
            AppColors.BitmapSettings =
                ReadSvg(Properties.Resources.ic_fluent_settings_28_regular, htmlColorCodeIcons);
            AppColors.BitmapRestart =
                ReadSvg(Properties.Resources.ic_fluent_arrow_sync_24_regular, htmlColorCodeIcons);
            AppColors.BitmapPinActive =
                ReadSvg(Properties.Resources.ic_fluent_pin_48_filled, htmlColorCodeIcons);
            AppColors.BitmapSearch =
                ReadSvg(Properties.Resources.ic_fluent_search_48_regular, htmlColorCodeIcons);

            colorAndCode.HtmlColorCode = Global.Vars.SystemTrayMenuSettings.ColorSearchField;
            colorAndCode.Color = Color.FromArgb(255, 255, 255);
            colorAndCode = ProcessColorAndCode(converter, colorAndCode, ref changed);
            Global.Vars.SystemTrayMenuSettings.ColorSearchField = colorAndCode.HtmlColorCode;
            AppColors.SearchField = colorAndCode.Color;

            colorAndCode.HtmlColorCode = Global.Vars.SystemTrayMenuSettings.ColorDarkModeSearchField;
            colorAndCode.Color = Color.FromArgb(25, 25, 25);
            colorAndCode = ProcessColorAndCode(converter, colorAndCode, ref changed);
            Global.Vars.SystemTrayMenuSettings.ColorDarkModeSearchField = colorAndCode.HtmlColorCode;
            AppColors.DarkModeSearchField = colorAndCode.Color;

            colorAndCode.HtmlColorCode = Global.Vars.SystemTrayMenuSettings.ColorBackground;
            colorAndCode.Color = Color.FromArgb(255, 255, 255);
            colorAndCode = ProcessColorAndCode(converter, colorAndCode, ref changed);
            Global.Vars.SystemTrayMenuSettings.ColorBackground = colorAndCode.HtmlColorCode;
            AppColors.Background = colorAndCode.Color;

            colorAndCode.HtmlColorCode = Global.Vars.SystemTrayMenuSettings.ColorDarkModeBackground;
            colorAndCode.Color = Color.FromArgb(32, 32, 32);
            colorAndCode = ProcessColorAndCode(converter, colorAndCode, ref changed);
            Global.Vars.SystemTrayMenuSettings.ColorDarkModeBackground = colorAndCode.HtmlColorCode;
            AppColors.DarkModeBackground = colorAndCode.Color;

            colorAndCode.HtmlColorCode = Global.Vars.SystemTrayMenuSettings.ColorBackgroundBorder;
            colorAndCode.Color = Color.FromArgb(0, 0, 0);
            colorAndCode = ProcessColorAndCode(converter, colorAndCode, ref changed);
            Global.Vars.SystemTrayMenuSettings.ColorBackgroundBorder = colorAndCode.HtmlColorCode;
            AppColors.BackgroundBorder = colorAndCode.Color;

            colorAndCode.HtmlColorCode = Global.Vars.SystemTrayMenuSettings.ColorDarkModeBackgroundBorder;
            colorAndCode.Color = Color.FromArgb(0, 0, 0);
            colorAndCode = ProcessColorAndCode(converter, colorAndCode, ref changed);
            Global.Vars.SystemTrayMenuSettings.ColorDarkModeBackgroundBorder = colorAndCode.HtmlColorCode;
            AppColors.DarkModeBackgroundBorder = colorAndCode.Color;

            colorAndCode.HtmlColorCode = Global.Vars.SystemTrayMenuSettings.ColorArrow;
            colorAndCode.Color = Color.FromArgb(96, 96, 96);
            colorAndCode = ProcessColorAndCode(converter, colorAndCode, ref changed);
            Global.Vars.SystemTrayMenuSettings.ColorArrow = colorAndCode.HtmlColorCode;
            AppColors.Arrow = colorAndCode.Color;

            colorAndCode.HtmlColorCode = Global.Vars.SystemTrayMenuSettings.ColorArrowHoverBackground;
            colorAndCode.Color = Color.FromArgb(218, 218, 218);
            colorAndCode = ProcessColorAndCode(converter, colorAndCode, ref changed);
            Global.Vars.SystemTrayMenuSettings.ColorArrowHoverBackground = colorAndCode.HtmlColorCode;
            AppColors.ArrowHoverBackground = colorAndCode.Color;

            colorAndCode.HtmlColorCode = Global.Vars.SystemTrayMenuSettings.ColorArrowHover;
            colorAndCode.Color = Color.FromArgb(0, 0, 0);
            colorAndCode = ProcessColorAndCode(converter, colorAndCode, ref changed);
            Global.Vars.SystemTrayMenuSettings.ColorArrowHover = colorAndCode.HtmlColorCode;
            AppColors.ArrowHover = colorAndCode.Color;

            colorAndCode.HtmlColorCode = Global.Vars.SystemTrayMenuSettings.ColorArrowClick;
            colorAndCode.Color = Color.FromArgb(255, 255, 255);
            colorAndCode = ProcessColorAndCode(converter, colorAndCode, ref changed);
            Global.Vars.SystemTrayMenuSettings.ColorArrowClick = colorAndCode.HtmlColorCode;
            AppColors.ArrowClick = colorAndCode.Color;

            colorAndCode.HtmlColorCode = Global.Vars.SystemTrayMenuSettings.ColorArrowClickBackground;
            colorAndCode.Color = Color.FromArgb(96, 96, 96);
            colorAndCode = ProcessColorAndCode(converter, colorAndCode, ref changed);
            Global.Vars.SystemTrayMenuSettings.ColorArrowClickBackground = colorAndCode.HtmlColorCode;
            AppColors.ArrowClickBackground = colorAndCode.Color;

            colorAndCode.HtmlColorCode = Global.Vars.SystemTrayMenuSettings.ColorSliderArrowsAndTrackHover;
            colorAndCode.Color = Color.FromArgb(192, 192, 192);
            colorAndCode = ProcessColorAndCode(converter, colorAndCode, ref changed);
            Global.Vars.SystemTrayMenuSettings.ColorSliderArrowsAndTrackHover = colorAndCode.HtmlColorCode;
            AppColors.SliderArrowsAndTrackHover = colorAndCode.Color;

            colorAndCode.HtmlColorCode = Global.Vars.SystemTrayMenuSettings.ColorSlider;
            colorAndCode.Color = Color.FromArgb(205, 205, 205);
            colorAndCode = ProcessColorAndCode(converter, colorAndCode, ref changed);
            Global.Vars.SystemTrayMenuSettings.ColorSlider = colorAndCode.HtmlColorCode;
            AppColors.Slider = colorAndCode.Color;

            colorAndCode.HtmlColorCode = Global.Vars.SystemTrayMenuSettings.ColorSliderHover;
            colorAndCode.Color = Color.FromArgb(166, 166, 166);
            colorAndCode = ProcessColorAndCode(converter, colorAndCode, ref changed);
            Global.Vars.SystemTrayMenuSettings.ColorSliderHover = colorAndCode.HtmlColorCode;
            AppColors.SliderHover = colorAndCode.Color;

            colorAndCode.HtmlColorCode = Global.Vars.SystemTrayMenuSettings.ColorSliderDragging;
            colorAndCode.Color = Color.FromArgb(96, 96, 96);
            colorAndCode = ProcessColorAndCode(converter, colorAndCode, ref changed);
            Global.Vars.SystemTrayMenuSettings.ColorSliderDragging = colorAndCode.HtmlColorCode;
            AppColors.SliderDragging = colorAndCode.Color;

            colorAndCode.HtmlColorCode = Global.Vars.SystemTrayMenuSettings.ColorScrollbarBackground;
            colorAndCode.Color = Color.FromArgb(240, 240, 240);
            colorAndCode = ProcessColorAndCode(converter, colorAndCode, ref changed);
            Global.Vars.SystemTrayMenuSettings.ColorScrollbarBackground = colorAndCode.HtmlColorCode;
            AppColors.ScrollbarBackground = colorAndCode.Color;

            colorAndCode.HtmlColorCode = Global.Vars.SystemTrayMenuSettings.ColorArrowDarkMode;
            colorAndCode.Color = Color.FromArgb(103, 103, 103);
            colorAndCode = ProcessColorAndCode(converter, colorAndCode, ref changed);
            Global.Vars.SystemTrayMenuSettings.ColorArrowDarkMode = colorAndCode.HtmlColorCode;
            AppColors.ArrowDarkMode = colorAndCode.Color;

            colorAndCode.HtmlColorCode = Global.Vars.SystemTrayMenuSettings.ColorArrowHoverBackgroundDarkMode;
            colorAndCode.Color = Color.FromArgb(55, 55, 55);
            colorAndCode = ProcessColorAndCode(converter, colorAndCode, ref changed);
            Global.Vars.SystemTrayMenuSettings.ColorArrowHoverBackgroundDarkMode = colorAndCode.HtmlColorCode;
            AppColors.ArrowHoverBackgroundDarkMode = colorAndCode.Color;

            colorAndCode.HtmlColorCode = Global.Vars.SystemTrayMenuSettings.ColorArrowHoverDarkMode;
            colorAndCode.Color = Color.FromArgb(103, 103, 103);
            colorAndCode = ProcessColorAndCode(converter, colorAndCode, ref changed);
            Global.Vars.SystemTrayMenuSettings.ColorArrowHoverDarkMode = colorAndCode.HtmlColorCode;
            AppColors.ArrowHoverDarkMode = colorAndCode.Color;

            colorAndCode.HtmlColorCode = Global.Vars.SystemTrayMenuSettings.ColorArrowClickDarkMode;
            colorAndCode.Color = Color.FromArgb(23, 23, 23);
            colorAndCode = ProcessColorAndCode(converter, colorAndCode, ref changed);
            Global.Vars.SystemTrayMenuSettings.ColorArrowClickDarkMode = colorAndCode.HtmlColorCode;
            AppColors.ArrowClickDarkMode = colorAndCode.Color;

            colorAndCode.HtmlColorCode = Global.Vars.SystemTrayMenuSettings.ColorArrowClickBackgroundDarkMode;
            colorAndCode.Color = Color.FromArgb(166, 166, 166);
            colorAndCode = ProcessColorAndCode(converter, colorAndCode, ref changed);
            Global.Vars.SystemTrayMenuSettings.ColorArrowClickBackgroundDarkMode = colorAndCode.HtmlColorCode;
            AppColors.ArrowClickBackgroundDarkMode = colorAndCode.Color;

            colorAndCode.HtmlColorCode = Global.Vars.SystemTrayMenuSettings.ColorSliderArrowsAndTrackHoverDarkMode;
            colorAndCode.Color = Color.FromArgb(77, 77, 77);
            colorAndCode = ProcessColorAndCode(converter, colorAndCode, ref changed);
            Global.Vars.SystemTrayMenuSettings.ColorSliderArrowsAndTrackHoverDarkMode = colorAndCode.HtmlColorCode;
            AppColors.SliderArrowsAndTrackHoverDarkMode = colorAndCode.Color;

            colorAndCode.HtmlColorCode = Global.Vars.SystemTrayMenuSettings.ColorSliderDarkMode;
            colorAndCode.Color = Color.FromArgb(77, 77, 77);
            colorAndCode = ProcessColorAndCode(converter, colorAndCode, ref changed);
            Global.Vars.SystemTrayMenuSettings.ColorSliderDarkMode = colorAndCode.HtmlColorCode;
            AppColors.SliderDarkMode = colorAndCode.Color;

            colorAndCode.HtmlColorCode = Global.Vars.SystemTrayMenuSettings.ColorSliderHoverDarkMode;
            colorAndCode.Color = Color.FromArgb(122, 122, 122);
            colorAndCode = ProcessColorAndCode(converter, colorAndCode, ref changed);
            Global.Vars.SystemTrayMenuSettings.ColorSliderHoverDarkMode = colorAndCode.HtmlColorCode;
            AppColors.SliderHoverDarkMode = colorAndCode.Color;

            colorAndCode.HtmlColorCode = Global.Vars.SystemTrayMenuSettings.ColorSliderDraggingDarkMode;
            colorAndCode.Color = Color.FromArgb(166, 166, 166);
            colorAndCode = ProcessColorAndCode(converter, colorAndCode, ref changed);
            Global.Vars.SystemTrayMenuSettings.ColorSliderDraggingDarkMode = colorAndCode.HtmlColorCode;
            AppColors.SliderDraggingDarkMode = colorAndCode.Color;

            colorAndCode.HtmlColorCode = Global.Vars.SystemTrayMenuSettings.ColorScrollbarBackgroundDarkMode;
            colorAndCode.Color = Color.FromArgb(23, 23, 23);
            colorAndCode = ProcessColorAndCode(converter, colorAndCode, ref changed);
            Global.Vars.SystemTrayMenuSettings.ColorScrollbarBackgroundDarkMode = colorAndCode.HtmlColorCode;
            AppColors.ScrollbarBackgroundDarkMode = colorAndCode.Color;

            //if (save && changed)
            //{
            //    Global.Vars.SystemTrayMenuSettings.Save();
            //}
        }

        private static Bitmap ReadSvg(byte[] byteArray, string htmlColorCode)
        {
            string str = Encoding.UTF8.GetString(byteArray);
            str = str.Replace("#585858", htmlColorCode);
            byteArray = Encoding.UTF8.GetBytes(str);

            using MemoryStream stream = new(byteArray);
            SvgDocument svgDocument = SvgDocument.Open<SvgDocument>(stream);
            svgDocument.Color = new SvgColourServer(Color.Black);
            return svgDocument.Draw();
        }

        private static bool IsRegistryValueThisValue(string keyName, string valueName, string value)
        {
            bool isRegistryValueThisValue = false;

            try
            {
                object registryHideFileExt = Registry.GetValue(keyName, valueName, 1);

                if (registryHideFileExt == null)
                {
                    Log.Info($"Could not read registry keyName:{keyName} valueName:{valueName}");
                }
                else if (registryHideFileExt.ToString() == value)
                {
                    isRegistryValueThisValue = true;
                }
            }
            catch (Exception ex)
            {
                if (ex is System.Security.SecurityException ||
                    ex is IOException)
                {
                    Log.Warn($"Could not read registry keyName:{keyName} valueName:{valueName}", ex);
                }
                else
                {
                    throw;
                }
            }

            return isRegistryValueThisValue;
        }

        private static void UpgradeIfNotUpgraded()
        {
            //if (!Global.Vars.SystemTrayMenuSettings.IsUpgraded)
            //{
            //    Global.Vars.SystemTrayMenuSettings.Upgrade();
            //    Global.Vars.SystemTrayMenuSettings.IsUpgraded = true;
            //    Global.Vars.SystemTrayMenuSettings.Save();
            //    Log.Info($"Settings upgraded from {CustomSettingsProvider.UserConfigPath}");
            //}
        }

        private static ColorAndCode ProcessColorAndCode(
            ColorConverter colorConverter,
            ColorAndCode colorAndCode,
            ref bool changedHtmlColorCode)
        {
            try
            {
                colorAndCode.Color = (Color)colorConverter.ConvertFromString(colorAndCode.HtmlColorCode);
            }
            catch (ArgumentException ex)
            {
                Log.Warn($"HtmlColorCode {colorAndCode.HtmlColorCode}", ex);
                colorAndCode.HtmlColorCode = ColorTranslator.ToHtml(colorAndCode.Color);
                changedHtmlColorCode = true;
            }

            return colorAndCode;
        }
    }
}
