using BuechermarktClient.Models;
using MongoDB.Driver;
using System;
using System.ComponentModel;
using System.Threading;
using System.Windows;

namespace BuechermarktClient
{
   /// <summary>
   /// Interaktionslogik für BookTypesEdit.xaml
   /// </summary>
   public partial class StudentsEdit : Window, INotifyPropertyChanged
   {
      private bool New = false;
      private Student Student = null;
      public string Forname
      {
         get
         {
            return Student.Forname;
         }
         set {
            if(value != Student.Forname)
            {
               Student.Forname = value;
               OnPropertyChanged("Forname");
            }
         }
      }

      public string Lastname
      {
         get
         {
            return Student.Lastname;
         }
         set
         {
            if (value != Student.Lastname)
            {
               Student.Lastname = value;
               OnPropertyChanged("Lastname");
            }
         }
      }

      public string EMail
      {
         get
         {
            return Student.EMail;
         }
         set
         {
            if (value != Student.EMail)
            {
               Student.EMail = value;
               OnPropertyChanged("EMail");
            }
         }
      }

      public string PhoneNumber
      {
         get
         {
            return Student.PhoneNumber;
         }
         set
         {
            if (value != Student.PhoneNumber)
            {
               Student.PhoneNumber = value;
               OnPropertyChanged("PhoneNumber");
            }
         }
      }

      public string ID
      {
         get
         {
            return Student.ID.ToString();
         }
      }

      public string LabelId
      {
         get
         {
            return Student.LabelId;
         }
         set
         {
            if (value != Student.LabelId)
            {
               Student.LabelId = value;
               OnPropertyChanged("LabelId");
            }
         }
      }

      public string Form
      {
         get
         {
            return Student.Form;
         }
         set
         {
            if (value != Student.Form)
            {
               Student.Form = value;
               OnPropertyChanged("Form");
            }
         }
      }

      public event PropertyChangedEventHandler PropertyChanged;
      protected void OnPropertyChanged(string propertyName)
      {
         PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
      }

      public StudentsEdit(Student student)
      {
         DataContext = this;
         if(student == null)
         {
            Student = new Student();
            New = true;
         } else
         {
            Student = student;
         }
         InitializeComponent();
      }

      private void Save_Click(object sender, RoutedEventArgs e)
      {
         while (true)
         {
            try
            {
               if (New)
               {
                  MainWindow.StudentCollection.InsertOne(Student);
                  Student = new Student();
                  Forname = "";
                  Lastname = "";
                  EMail = "";
                  PhoneNumber = "";
                  LabelId = "";
                  Form = "";
               }
               else
               {
                  MainWindow.StudentCollection.FindOneAndUpdate(s => s.ID == Student.ID, Builders<Student>.Update
                     .Set(s => s.Forname, Student.Forname)
                     .Set(s => s.Lastname, Student.Lastname)
                     .Set(s => s.EMail, Student.EMail)
                     .Set(s => s.PhoneNumber, Student.PhoneNumber)
                     .Set(s => s.LabelId, Student.LabelId)
                     .Set(s => s.Form, Student.Form));

                  Close();
               }
               return;
            } catch (MongoExecutionTimeoutException)
            {
               Thread.Sleep(100);
            }
         }
      }

      private void Delete_Click(object sender, RoutedEventArgs e)
      {
         while (true)
         {
            try
            {
               if (Student.ID != null)
               {
                  MainWindow.StudentCollection.DeleteOne(bt => bt.ID == Student.ID);
               }
               Close();
               return;
            }
            catch (MongoExecutionTimeoutException)
            {
               Thread.Sleep(100);
            }
         }
      }
   }
}
