using System;
using System.Linq;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using MongoDB.Driver;
using System.Windows.Data;
using System.Globalization;
using MongoDB.Bson;

namespace BuechermarktClient
{
   [ValueConversion(typeof(object), typeof(string))]
   public class EnumConverter : IValueConverter
   {
      public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
      {
         return value?.ToString();
      }

      public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
      {
         throw new NotImplementedException();
      }
   }

   [ValueConversion(typeof(object), typeof(string))]
   public class StudentConverter : IValueConverter
   {
      public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
      {
         var i = MainWindow.StudentCollection.Find(s => s.ID == (ObjectId)value).FirstOrDefault();
         if (i == null) return value;
         return i.Lastname + ", " + i.Forname;
      }

      public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
      {
         throw new NotImplementedException();
      }
   }

   [ValueConversion(typeof(object), typeof(string))]
   public class BookTypeConverter : IValueConverter
   {
      public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
      {
         var i = MainWindow.BookTypeCollection.Find(bt => bt.ID == (ObjectId)value).FirstOrDefault();
         if (i == null) return value;
         return i.Name;
      }

      public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
      {
         throw new NotImplementedException();
      }
   }

   /// <summary>
   /// Interaktionslogik für Books.xaml
   /// </summary>
   public partial class Books : Window
   {
      public Thread RefreshThread = null;
      private bool ThreadRunning = true;

      public Books()
      {
         InitializeComponent();
         RefreshThread = new Thread(RefreshThreadS);
         RefreshThread.Start();
         RefreshThread.IsBackground = true;
         Closing += Book_Closing;
      }

      private void Book_Closing(object sender, System.ComponentModel.CancelEventArgs e)
      {
         ThreadRunning = false;
      }

      public void RefreshThreadS()
      {
         while (ThreadRunning)
         {
            LoadList();
            Thread.Sleep(1000);
         }
      }

      public void LoadList()
      {
         var list = MainWindow.BookCollection.FindSync((b) => true).ToList();
         Dispatcher.BeginInvoke(new Action(delegate () {
            BooksList.ItemsSource = list;
         }));
      }

      private void AddNew_Click(object sender, RoutedEventArgs e)
      {
         var editWindow = new BooksEdit(null)
         {
            Owner = this
         };
         editWindow.Show();
      }

      private void ListViewItem_PreviewMouseUp(object sender, MouseButtonEventArgs e)
      {
         if (sender is ListViewItem item && item.IsSelected)
         {
            var editWindow = new BooksEdit(item.DataContext as Models.Book)
            {
               Owner = this
            };
            editWindow.Show();
         }
      }
   }
}
