using Gremlin.GremlinData.DBClasses;
using Gremlin.GremlinUtilities;
using Gremlin.GremlinData.EntityClasses;
using Gremlin.GremlinUI.ViewModels;
using Gremlin.Models;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using static Gremlin.GremlinData.DBClasses.GremlinContext;

namespace Gremlin.GremlinUI
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            UpdateUI();
            //DbHelper.DbBuilder();
            //DbHelper.DbInitializer();
            //DbHelper.InsertTestDataIntoDB();
        }

        private void UpdateUI()
        {
            lblConnectionString.Content = $"server={Properties.Settings.Default.server};" +
                    $"port={Properties.Settings.Default.port};" +
                    $"database={Properties.Settings.Default.database};" +
                    $"user={Properties.Settings.Default.user};" +
                    $"password={Properties.Settings.Default.password};" +
                    $"SslMode={Properties.Settings.Default.SslMode};" +
                    $"SslCa={Properties.Settings.Default.SslCA}";

            string userName = RUSettingModel.GetSettingValue(Properties.Settings.Default.userSettingID, "userName");
            lblWelcome.Content = $"Welcome back to Gremlin, {userName}!";
        }

        private void BtnOpenQuote_Click(object sender, RoutedEventArgs e)
        {
            ///Öffnet Fenster zur Auswahl eines oder mehrerer zu prozessierender Angebote.
            OpenFileDialog dlg = new()
            {
                Multiselect = true,
                Filter = "Angebote|*.docx; *.xlsx"
            };

            if (dlg.ShowDialog() == true)
            {
                //Auswahl dlg.filenames an passende Objekte übergeben
                string message = dlg.FileNames[0];
                string caption = "Sie haben u.a. diese Dateien ausgewählt:";
                _ = MessageBox.Show(message, caption);
            }
        }

        private async void BtnImportAccounts_Click(object sender, RoutedEventArgs e)
        {
            //DbHelper.ImportAccountsFromCSV();
            _ = await DbHelper.ImportAccountsFromCsvAsync();
        }

        private async void BtnImportContacts_Click(object sender, RoutedEventArgs e)
        {
            //DbHelper.ImportContactsFromCSV();
            _ = await DbHelper.ImportContactsFromCsvAsync();
        }

        private async void BtnImportProducts_Click(object sender, RoutedEventArgs e)
        {
            //DbHelper.ImportProductsFromCSV();
            _ = await DbHelper.ImportProductsFromCsvAsync();
        }

        private async void BtnImportCDs_Click(object sender, RoutedEventArgs e)
        {
            _ = await DbHelper.ImportCustomDescriptionsFromDocxAsync();
        }

        private async void BtnImportLSAGListData_Click(object sender, RoutedEventArgs e)
        {
            //DbHelper.ImportLSAGContactListToolData();
            _ = await DbHelper.ImportLSAGContactListToolDataAsync();
        }

        private void BtnRefreshAccountList_Click(object sender, RoutedEventArgs e)
        {
            ObservableCollection<Account> accounts = new(AccountVM.GetAccounts());
            dg_Test.ItemsSource = accounts;
        }

        private void BtnChooseDb_Click(object sender, RoutedEventArgs e)
        {
            ChooseDB chooseDB = new();
            if ((bool)chooseDB.ShowDialog())
                UpdateUI();
        }

        private void BtnResetDb_Click(object sender, RoutedEventArgs e)
        {
            MessageBoxResult _userInput = MessageBox.Show("Soll die vorhandene Datenbank wirklich gelöscht werden?", "Datenbank-Reset bestätigen", MessageBoxButton.YesNo);
            if (_userInput == MessageBoxResult.Yes)
            {
                DbHelper.DbBuilder();
                DbHelper.DbInitializer();
                _ = MessageBox.Show($"DB wurde zurückgesetzt.");
            }
        }

        private void BtnSeedDb_Click(object sender, RoutedEventArgs e)
        {
            DbHelper.InsertTestDataIntoDB();
        }

        private async void BtnUpdateProductsFromCPL(object sender, RoutedEventArgs e)
        {
            _ = await DbHelper.UpdateProductsFromCsvAsync();
        }

        private async void BtnImportCDFromCSV_Click(object sender, RoutedEventArgs e)

        {
            _ = await DbHelper.ImportCustomDescriptionsFromCsvAsync();
        }

        private void BtnTest_Click(object sender, RoutedEventArgs e)
        {
            DataImport dataImport = new();
            dataImport.ShowDialog();
        }

        private void BtnQuoteUI_Click(object sender, RoutedEventArgs e)
        {
            QuoteUI quoteUI = new();
            _ = quoteUI.ShowDialog();
        }
    }
}
