using C4IT.MultiLanguage;
using C4IT.FASD.Base;
using FasdDesktopUi.Basics.Enums;
using FasdDesktopUi.Basics.Models;
using FasdDesktopUi.Basics.UserControls.AdaptableIcon;
using FasdDesktopUi.Pages.DetailsPage;
using FasdDesktopUi.Pages.SlimPage;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using static C4IT.Logging.cLogManager;
using System.Reflection;
using C4IT.Logging;
using System.Windows.Navigation;
using F4SD_AdaptableIcon.Enums;
using FasdDesktopUi.Basics;
using System.Diagnostics;
using C4IT.Configuration;

namespace FasdDesktopUi.Pages.SettingsPage
{
    public partial class SettingsPageView : SettingsPageBase
    {
        #region Properties

        private static SettingsPageView _Instance = null;
        public static SettingsPageView Instance { get
            {
                return _Instance ?? (_Instance = new SettingsPageView());
            }
        }

        private Dictionary<enumHighlightColor, bool> highlightColorActivationStatus;

        #region ZoomInPercent

        public int ZoomDetailsPageInPecent
        {
            get { return cFasdCockpitConfig.Instance.DetailsPageZoom; }
            set
            {
                if (value < 50)
                    value = 50;
                else if (value > 250)
                    value = 250;

                cFasdCockpitConfig.Instance.SetDetailsPageZoom(value);
                OnPropertyChanged(nameof(ZoomDetailsPageInPecent));
            }
        }
        public int ZoomSlimPageInPercent
        {
            get { return cFasdCockpitConfig.Instance.SlimPageZoom; }
            set
            {
                if (value < 50)
                    value = 50;
                else if (value > 250)
                    value = 250;

                cFasdCockpitConfig.Instance.SetSlimPageZoom(value);
                OnPropertyChanged(nameof(ZoomSlimPageInPercent));
            }
        }

        #endregion

        #region SlimPageVisibility

        public bool IsSlimPageVisibile
        {
            get
            {
                return cSupportCaseDataProvider.slimPage?.Visibility == Visibility.Visible;
            }
        }

        #endregion

        #region IsDetailsPageVisible

        public bool IsDetailsPageVisibile
        {
            get
            {
                return cSupportCaseDataProvider.detailsPage?.Visibility == Visibility.Visible;
            }
        }

        #endregion

        public int PositionOfSmallViews
        {
            get {
                return cF4sdGlobalConfig.ConvertHorizontalAlignmentToPosition(cFasdCockpitConfig.Instance.Global.SmallViewAlignment, 0);
            }
            set
            {
                var _algn = cF4sdGlobalConfig.ConvertPositionToHorizontalAlignment(value, enumF4sdHorizontalAlignment.Right);
                UpdatePositionOfSmallViewTextBlock(value);
                if (_algn != cFasdCockpitConfig.Instance.Global.SmallViewAlignment)
                {
                    cFasdCockpitConfig.Instance.Global.SmallViewAlignment = _algn;
                    cFasdCockpitConfig.Instance.Global.Save("SmallViewAlignment");
                    cFasdCockpitConfig.Instance.OnUiSettingsChanged();
                }
                OnPropertyChanged(nameof(PositionOfSmallViews));
            }
        }

        public int PositionOfFavouriteBar
        {
            get
            {
                return cF4sdGlobalConfig.ConvertHorizontalAlignmentToPosition(cFasdCockpitConfig.Instance.Global.FavouriteBarAlignment, 0);
            }
            set
            {
                var _algn = cF4sdGlobalConfig.ConvertPositionToHorizontalAlignment(value, enumF4sdHorizontalAlignment.Right);
                UpdatePositionOfFavouriteBarTextBlock(value);
                if (cFasdCockpitConfig.Instance.Global.FavouriteBarAlignment != _algn)
                {
                    cFasdCockpitConfig.Instance.Global.FavouriteBarAlignment = _algn;
                    cFasdCockpitConfig.Instance.Global.Save("FavouriteBarAlignment");
                    cFasdCockpitConfig.Instance.OnUiSettingsChanged();
                }
                OnPropertyChanged(nameof(PositionOfFavouriteBar));
            }
        }

        public bool ShouldSkipSlimView
        {
            get => cFasdCockpitConfig.Instance.Global.ShouldSkipSlimView;
            set
            {
                cFasdCockpitConfig.Instance.Global.ShouldSkipSlimView = value;
                cFasdCockpitConfig.Instance.Global.Save("ShouldSkipSlimView");
                OnPropertyChanged(nameof(ShouldSkipSlimView));
            }
        }

        #endregion

        public static SettingsPageView Create()
        {
            if (Instance == null)
                return new SettingsPageView();
            return Instance;
        }

        private SettingsPageView()
        {
            InitializeComponent();
            UpdatePositionOfSmallViewTextBlock(PositionOfSmallViews);
            UpdatePositionOfFavouriteBarTextBlock(PositionOfFavouriteBar);
            cFasdCockpitConfig.Instance.UiSettingsChanged += CockpitConfig_UiSettingsChanged;
        }

        private void UpdatePositionOfFavouriteBarTextBlock(int positionIndex)
        {
            try
            {
                Dispatcher.Invoke(() =>
                {
                    FavouritePositionSlider.ClearValue(BackgroundProperty);

                    switch (positionIndex)
                    {
                        case 1:
                            FavouritePositionLeftTextBlock.ClearValue(ForegroundProperty);
                            FavouritePositionRightTextBlock.ClearValue(ForegroundProperty);
                            FavouritePositionSlider.SetResourceReference(BackgroundProperty, "Color.FunctionMarker");
                            break;
                       case 2:
                            FavouritePositionRightTextBlock.SetResourceReference(ForegroundProperty, "Color.FunctionMarker");
                            FavouritePositionLeftTextBlock.ClearValue(ForegroundProperty);
                            break;
                        default:
                            FavouritePositionLeftTextBlock.SetResourceReference(ForegroundProperty, "Color.FunctionMarker");
                            FavouritePositionRightTextBlock.ClearValue(ForegroundProperty);
                            break;
                    }
                });
            }
            catch (Exception E)
            {
                LogException(E);
            }
        }

        private void UpdatePositionOfSmallViewTextBlock(int positionIndex)
        {
            try
            {
                Dispatcher.Invoke(() =>
                {
                    switch (positionIndex)
                    {
                        case 1:
                            break;
                        case 2:
                            SmallViewRightTextBlock.SetResourceReference(ForegroundProperty, "Color.FunctionMarker");
                            SmallViewLeftTextBlock.ClearValue(ForegroundProperty);
                            break;
                        default:
                            SmallViewLeftTextBlock.SetResourceReference(ForegroundProperty, "Color.FunctionMarker");
                            SmallViewRightTextBlock.ClearValue(ForegroundProperty);
                            break;
                    }
                });
            }
            catch (Exception E)
            {
                LogException(E);
            }
        }

        private void CockpitConfig_UiSettingsChanged(object sender, EventArgs e)
        {
            ZoomDetailsPageInPecent = cFasdCockpitConfig.Instance.DetailsPageZoom;
            ZoomSlimPageInPercent = cFasdCockpitConfig.Instance.SlimPageZoom;

            // hide or deactivate ShouldSkipSlimView options due to the policies
            var _policy = cFasdCockpitConfig.Instance.Global.GetPropertyPolicy("ShouldSkipSlimView");
            if (_policy == enumConfigPolicy.Hidden)
            {
                ShouldSkipSlimViewLabel.Visibility = Visibility.Collapsed;
                ShouldSkipSlimViewCheckBox.Visibility = Visibility.Collapsed;
            }
            else
            {
                ShouldSkipSlimViewLabel.Visibility = Visibility.Visible;
                ShouldSkipSlimViewCheckBox.Visibility = Visibility.Visible;
            }
            if (_policy == enumConfigPolicy.Default)
            {
                ShouldSkipSlimViewCheckBox.IsEnabled = true;
                ShouldSkipSlimViewPolicy.Visibility = Visibility.Collapsed;
                ShouldSkipSlimViewCheckBox.ToolTip = null;
                ShouldSkipSlimViewPolicy.ToolTip = null;
            }
            else
            {
                ShouldSkipSlimViewCheckBox.IsEnabled = false;
                ShouldSkipSlimViewPolicy.Visibility = Visibility.Visible;
                ShouldSkipSlimViewCheckBox.ToolTip = cMultiLanguageSupport.GetItem("Settings.Global.PolicyTooltip");
                ShouldSkipSlimViewPolicy.ToolTip = cMultiLanguageSupport.GetItem("Settings.Global.PolicyTooltip");
            }
            OnPropertyChanged(nameof(ShouldSkipSlimView));

            // hide or deactivate SmallViewAlignment options due to the policies
            _policy = cFasdCockpitConfig.Instance.Global.GetPropertyPolicy("SmallViewAlignment");
            if (_policy == enumConfigPolicy.Hidden)
            {
                PositionOfSmallViewsLabel.Visibility = Visibility.Collapsed;
                PositionOfSmallViewsInput.Visibility = Visibility.Collapsed;
            }
            else
            {
                PositionOfSmallViewsLabel.Visibility = Visibility.Visible;
                PositionOfSmallViewsInput.Visibility = Visibility.Visible;
            }
            if (_policy == enumConfigPolicy.Default)
            {
                PositionOfSmallViewsInput.IsEnabled = true;
                PositionOfSmallViewsPolicy.Visibility = Visibility.Collapsed;
                PositionOfSmallViewsPolicy.ToolTip = null;
            }
            else
            {
                PositionOfSmallViewsInput.IsEnabled = false;
                PositionOfSmallViewsPolicy.Visibility = Visibility.Visible;
                PositionOfSmallViewsPolicy.ToolTip = cMultiLanguageSupport.GetItem("Settings.Global.PolicyTooltip");
            }
            OnPropertyChanged(nameof(PositionOfSmallViews));

            // hide or deactivate FavouriteBarAlignment options due to the policies
            _policy = cFasdCockpitConfig.Instance.Global.GetPropertyPolicy("FavouriteBarAlignment");
            if (_policy == enumConfigPolicy.Hidden)
            {
                PositionOfFavouriteBarLabel.Visibility = Visibility.Collapsed;
                PositionOfFavouriteBarInput.Visibility = Visibility.Collapsed;
            }
            else
            {
                PositionOfFavouriteBarLabel.Visibility = Visibility.Visible;
                PositionOfFavouriteBarInput.Visibility = Visibility.Visible;
            }
            if (_policy == enumConfigPolicy.Default)
            {
                PositionOfFavouriteBarInput.IsEnabled = true;
                PositionOfFavouriteBarPolicy.Visibility = Visibility.Collapsed;
                PositionOfFavouriteBarPolicy.ToolTip = null;
            }
            else
            {
                PositionOfFavouriteBarInput.IsEnabled = false;
                PositionOfFavouriteBarPolicy.Visibility = Visibility.Visible;
                PositionOfFavouriteBarPolicy.ToolTip = cMultiLanguageSupport.GetItem("Settings.Global.PolicyTooltip");
            }
            OnPropertyChanged(nameof(PositionOfFavouriteBar));

        }

        public void SetUpSettingsControls()
        {
            try
            {
                enumAppColorMode selectedAppColorMode = cFasdCockpitConfig.Instance.DetailsPageColorMode;
                SelectAppColorMode(selectedAppColorMode);

                foreach (Border highlightBorder in HighlightColorStack.Children)
                {
                    HighlightColorBorder_Click(highlightBorder);
                    HighlightColorBorder_Click(highlightBorder);
                }

            }
            catch (Exception E)
            {
                LogException(E);
            }
        }

        #region Events

        #region Window Events

        private void Window_Initialized(object sender, EventArgs e)
        {
            try
            {
                highlightColorActivationStatus = !string.IsNullOrEmpty(cFasdCockpitConfig.Instance.HighlightColorVisibility) ?
                    JsonConvert.DeserializeObject<Dictionary<enumHighlightColor, bool>>(cFasdCockpitConfig.Instance.HighlightColorVisibility) :
                    new Dictionary<enumHighlightColor, bool>()
                    {
                        { enumHighlightColor.blue, true},
                        { enumHighlightColor.green, true},
                        { enumHighlightColor.orange, true},
                        { enumHighlightColor.red, true}
                    };

                if (DesignerProperties.GetIsInDesignMode(this))
                    return;

                SetUpSettingsControls();
            }
            catch (Exception E)
            {
                LogException(E);
            }
        }

        private async void SettingsWindow_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            try
            {
                OnPropertyChanged(nameof(IsSlimPageVisibile));
                OnPropertyChanged(nameof(IsDetailsPageVisibile));

                BlurInvoker_IsActiveChanged(sender, e);

                if (e.NewValue is bool b && b is false)
                    if (cSupportCaseDataProvider.detailsPage?.Visibility == Visibility.Visible)
                        await cSupportCaseDataProvider.detailsPage.AdjustWindowSizeAsync();
            }
            catch (Exception E)
            {
                LogException(E);
            }
        }
        #endregion

        #region CloseButton

        private void CloseButton_Click()
        {
            Hide();
        }

        private void CloseButton_MouseUp(object sender, MouseButtonEventArgs e)
        {
            CloseButton_Click();
        }

        private void CloseButton_TouchDown(object sender, TouchEventArgs e)
        {
            CloseButton_Click();
        }

        #endregion

        #region SetLanguage

        private void Flag_Click(object sender)
        {
            if (!(sender is FrameworkElement FE))
                return;

            var NewLanguage = FE.Tag?.ToString();
            var OldLanguage = cMultiLanguageSupport.CurrentLanguage;


            cMultiLanguageSupport.CurrentLanguage = NewLanguage;

            cFasdCockpitConfig.Instance.SelectedLanguage = NewLanguage;
            cFasdCockpitConfig.Instance.Save("SelectedLanguage");

            if (OldLanguage == NewLanguage)
                return;

            var dialogResult = CustomMessageBox.CustomMessageBox.Show(cMultiLanguageSupport.GetItem("Menu.SelectLanguage.RestartDialog.Text"), cMultiLanguageSupport.GetItem("Menu.SelectLanguage.RestartDialog.Caption"), enumHealthCardStateLevel.Info, null, true);

            if (dialogResult == true)
            {
                System.Windows.Forms.Application.Restart();
                Application.Current.Shutdown();
            }
        }

        private void Flag_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Flag_Click(sender);
        }

        private void Flag_TouchDown(object sender, TouchEventArgs e)
        {
            Flag_Click(sender);
        }

        #endregion

        #region SelectColorMode

        private void SelectAppColorMode(enumAppColorMode appColorMode)
        {
            try
            {
                string src = "";

                switch (appColorMode)
                {
                    case enumAppColorMode.DarkMode:
                        LightModeIcon.SelectedInternIcon = enumInternIcons.style_sun;
                        LightModeIcon.ClearValue(AdaptableIcon.PrimaryIconColorProperty);
                        DarkModeIcon.SelectedInternIcon = enumInternIcons.style_moonFilled;
                        DarkModeIcon.SetResourceReference(AdaptableIcon.PrimaryIconColorProperty, "Color.Menu.Icon.Hover");

                        src = @"ResourceDictionaries\DarkModeResources.xaml";
                        break;
                    case enumAppColorMode.LightMode:
                        LightModeIcon.SelectedInternIcon = enumInternIcons.style_sunFilled;
                        LightModeIcon.SetResourceReference(AdaptableIcon.PrimaryIconColorProperty, "Color.Menu.Icon.Hover");
                        DarkModeIcon.SelectedInternIcon = enumInternIcons.style_moon;
                        DarkModeIcon.ClearValue(AdaptableIcon.PrimaryIconColorProperty);

                        src = @"ResourceDictionaries\LightModeResources.xaml";
                        break;
                    default:
                        LightModeIcon.SelectedInternIcon = enumInternIcons.style_sunFilled;
                        DarkModeIcon.SelectedInternIcon = enumInternIcons.style_moon;
                        LightModeIcon.SetResourceReference(AdaptableIcon.PrimaryIconColorProperty, "Color.Menu.Icon.Hover");
                        break;
                }

                cFasdCockpitConfig.Instance.DetailsPageColorMode = appColorMode;
                cFasdCockpitConfig.Instance.Save("DetailsPageColorMode");

                Application.Current.Resources.MergedDictionaries.Insert(0, new ResourceDictionary { Source = new Uri(src, UriKind.Relative) });
                Application.Current.Resources.MergedDictionaries.Remove(Application.Current.Resources.MergedDictionaries[1]);
            }
            catch (Exception E)
            {
                LogException(E);
            }
        }

        private void ColorModeIcon_Click(object sender)
        {
            try
            {
                string senderTag = (sender as FrameworkElement).Tag.ToString().ToLower();

                switch (senderTag)
                {
                    case "darkmode":
                        SelectAppColorMode(enumAppColorMode.DarkMode);
                        break;
                    case "lightmode":
                        SelectAppColorMode(enumAppColorMode.LightMode);
                        break;
                    default:
                        break;
                }
            }
            catch (Exception E)
            {
                LogException(E);
            }
        }

        private void ColorModeIcon_MouseUp(object sender, MouseButtonEventArgs e)
        {
            ColorModeIcon_Click(sender);
        }

        private void ColorModeIcon_TouchDown(object sender, TouchEventArgs e)
        {
            ColorModeIcon_Click(sender);
        }

        #endregion

        #region SelectHighlightColor

        private void SetHighlightColor(enumHighlightColor selectedColor, bool isActive)
        {
            try
            {
                Border selectedBorder = new Border();
                string colorReference = "";

                switch (selectedColor)
                {
                    case enumHighlightColor.blue:
                        selectedBorder = HighlightBlue;
                        colorReference = "Color.Blue";
                        break;
                    case enumHighlightColor.green:
                        selectedBorder = HighlightGreen;
                        colorReference = "Color.Green";
                        break;
                    case enumHighlightColor.orange:
                        selectedBorder = HighlightOrange;
                        colorReference = "Color.Orange";
                        break;
                    case enumHighlightColor.red:
                        selectedBorder = HighlightRed;
                        colorReference = "Color.Red";
                        break;
                }

                selectedBorder.ClearValue(BorderBrushProperty);

                if (isActive)
                {
                    selectedBorder.SetResourceReference(BorderBrushProperty, "Color.Menu.Icon");
                    selectedBorder.SetResourceReference(BackgroundProperty, colorReference);
                }
                else
                {
                    selectedBorder.SetResourceReference(BorderBrushProperty, colorReference);
                    selectedBorder.Background = Brushes.Transparent;
                }

            }
            catch (Exception E)
            {
                LogException(E);
            }
        }

        private void HightlightColorBorder_MouseEnter(object sender, MouseEventArgs e)
        {
            try
            {
                string tagValue = (sender as FrameworkElement).Tag.ToString().ToLower();

                switch (tagValue)
                {
                    case "blue":
                        SetHighlightColor(enumHighlightColor.blue, !highlightColorActivationStatus[enumHighlightColor.blue]);
                        break;
                    case "green":
                        SetHighlightColor(enumHighlightColor.green, !highlightColorActivationStatus[enumHighlightColor.green]);
                        break;
                    case "orange":
                        SetHighlightColor(enumHighlightColor.orange, !highlightColorActivationStatus[enumHighlightColor.orange]);
                        break;
                    case "red":
                        SetHighlightColor(enumHighlightColor.red, !highlightColorActivationStatus[enumHighlightColor.red]);
                        break;
                    default:
                        break;
                }
            }
            catch (Exception E)
            {
                LogException(E);
            }
        }

        private void HightlightColorBorder_MouseLeave(object sender, MouseEventArgs e)
        {
            try
            {
                string tagValue = (sender as FrameworkElement).Tag.ToString().ToLower();

                switch (tagValue)
                {
                    case "blue":
                        SetHighlightColor(enumHighlightColor.blue, highlightColorActivationStatus[enumHighlightColor.blue]);
                        break;
                    case "green":
                        SetHighlightColor(enumHighlightColor.green, highlightColorActivationStatus[enumHighlightColor.green]);
                        break;
                    case "orange":
                        SetHighlightColor(enumHighlightColor.orange, highlightColorActivationStatus[enumHighlightColor.orange]);
                        break;
                    case "red":
                        SetHighlightColor(enumHighlightColor.red, highlightColorActivationStatus[enumHighlightColor.red]);
                        break;
                    default:
                        break;
                }
            }
            catch (Exception E)
            {
                LogException(E);
            }
        }

        private void HighlightColorBorder_Click(object sender)
        {
            try
            {
                enumHighlightColor clickedHighlightColor = enumHighlightColor.none;

                switch ((sender as Border).Tag.ToString().ToLower())
                {
                    case "blue":
                        clickedHighlightColor = enumHighlightColor.blue;
                        if (highlightColorActivationStatus[clickedHighlightColor])
                            Application.Current.Resources["HighlightColor.Blue"] = FindResource("Transparent.Custom");
                        else
                            Application.Current.Resources["HighlightColor.Blue"] = FindResource("Color.Blue");
                        break;
                    case "green":
                        clickedHighlightColor = enumHighlightColor.green;
                        if (highlightColorActivationStatus[clickedHighlightColor])
                            Application.Current.Resources["HighlightColor.Green"] = FindResource("Transparent.Custom");
                        else
                            Application.Current.Resources["HighlightColor.Green"] = FindResource("Color.Green");
                        break;
                    case "orange":
                        clickedHighlightColor = enumHighlightColor.orange;
                        if (highlightColorActivationStatus[clickedHighlightColor])
                            Application.Current.Resources["HighlightColor.Orange"] = FindResource("Transparent.Custom");
                        else
                            Application.Current.Resources["HighlightColor.Orange"] = FindResource("Color.Orange");
                        break;
                    case "red":
                        clickedHighlightColor = enumHighlightColor.red;
                        if (highlightColorActivationStatus[clickedHighlightColor])
                            Application.Current.Resources["HighlightColor.Red"] = FindResource("Transparent.Custom");
                        else
                            Application.Current.Resources["HighlightColor.Red"] = FindResource("Color.Red");
                        break;
                }

                highlightColorActivationStatus[clickedHighlightColor] = !highlightColorActivationStatus[clickedHighlightColor];

                string jsonText = JsonConvert.SerializeObject(highlightColorActivationStatus, Formatting.Indented);
                cFasdCockpitConfig.Instance.HighlightColorVisibility = jsonText;
                cFasdCockpitConfig.Instance.Save("HighlightColorVisibility");

                SetHighlightColor(clickedHighlightColor, highlightColorActivationStatus[clickedHighlightColor]);
            }
            catch (Exception E)
            {
                LogException(E);
            }
        }

        private void HighlightColorBorder_MouseUp(object sender, MouseButtonEventArgs e)
        {
            HighlightColorBorder_Click(sender);
        }

        private void HighlightColorBorder_TouchDown(object sender, TouchEventArgs e)
        {
            HighlightColorBorder_Click(sender);
        }

        #endregion

        #region SmallViewTextBlockClick

        private void SmallViewTextBlock_Click(object sender)
        {
            try
            {
                if (sender is FrameworkElement senderElement)
                    if (int.TryParse(senderElement.Tag.ToString(), out int positionIndex))
                        PositionOfSmallViews = positionIndex;
            }
            catch (Exception E)
            {
                LogException(E);
            }
        }

        private void SmallViewTextBlock_TouchDown(object sender, TouchEventArgs e)
        {
            SmallViewTextBlock_Click(sender);
        }

        private void SmallViewTextBlock_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            SmallViewTextBlock_Click(sender);
        }

        #endregion

        #region FavouritePositionTextBlock_Click

        private void FavouritePositionTextBlock_Click(object sender)
        {
            try
            {
                if (sender is FrameworkElement senderElement)
                    if (int.TryParse(senderElement.Tag.ToString(), out int positionIndex))
                        PositionOfFavouriteBar = positionIndex;
            }
            catch (Exception E)
            {
                LogException(E);
            }
        }

        private void FavouritePositionTextBlock_TouchDown(object sender, TouchEventArgs e)
        {
            FavouritePositionTextBlock_Click(sender);
        }

        private void FavouritePositionTextBlock_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            FavouritePositionTextBlock_Click(sender);
        }

        #endregion

        #endregion

        #region ApplyZoomButton_Click

        private void ScaleValueTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                if (!(sender is FrameworkElement senderElement))
                    return;

                if (!senderElement.IsFocused)
                    return;

                UpdateApplyButtonVisibility();
            }
            catch (Exception E)
            {
                LogException(E);
            }
        }

        private void UpdateApplyButtonVisibility()
        {
            try
            {
                if (ApplySlimPageZoomButton != null)
                {
                    bool isSlimPageButtonVisible = false;
                    if (int.TryParse(SlimPageScaleValueTextBox.Text, out var slimPageZoom))
                        if (slimPageZoom >= 50 && slimPageZoom <= 250)
                            isSlimPageButtonVisible = slimPageZoom != ZoomSlimPageInPercent;

                    ApplySlimPageZoomButton.Visibility = isSlimPageButtonVisible ? Visibility.Visible : Visibility.Collapsed;
                }


                if (ApplyDetailsPageZoomButton != null)
                {
                    bool isDetailsPageButtonVisible = false;
                    if (int.TryParse(ScaleValueTextBox.Text, out var detailsPageZoom))
                        if (detailsPageZoom >= 50 && detailsPageZoom <= 250)
                            isDetailsPageButtonVisible = detailsPageZoom != ZoomDetailsPageInPecent;

                    ApplyDetailsPageZoomButton.Visibility = isDetailsPageButtonVisible ? Visibility.Visible : Visibility.Collapsed;
                }
            }
            catch (Exception E)
            {
                LogException(E);
            }
        }

        private void ApplyZoomButton_Click(object sender)
        {
            try
            {
                if (!(sender is FrameworkElement senderElement))
                    return;

                switch (senderElement.Tag)
                {
                    case "SlimPageZoom":
                        if (int.TryParse(SlimPageScaleValueTextBox.Text, out var slimPageZoom))
                            ZoomSlimPageInPercent = slimPageZoom;
                        break;
                    case "DetailsPageZoom":
                        if (int.TryParse(ScaleValueTextBox.Text, out var detailsPageZoom))
                            ZoomDetailsPageInPecent = detailsPageZoom;
                        break;
                    default:
                        break;
                }

                UpdateApplyButtonVisibility();
            }
            catch (Exception E)
            {
                LogException(E);
            }
        }

        private void ApplyZoomButton_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            ApplyZoomButton_Click(sender);
        }

        private void ApplyZoomButton_TouchDown(object sender, TouchEventArgs e)
        {
            ApplyZoomButton_Click(sender);
        }

        #endregion

        private void SettingsWindow_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Enter)
                {
                    if (int.TryParse(ScaleValueTextBox.Text, out var detailsPageZoom))
                        ZoomDetailsPageInPecent = detailsPageZoom;

                    if (int.TryParse(SlimPageScaleValueTextBox.Text, out var slimPageZoom))
                        ZoomSlimPageInPercent = slimPageZoom;

                    UpdateApplyButtonVisibility();
                }
            }
            catch (Exception E)
            {
                LogException(E);
            }
        }

        private async void SettingsWindow_Closed(object sender, EventArgs e)
        {
            try
            {
                _Instance = null;
                if (cSupportCaseDataProvider.detailsPage?.Visibility == Visibility.Visible)
                    await cSupportCaseDataProvider.detailsPage.AdjustWindowSizeAsync();
            }
            catch (Exception E)
            {
                LogException(E);
            }
        }

    }
}
