using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
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 C4IT.MultiLanguage;
using FasdDesktopUi.Basics.Models;
using FasdDesktopUi.Pages.SettingsPage;

using static C4IT.Logging.cLogManager;

namespace FasdDesktopUi.Pages.ShortCutPage
{
    public partial class ShortCutPageView : SettingsPageBase
    {
        private static ShortCutPageView _Instance = null;

        public static ShortCutPageView Instance
        {
            get { return _Instance = _Instance ?? new ShortCutPageView(); }
        }

        private ShortCutPageView()
        {
            InitializeComponent();
        }

        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);
            InitializeHotKeyInformation();
        }

        private void InitializeHotKeyInformation()
        {
            try
            {
                var hotKeyCategories = new List<cHotKeyCategoryInformation>();

                var initialSearchCategory = new cHotKeyCategoryInformation()
                {
                    Names = new C4IT.MultiLanguage.cMultiLanguageDictionary()
                    {
                        ["EN"] = "Initial search",
                        ["DE"] = "Initiale Suche"
                    },
                    HotKeyInformation = new List<cHotKeyInformation>()
                    {
                        new cHotKeyInformation()
                        {
                            Names = new C4IT.MultiLanguage.cMultiLanguageDictionary()
                            {
                                ["EN"] = "Start new empty search",
                                ["DE"] = "Neue leere Suche starten"
                            },
                            Modifiers = new List<ModifierKeys>() { ModifierKeys.Control },
                            HotKey = Key.F3
                        },
                        new cHotKeyInformation()
                        {
                            Names = new C4IT.MultiLanguage.cMultiLanguageDictionary()
                            {
                                ["EN"] = "Start new search with highlighted text",
                                ["DE"] = "Neue Suche nach markiertem Text starten"
                            },
                            Modifiers = new List<ModifierKeys>() { ModifierKeys.Control, ModifierKeys.Alt },
                            HotKey = Key.F3
                        },
                        new cHotKeyInformation()
                        {
                            Names = new C4IT.MultiLanguage.cMultiLanguageDictionary()
                            {
                                ["EN"] = "Select previous/next result",
                                ["DE"] = "Vorheriges/Nächstes Ergebnis auwählen"
                            },
                            HotKey = Key.Up,
                            AlternativeKey = Key.Down
                        },
                        new cHotKeyInformation()
                        {
                            Names = new C4IT.MultiLanguage.cMultiLanguageDictionary()
                            {
                                ["EN"] = "Confirm selection",
                                ["DE"] = "Auswahl bestätigen"
                            },
                            HotKey = Key.Enter
                        },
                        new cHotKeyInformation()
                        {
                            Names = new C4IT.MultiLanguage.cMultiLanguageDictionary()
                            {
                                ["EN"] = "Cancel search",
                                ["DE"] = "Suche abbrechen"
                            },
                            HotKey = Key.Escape
                        }
                    }
                };
                hotKeyCategories.Add(initialSearchCategory);

                var slimViewCategory = new cHotKeyCategoryInformation()
                {
                    Names = new C4IT.MultiLanguage.cMultiLanguageDictionary()
                    {
                        ["EN"] = "Slim View",
                        ["DE"] = "Slim View"
                    },
                    HotKeyInformation = new List<cHotKeyInformation>()
                    {
                                                new cHotKeyInformation()
                        {
                            Names = new C4IT.MultiLanguage.cMultiLanguageDictionary()
                            {
                                ["EN"] = "Open Details Page",
                                ["DE"] = "Details Page öffnen"
                            },
                            HotKey = Key.Enter
                        }
                    }
                };
                hotKeyCategories.Add(slimViewCategory);

                var detailsPageCategory = new cHotKeyCategoryInformation()
                {
                    Names = new C4IT.MultiLanguage.cMultiLanguageDictionary()
                    {
                        ["EN"] = "Details Page",
                        ["DE"] = "Details Page"
                    },
                    HotKeyInformation = new List<cHotKeyInformation>()
                    {
                        new cHotKeyInformation()
                        {
                            Names = new C4IT.MultiLanguage.cMultiLanguageDictionary()
                            {
                                ["EN"] = "Open previous/next category",
                                ["DE"] = "Vorherige/nächste Kategorie öffnen"
                            },
                            HotKey = Key.Up,
                            AlternativeKey = Key.Down
                        },
                        new cHotKeyInformation()
                        {
                            Names = new C4IT.MultiLanguage.cMultiLanguageDictionary()
                            {
                                ["EN"] = "Fold in/out history",
                                ["DE"] = "Historie ein-/ausklappen"
                            },
                            HotKey = Key.Left,
                            AlternativeKey = Key.Right
                        },
                        new cHotKeyInformation()
                        {
                            Names = new C4IT.MultiLanguage.cMultiLanguageDictionary()
                            {
                                ["EN"] = "Opens/closes all categories at once",
                                ["DE"] = "Alle Kateogrien auf einmal öffnen/schließen"
                            },
                            HotKey = Key.OemPlus,
                            AlternativeKey = Key.OemMinus
                        },
                        new cHotKeyInformation()
                        {
                            Names = new C4IT.MultiLanguage.cMultiLanguageDictionary()
                            {
                                ["EN"] = "Open Quick Action menu",
                                ["DE"] = "Quick Action Menü öffnen"
                            },
                            HotKey = Key.Q,
                        },
                        new cHotKeyInformation()
                        {
                            Names = new cMultiLanguageDictionary()
                            {
                                ["EN"] = "Reset zoom",
                                ["DE"] = "Zoom zurücksetzen"
                            },
                            HotKey = Key.NumPad0,
                            Modifiers = new List<ModifierKeys>() { ModifierKeys.Control }
                        }
                    }
                };
                hotKeyCategories.Add(detailsPageCategory);

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

        private void DrawHotKeyCategories(List<cHotKeyCategoryInformation> HotKeyCategories)
        {
            try
            {
                const int keyFontSize = 11;
                ContentPanel.Children.Clear();

                if (HotKeyCategories is null)
                    return;

                foreach (var category in HotKeyCategories)
                {
                    TextBlock titleTextBlock = new TextBlock() { Text = category.Names.GetValue() };
                    ContentPanel.Children.Add(titleTextBlock);

                    Border categoryBorder = new Border() { CornerRadius = new CornerRadius(7.5), Margin = new Thickness(5), Padding = new Thickness(10, 5, 10, 5), Style = null };
                    categoryBorder.SetResourceReference(BackgroundProperty, "BackgroundColor.Menu.MainCategory");


                    Grid hotKeyGrid = new Grid();
                    hotKeyGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(250) });
                    hotKeyGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) });

                    for (int i = 0; i < category.HotKeyInformation.Count; i++)
                    {
                        var hotKeyInfo = category.HotKeyInformation[i];

                        hotKeyGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) });

                        TextBlock hotKeyNameTextBlock = new TextBlock() { Text = hotKeyInfo.Names.GetValue(), FontWeight = FontWeights.Regular };
                        hotKeyNameTextBlock.SetResourceReference(ForegroundProperty, "FontColor.Menu.Categories");
                        Grid.SetColumn(hotKeyNameTextBlock, 0);
                        Grid.SetRow(hotKeyNameTextBlock, i);
                        hotKeyGrid.Children.Add(hotKeyNameTextBlock);

                        StackPanel hotKeyPanel = new StackPanel() { Orientation = Orientation.Horizontal, HorizontalAlignment = HorizontalAlignment.Center };

                        foreach (var modifier in hotKeyInfo.Modifiers)
                        {
                            Border modifierKeyBorder = new Border();
                            string modifierText = modifier.Equals(ModifierKeys.Control) ? cMultiLanguageSupport.GetItem("Global.KeyBoard.Ctrl") : modifier.ToString();

                            modifierKeyBorder.Child = new TextBlock() { Text = modifierText, TextAlignment = TextAlignment.Center, FontSize = keyFontSize, Margin = new Thickness(0), TextWrapping = TextWrapping.NoWrap, Foreground = (SolidColorBrush)new BrushConverter().ConvertFrom("#3D3C3C") };
                            hotKeyPanel.Children.Add(modifierKeyBorder);

                            TextBlock plusTextBlock = new TextBlock() { Text = "+", Margin = new Thickness(10, 0, 10, 0), FontWeight = FontWeights.Regular };
                            hotKeyPanel.Children.Add(plusTextBlock);
                        }

                        Border keyBorder = new Border();
                        keyBorder.Child = new TextBlock() { Text = hotKeyInfo.GetKeyDisplayString(hotKeyInfo.HotKey), TextAlignment = TextAlignment.Center, FontSize = keyFontSize, Margin = new Thickness(0), TextWrapping = TextWrapping.NoWrap, Foreground = (SolidColorBrush)new BrushConverter().ConvertFrom("#3D3C3C") };
                        hotKeyPanel.Children.Add(keyBorder);

                        if (hotKeyInfo.AlternativeKey != null)
                        {
                            TextBlock alternativeTextBlock = new TextBlock() { Text = "/", Margin = new Thickness(10, 0, 10, 0), FontWeight = FontWeights.Regular };
                            hotKeyPanel.Children.Add(alternativeTextBlock);

                            Border alternativeKeyBorder = new Border();
                            alternativeKeyBorder.Child = new TextBlock() { Text = hotKeyInfo.GetKeyDisplayString(hotKeyInfo.AlternativeKey.Value), FontSize = keyFontSize, TextAlignment = TextAlignment.Center, Margin = new Thickness(0), TextWrapping = TextWrapping.NoWrap, Foreground = (SolidColorBrush)new BrushConverter().ConvertFrom("#3D3C3C") };
                            hotKeyPanel.Children.Add(alternativeKeyBorder);
                        }

                        Grid.SetColumn(hotKeyPanel, 1);
                        Grid.SetRow(hotKeyPanel, i);
                        hotKeyGrid.Children.Add(hotKeyPanel);
                    }

                    categoryBorder.Child = hotKeyGrid;

                    ContentPanel.Children.Add(categoryBorder);
                }

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

        #region CloseButton

        private void CloseButton_Click()
        {
            Close();
        }

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

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

        #endregion
    }
}
