using MahApps.Metro.Controls;
using MySql.Data.MySqlClient;
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.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ZKuP
{
    /// <summary>
    /// Interaktionslogik für CreateMcFitUser.xaml
    /// </summary>
    public partial class CreateMcFitUser : MetroWindow
	{
        public CreateMcFitUser()
        {
            InitializeComponent();
            gridMcFitDisclaimer.Visibility = Visibility.Visible;
        }

        private void btnAccept_Click(object sender, RoutedEventArgs e)
        {
            gridMcFitDisclaimer.Visibility = Visibility.Collapsed;
        }

        private void btnDecline_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private async void btnAddUser_Click(object sender, RoutedEventArgs e)
        {
            if (tbVorname.Text != "" &&
                tbNachname.Text != "" &&
                tbAbteilung.Text != "" &&
                tbHandynr.Text != "" &&
                tbEmail.Text != "")
            {
                //await SQL.WriteSQL($"INSERT INTO {MainWindow.table}.mcFitUsers (Vorname, Name, Abteilung, Handynr, Email) VALUES ('{tbVorname.Text}','{tbNachname.Text}','{tbAbteilung.Text}','{tbHandynr.Text}','{tbEmail.Text}')");
                await SQL.CreateAndWriteSQL($"INSERT INTO {MainWindow.table}.mcFitUsers (Vorname, Name, Abteilung, Handynr, Email) VALUES (@tbVorname,@tbNachname,@tbAbteilung,@tbHandynr,@tbEmail)", new List<MySqlParameter>()
                    {
                        new MySqlParameter("@tbVorname", tbVorname.Text),
                        new MySqlParameter("@tbNachname", tbNachname.Text),
                        new MySqlParameter("@tbAbteilung", tbAbteilung.Text),
                        new MySqlParameter("@tbHandynr", tbHandynr.Text),
                        new MySqlParameter("@tbEmail", tbEmail.Text)
                    });

                var exist = await SQL.RowExists("mcFitUsers", "Email", tbEmail.Text);

                if (exist)
                {
                    MessageBox.Show($"Der User für McFit:\n\n{tbVorname.Text} {tbNachname.Text}\n\nwurde erfolgreich erstellt", "User erfolgreich erstellt", MessageBoxButton.OK, MessageBoxImage.Information);
                    this.Close();
                }
                else
                {
                    MessageBox.Show($"Der User für McFit:\n\n{tbVorname.Text} {tbNachname.Text}\n\nkonnte nicht erstellt werden\nBitte in einigen Minuten nochmal probieren\nBei fortlaufenden Problemen bitte an Marcus Bachler wenden", "User erfolgreich erstellt", MessageBoxButton.OK, MessageBoxImage.Error);
                    this.Close();
                }
            }
            else
            {
                MessageBox.Show("Alle Felder müssen ausgefüllt werden", "Fehler", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
        }

        private void tbAbteilung_TextChanged(object sender, TextChangedEventArgs e)
        {

        }


        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Helper.CheckWindowIsInScreenSpace(this);
        }

        private void Window_LocationChanged(object sender, EventArgs e)
        {
            Helper.CheckWindowIsInScreenSpace(this);
        }
    }
}
