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 FasdDesktopUi.Basics.Enums;
using FasdDesktopUi.Basics.UserControls.AdaptableIcon;
using F4SD_AdaptableIcon.Enums;

using static C4IT.Logging.cLogManager;

using Newtonsoft.Json;



namespace FasdDesktopUi.Pages.DetailsPage.UserControls
{
    public partial class DetailsPageSettings : UserControl
    {
        #region Properties

        private Dictionary<enumHighlightColor, bool> highlightColorActivationStatus;

        #endregion

        public DetailsPageSettings()
        {
            InitializeComponent();
        }

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

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

                SizeSlider.Value = cFasdCockpitConfig.Instance.DetailsPageZoom / 100.0;
            }
            catch (Exception E)
            {
                LogException(E);
            }
        }

        #region Events

        private void UserControl_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);
            }
        }

        #region CloseButton

        private void CloseButton_Click()
        {
            SettingsUserControl.Visibility = Visibility.Collapsed;
        }

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

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

        #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
            {
                if (sender is FrameworkElement senderFrameworkElement)
                {
                    string senderTag = senderFrameworkElement.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
            {
                if (sender is FrameworkElement senderFrameworkElement)
                {
                    string tagValue = senderFrameworkElement.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
            {
                if (sender is FrameworkElement senderFrameworkElement)
                {
                    string tagValue = senderFrameworkElement.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;

                if (sender is FrameworkElement senderFrameworkElement)
                {
                    switch (senderFrameworkElement.Tag.ToString().ToLower())
                    {
                        case "blue":
                            clickedHighlightColor = enumHighlightColor.blue;
                            if (highlightColorActivationStatus[clickedHighlightColor])
                                Application.Current.Resources["HighlightColor.Blue"] = Brushes.Transparent;
                            else
                                Application.Current.Resources["HighlightColor.Blue"] = FindResource("Color.Blue");
                            break;
                        case "green":
                            clickedHighlightColor = enumHighlightColor.green;
                            if (highlightColorActivationStatus[clickedHighlightColor])
                                Application.Current.Resources["HighlightColor.Green"] = Brushes.Transparent;
                            else
                                Application.Current.Resources["HighlightColor.Green"] = FindResource("Color.Green");
                            break;
                        case "orange":
                            clickedHighlightColor = enumHighlightColor.orange;
                            if (highlightColorActivationStatus[clickedHighlightColor])
                                Application.Current.Resources["HighlightColor.Orange"] = Brushes.Transparent;
                            else
                                Application.Current.Resources["HighlightColor.Orange"] = FindResource("Color.Orange");
                            break;
                        case "red":
                            clickedHighlightColor = enumHighlightColor.red;
                            if (highlightColorActivationStatus[clickedHighlightColor])
                                Application.Current.Resources["HighlightColor.Red"] = Brushes.Transparent;
                            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

        #endregion

    }
}
