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.Navigation;
using System.Windows.Shapes;
using Xceed.Wpf.Toolkit;
using Client.EHEC_Service;
using AutoMapper;


namespace Client
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        
        public MainWindow()
        {
            InitializeComponent();
            // Start Automapper
            Mapper.Initialize(cfg => cfg.AddProfile<AutomapperProfile>());
            // loading of dropdowns
            ComboBoxBakterienstamm.ItemsSource = Global.GlobalInstance.Service.GetResults();
            ComboBoxBakterienstamm.DisplayMemberPath = "Name";
            // check if Server(ping) answers throug a invoke of ButtonOnline_Click
            ButtonOnline.RaiseEvent(new RoutedEventArgs(Button.ClickEvent)); 

        }


        private void ButtonClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
        private void ButtonSend_Click(object sender, RoutedEventArgs e)
        {
            
            LabelRueckmeldungsfeld.Content = "Sende Daten. ";
            
            Doctor newDoctor = new Doctor(TextboxArztVorname.Text,
                                          TextboxArztName.Text,
                                          TextboxArztStrasseNr.Text,
                                          TextboxArztPlz.Text,
                                          TextboxArztOrt.Text,
                                          TextboxArztRegion.Text,
                                          TextboxArztKantonsarzt.Text
                                          );

            
            Patient newPatient = new Patient(TextboxPatientVorname.Text,
                                             TextboxPatientName.Text,
                                             TextboxPatientStrasseNr.Text,
                                             TextboxPatientPlz.Text,
                                             TextboxPatientOrt.Text,
                                             TextboxPatientRegion.Text,
                                             TextboxPatientGeburtstag.Text
                                             );
            

            Result newResult = new Result(ComboBoxBakterienstamm.Text);

            LabelRueckmeldungsfeld.Content += "erstelle Doktor, ";
            newDoctor.CreateDoctor();
            LabelArztId.Content = Convert.ToString(newDoctor.Id);

            LabelRueckmeldungsfeld.Content += "erstelle Patient, ";
            newPatient.CreatePatient();
            LabelPatientId.Content = Convert.ToString(newPatient.Id);

            LabelRueckmeldungsfeld.Content += "erstelle Bakterie, ";
            newResult.CreateResult();
            LabelBakterienstamm.Content = Convert.ToString(newResult.Id);

            Exam newExam = new Exam(newDoctor.Id,
                                    newPatient.Id,
                                    newResult.Id
                                    );
            LabelRueckmeldungsfeld.Content += "erstelle Untersuchung, ";
            newExam.CreateExam();
            

            Origin newOrigin1 = new Origin(TextboxNahrung1Restaurant.Text,
                                           TextboxNahrung1Essen.Text,
                                           TextboxNahrung1Strasse.Text,
                                           Convert.ToInt32(TextboxNahrung1Plz.Text),
                                           TextboxNahrung1Ort.Text
                                           );
            
            Origin newOrigin2 = new Origin(TextboxNahrung2Restaurant.Text,
                                           TextboxNahrung2Essen.Text,
                                           TextboxNahrung2Strasse.Text,
                                           Convert.ToInt32(TextboxNahrung2Plz.Text),
                                           TextboxNahrung2Ort.Text
                                           );
            
            Origin newOrigin3 = new Origin(TextboxNahrung3Restaurant.Text,
                                           TextboxNahrung3Essen.Text,
                                           TextboxNahrung3Strasse.Text,
                                           Convert.ToInt32(TextboxNahrung3Plz.Text),
                                           TextboxNahrung3Ort.Text
                                           );
            
            Origin newOrigin4 = new Origin(TextboxNahrung4Restaurant.Text,
                                           TextboxNahrung4Essen.Text,
                                           TextboxNahrung4Strasse.Text,
                                           Convert.ToInt32(TextboxNahrung4Plz.Text),
                                           TextboxNahrung4Ort.Text
                                           );
            
            LabelRueckmeldungsfeld.Content += "erstelle Nahrungsaufnahmen.";

            newOrigin1.CreateOrigin();
            LabelNahrung1Id.Content = Convert.ToString(newOrigin1.Id);

            newOrigin2.CreateOrigin();
            LabelNahrung2Id.Content = Convert.ToString(newOrigin2.Id);

            newOrigin3.CreateOrigin();
            LabelNahrung3Id.Content = Convert.ToString(newOrigin3.Id);

            newOrigin4.CreateOrigin();
            LabelNahrung4Id.Content = Convert.ToString(newOrigin4.Id);

            Origin_Exam newOrigin_Exam1 = new Origin_Exam(newOrigin1.Id,
                                                         newExam.Id
                                                         );

            Origin_Exam newOrigin_Exam2 = new Origin_Exam(newOrigin2.Id,
                                                         newExam.Id
                                                         );

            Origin_Exam newOrigin_Exam3 = new Origin_Exam(newOrigin3.Id,
                                                         newExam.Id
                                                         );

            Origin_Exam newOrigin_Exam4 = new Origin_Exam(newOrigin4.Id,
                                                         newExam.Id
                                                         );

            LabelRueckmeldungsfeld.Content += "Verlinke Nahrungsaufnahmen mit der Untersuchungsnummer.";
            newOrigin_Exam1.CreateOrigin_Exam();
            newOrigin_Exam2.CreateOrigin_Exam();
            newOrigin_Exam3.CreateOrigin_Exam();
            newOrigin_Exam4.CreateOrigin_Exam();
            LabelRueckmeldungsfeld.Content = "Fertig. Behandlung Nr." + Convert.ToString(newExam.Id) + " gespeichert.";
            CleanExam();
        }

        private void CleanExam()
        {
            TextboxPatientName.Clear();
            TextboxPatientVorname.Clear();
            TextboxPatientStrasseNr.Clear();
            TextboxPatientPlz.Clear();
            TextboxPatientOrt.Clear();
            TextboxPatientRegion.Clear();
            TextboxPatientGeburtstag.Clear();
            ComboBoxBakterienstamm.Items.Clear();
            TextboxNahrung1Restaurant.Clear();
            TextboxNahrung1Essen.Clear();
            TextboxNahrung1Strasse.Clear();
            TextboxNahrung1Plz.Clear();
            TextboxNahrung1Ort.Clear();
            TextboxNahrung2Restaurant.Clear();
            TextboxNahrung2Essen.Clear();
            TextboxNahrung2Strasse.Clear();
            TextboxNahrung2Plz.Clear();
            TextboxNahrung2Ort.Clear();
            TextboxNahrung3Restaurant.Clear();
            TextboxNahrung3Essen.Clear();
            TextboxNahrung3Strasse.Clear();
            TextboxNahrung3Plz.Clear();
            TextboxNahrung3Ort.Clear();
            TextboxNahrung4Restaurant.Clear();
            TextboxNahrung4Essen.Clear();
            TextboxNahrung4Strasse.Clear();
            TextboxNahrung4Plz.Clear();
            TextboxNahrung4Ort.Clear();
        }
        private void ButtonAutogenerateOne_Click(object sender, RoutedEventArgs e)
        {
            TextboxArztName.Text = new Bogus.DataSets.Name("de_CH").LastName();
            TextboxArztVorname.Text = new Bogus.DataSets.Name("de_CH").FirstName();
            TextboxArztStrasseNr.Text = new Bogus.DataSets.Address("de_CH").StreetName();
            TextboxArztPlz.Text = new Bogus.DataSets.Address("de_CH").ZipCode();
            TextboxArztOrt.Text = new Bogus.DataSets.Address("de_CH").City();
            TextboxArztKantonsarzt.Text = Testdata.RandomEnumValue<Testdata.Doctortyp>().ToString();
            TextboxArztRegion.Text = new Bogus.DataSets.Address("de_CH").State();
            TextboxPatientName.Text = new Bogus.DataSets.Name("de_CH").LastName();
            TextboxPatientVorname.Text = new Bogus.DataSets.Name("de_CH").FirstName();
            TextboxPatientStrasseNr.Text = new Bogus.DataSets.Address("de_CH").StreetName();
            TextboxPatientPlz.Text = new Bogus.DataSets.Address("de_CH").ZipCode();
            TextboxPatientOrt.Text = new Bogus.DataSets.Address("de_CH").City();
            TextboxPatientRegion.Text = new Bogus.DataSets.Address("de_CH").State();
            TextboxPatientGeburtstag.Text = new Bogus.DataSets.Date().Past().ToString();
            ComboBoxBakterienstamm.Text = "bacteriaName";
            TextboxNahrung1Restaurant.Text = Testdata.RandomEnumValue<Testdata.RestaurantPre>().ToString() + 
                                      " " + Testdata.RandomEnumValue<Testdata.RestaurantName>().ToString();
            TextboxNahrung1Essen.Text = Testdata.RandomEnumValue<Testdata.Vegie>().ToString() +
                                        " " + Testdata.RandomEnumValue<Testdata.Vegie>().ToString();
            TextboxNahrung1Strasse.Text = new Bogus.DataSets.Address("de_CH").StreetName();
            TextboxNahrung1Plz.Text = new Bogus.DataSets.Address("de_CH").ZipCode();
            TextboxNahrung1Ort.Text = new Bogus.DataSets.Address("de_CH").City();
            TextboxNahrung2Restaurant.Text = Testdata.RandomEnumValue<Testdata.RestaurantPre>().ToString() +
                                      " " + Testdata.RandomEnumValue<Testdata.RestaurantName>().ToString();
            TextboxNahrung2Essen.Text = Testdata.RandomEnumValue<Testdata.Vegie>().ToString() +
                                        " " + Testdata.RandomEnumValue<Testdata.Vegie>().ToString();
            TextboxNahrung1Strasse.Text = new Bogus.DataSets.Address("de_CH").StreetName();
            TextboxNahrung2Plz.Text = new Bogus.DataSets.Address("de_CH").ZipCode();
            TextboxNahrung2Ort.Text = new Bogus.DataSets.Address("de_CH").City();
            TextboxNahrung3Restaurant.Text = Testdata.RandomEnumValue<Testdata.RestaurantPre>().ToString() +
                                        " " + Testdata.RandomEnumValue<Testdata.RestaurantName>().ToString();
            TextboxNahrung3Essen.Text = Testdata.RandomEnumValue<Testdata.Vegie>().ToString() +
                                        " " + Testdata.RandomEnumValue<Testdata.Vegie>().ToString();
            TextboxNahrung3Strasse.Text = new Bogus.DataSets.Address("de_CH").StreetName();
            TextboxNahrung3Plz.Text = new Bogus.DataSets.Address("de_CH").ZipCode();
            TextboxNahrung3Ort.Text = new Bogus.DataSets.Address("de_CH").City();
            TextboxNahrung4Restaurant.Text = Testdata.RandomEnumValue<Testdata.RestaurantPre>().ToString() +
                                        " " + Testdata.RandomEnumValue<Testdata.RestaurantName>().ToString();
            TextboxNahrung4Essen.Text = Testdata.RandomEnumValue<Testdata.Vegie>().ToString() +
                                        " " + Testdata.RandomEnumValue<Testdata.Vegie>().ToString();
            TextboxNahrung4Strasse.Text = new Bogus.DataSets.Address("de_CH").StreetName();
            TextboxNahrung4Plz.Text = new Bogus.DataSets.Address("de_CH").ZipCode();
            TextboxNahrung4Ort.Text = new Bogus.DataSets.Address("de_CH").City();
        }
        private void ButtonAutogenerateMany_Click(object sender, RoutedEventArgs e)
        {

        }

        private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            
        }

        private void ButtonOnline_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string con = Global.GlobalInstance.Service.Ping();
                if (con == "pong")
                {
                    ButtonOnline.Content = "online";
                    ButtonOnline.Foreground = Brushes.Green;
                }
            }
            catch (Exception)
            {
                ButtonOnline.Content = "offline";
                ButtonOnline.Foreground = Brushes.Red;
            }
        }
    }




    // aufbewahren für die integration von Id's in die FK Tabellenspalten:
    //try {
    //    Global.GlobalInstance.Service.WritePatient(p);
    //    //servize.WritePatient(p);
    //} catch (Exception) {
    //    if (p.PatientId == 0) {
    //        LabelRueckmeldungsfeld.Content = "fehler id = 0, beim schreiben von Patient!";
    //    } else {
    //        LabelRueckmeldungsfeld.Content = "fehler id != 0, beim schreiben von Patient!";
    //    }
    //}
    //LabelRueckmeldungsfeld.Content = "erfolgreich Patient gespeichert";
    //TextboxPatientName.Clear();


    //private void TextChanged(object sender, KeyboardFocusChangedEventArgs e)
    //{
    //    if (((TextBox)sender).Text.Length == 0)
    //        ((TextBox)sender).Text = ((TextBox)sender).Text.ToString();
    //}

    //private void TextInputs(object sender, EventArgs e)
    //{
    //    if ((((TextBox)sender).Text) == (((TextBox)sender).Text))
    //        ((TextBox)sender).Text = string.Empty;
    //        ((TextBox)sender).SelectionStart = 1;
    //}

    //private void TextboxArztName_LostFocus(object sender, KeyboardFocusChangedEventArgs e) => TextChanged(sender, e);

    //private void TextboxArztName_MouseDown(object sender, MouseButtonEventArgs e)
    //{
    //    TextInputs(sender, e);
    //    //TextboxArztName.Text = string.Empty;
    //}
    //private void TextboxArztName_PreviewTextInput(object sender, TextCompositionEventArgs e)
    //{
    //    TextInputs(sender, e);
    //}


}
