using Elwig.Helpers;
using System;
using System.ComponentModel;
using System.Reflection;
using System.Windows;
using System.Windows.Input;

namespace Elwig.Windows {
    public partial class MainWindow : Window {

        public MainWindow() {
            InitializeComponent();
            var v = Assembly.GetExecutingAssembly().GetName().Version;
            VersionField.Text = "Version: " + (v == null ? "?" : $"{v.Major}.{v.Minor}.{v.Build}") + $" – {App.BranchName}";
            if (App.Client.Client == null) VersionField.Text += " (Unbekannt)";
            if (!App.Config.Debug) {
                HelpMenu.Items.Remove(Menu_Help_TestWindow);
                //QueryWindowButton.Visibility = Visibility.Hidden;
            }
            if (App.Config.UpdateUrl == null) Menu_Help_Update.IsEnabled = false;
            if (App.Config.Smtp == null) Menu_Help_Smtp.IsEnabled = false;
        }

        private void Window_Loaded(object sender, RoutedEventArgs evt) { }

        private void Window_Closing(object sender, CancelEventArgs evt) {
            if (App.NumWindows > 1 && !App.ForceShutdown) {
                var res = MessageBox.Show("Es sind noch weitere Fenster geöffnet.\nSollen alle Fenster geschlossen werden?",
                    "Elwig beenden", MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.No);
                if (res != MessageBoxResult.Yes) {
                    evt.Cancel = true;
                } else {
                    Application.Current.Shutdown();
                }
            }
        }

        private void Menu_Help_TestWindow_Click(object sender, RoutedEventArgs evt) {
            var w = new TestWindow();
            w.Show();
        }

        private async void Menu_Help_Update_Click(object sender, RoutedEventArgs evt) {
            await App.CheckForUpdates();
        }

        private async void Menu_Help_Smtp_Click(object sender, RoutedEventArgs evt) {
            Mouse.OverrideCursor = Cursors.AppStarting;
            try {
                using var client = await Utils.GetSmtpClient();
                await client!.DisconnectAsync(true);
                MessageBox.Show("E-Mail-Einstellungen erfolgreich überprüft!", "Erfolg", MessageBoxButton.OK, MessageBoxImage.Information);
            } catch (Exception exc) {
                MessageBox.Show(exc.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            Mouse.OverrideCursor = null;
        }

        private void Menu_Database_Query_Click(object sender, RoutedEventArgs evt) {
            var w = new QueryWindow();
            w.Show();
        }

        private void MemberAdminButton_Click(object sender, RoutedEventArgs evt) {
            var w = new MemberAdminWindow();
            w.Show();
        }

        private void ReceiptButton_Click(object sender, RoutedEventArgs evt) {
            App.FocusReceipt();
        }

        private void DeliveryAdminButton_Click(object sender, RoutedEventArgs evt) {
            var w = new DeliveryAdminWindow();
            w.Show();
        }

        private void BaseDataButton_Click(object sender, RoutedEventArgs evt) {
            App.FocusBaseData();
        }

        private void SeasonFinishButton_Click(object sender, RoutedEventArgs evt) {
            App.FocusSeasonFinish();
        }

        private void MailButton_Click(object sender, RoutedEventArgs evt) {
            App.FocusMailWindow();
        }
    }
}
