using C4IT.MultiLanguage;
using FasdDesktopUi.Basics;
using FasdDesktopUi.Pages.SearchPage;
using System;
using System.Collections.Generic;
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.Forms;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ContextMenu = System.Windows.Controls.ContextMenu;
using MenuItem = System.Windows.Controls.MenuItem;
using MessageBox = System.Windows.MessageBox;

namespace FasdDesktopUi.Pages.DesktopWidgetPage
{
    /// <summary>
    /// Interaction logic for DesktopWidgetPageView.xaml
    /// </summary>
    public partial class DesktopWidgetPageView : Window
    {

        public bool IsWindowVisible
        {
            get { return (bool)GetValue(IsWindowVisibleProperty); }
            set { SetValue(IsWindowVisibleProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsWindowVisible.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsWindowVisibleProperty =
            DependencyProperty.Register(nameof(IsWindowVisible), typeof(bool), typeof(DesktopWidgetPageView), new PropertyMetadata(true));


        public bool AreAllWidgetsVisible
        {
            get { return (bool)GetValue(AreAllWidgetsVisibleProperty); }
            set { SetValue(AreAllWidgetsVisibleProperty, value); }
        }

        // Using a DependencyProperty as the backing store for AreAllWidgetsVisible.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty AreAllWidgetsVisibleProperty =
            DependencyProperty.Register(nameof(AreAllWidgetsVisible), typeof(bool), typeof(DesktopWidgetPageView), new PropertyMetadata(false));



        public bool ShowMainNotification
        {
            get { return (bool)GetValue(ShowMainNotificationProperty); }
            set { SetValue(ShowMainNotificationProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ShowMainNotification.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ShowMainNotificationProperty =
            DependencyProperty.Register(nameof(ShowMainNotification), typeof(bool), typeof(DesktopWidgetPageView), new PropertyMetadata(true));




        public DesktopWidgetPageView()
        {
            InitializeComponent();

            SourceInitialized += (s, e) =>
            {
                IntPtr handle = new WindowInteropHelper(this).Handle;
                HwndSource.FromHwnd(handle).AddHook(new HwndSourceHook(cUtility.WindowProc));
            };
        }

        private bool IsAnyOtherVisible() => cSupportCaseDataProvider.detailsPage.IsVisible || SearchPageView.Instance.IsVisible;
        protected override void OnInitialized(EventArgs e)
        {


            cSupportCaseDataProvider.detailsPage.IsVisibleChanged += (sender, args) =>
            {
                this.Visibility = IsAnyOtherVisible() ? Visibility.Collapsed : Visibility.Visible;
            };

            SearchPageView.Instance.IsVisibleChanged += (sender, args) =>
            {

                this.Visibility = IsAnyOtherVisible() ? Visibility.Collapsed : Visibility.Visible;
            };


            base.OnInitialized(e);
        }

        private void F4SDWidget_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            AreAllWidgetsVisible = !AreAllWidgetsVisible;
            ShowMainNotification = !ShowMainNotification;
        }

        private void SearchWidget_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            SearchPageView.Instance.ActivateSearchView();
        }

        private void SettingsWidget_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            ContextMenu menu = SetupSettingsContextMenu();
            SettingsWidget.ContextMenu = menu;
            SettingsWidget.ContextMenu.IsOpen = true;
        }

        private ContextMenu SetupSettingsContextMenu()
        {
            ContextMenu menu = new ContextMenu();

            MenuItem languageItem = new MenuItem();
            languageItem.Header = cMultiLanguageSupport.GetItem("Menu.SelectLanguage");
            languageItem.Items.Add(new MenuItem { Header = "DE" });
            languageItem.Items.Add(new MenuItem { Header = "EN" });
            menu.Items.Add(languageItem);

            MenuItem optionsItem = new MenuItem();
            optionsItem.Header = cMultiLanguageSupport.GetItem("Menu.Options");
            optionsItem.Click += (s, e) => MessageBox.Show(cMultiLanguageSupport.GetItem("Menu.Options"));
            menu.Items.Add(optionsItem);

            MenuItem m42Item = new MenuItem();
            m42Item.Header = cMultiLanguageSupport.GetItem("M42Settings.SystemTray");
            m42Item.Click += (s, e) => MessageBox.Show(cMultiLanguageSupport.GetItem("M42Settings.SystemTray"));
            menu.Items.Add(m42Item);

            MenuItem phoneItem = new MenuItem();
            phoneItem.Header = cMultiLanguageSupport.GetItem("PhoneSettings.SystemTray");
            phoneItem.Click += (s, e) => MessageBox.Show(cMultiLanguageSupport.GetItem("PhoneSettings.SystemTray"));
            menu.Items.Add(phoneItem);

            return menu;
        }

        private void ShowMessageBox(string message)
        {
            MessageBox.Show(message);
        }

        private void RestartWidget_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            ContextMenu menu = SetupRestartContextMenu();
            RestartWidget.ContextMenu = menu;
            RestartWidget.ContextMenu.IsOpen = true;
        }

        private ContextMenu SetupRestartContextMenu()
        {
            ContextMenu menu = new ContextMenu();

            MenuItem restartItem = new MenuItem();
            restartItem.Header = cMultiLanguageSupport.GetItem("Menu.Restart");
            restartItem.Click += (s, e) => MessageBox.Show(cMultiLanguageSupport.GetItem("Menu.Restart"));
            menu.Items.Add(restartItem);

            MenuItem quitItem = new MenuItem();
            quitItem.Header = cMultiLanguageSupport.GetItem("Menu.Quit");
            quitItem.Click += (s, e) => MessageBox.Show(cMultiLanguageSupport.GetItem("Menu.Quit"));
            menu.Items.Add(quitItem);

            return menu;
        }

        private void TicketNotifyWidget_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            MessageBox.Show("Dies wurde leider noch nicht implementiert, sollte dich das stören setze es doch gerne selber um.");
        }

    }
}
