using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using BuechermarktClient.Models;
using System.Threading;
using MongoDB.Driver;
using System.Collections.Generic;

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

      public bool Chooser = false;

      private string _SearchField;
      public string SearchField {
         get
         {
            return _SearchField;
         }
         set
         {
            if(value != _SearchField)
            {
               _SearchField = value;
            }
         }
      }

      public BookType SelectedItem { get; private set; }

      public BookTypes() : this(false) { }

      public BookTypes(bool select)
      {
         DataContext = this;
         Chooser = select;
         InitializeComponent();
         RefreshThread = new Thread(RefreshThreadS);
         RefreshThread.Start();
         RefreshThread.IsBackground = true;
         Closing += BookTypes_Closing;
      }

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

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

      public void LoadList()
      {
         List<Models.BookType> list = null;
         var not = true;
         while (not)
         {
            try
            {
               list = MainWindow.BookTypeCollection.FindSync((b) => true).ToList();
               not = false;
            }
            catch (MongoExecutionTimeoutException)
            {
               Thread.Sleep(200);
            }
         }

         var show = new List<BookType>();
         if(SearchField != null && SearchField != String.Empty)
         {
            if (SearchField == "%d%")
            {
               foreach (var e in list)
               {
                  foreach (var e2 in list)
                  {
                     if (e.ID == e2.ID) continue;
                     if (e.ISBN == e2.ISBN) show.Add(e);
                  }
               }
            }
            else
            {
               foreach (var e in list)
               {
                  if (e.ISBN.ToLower().Contains(SearchField.ToLower()) || e.Name.ToLower().Contains(SearchField.ToLower()))
                  {
                     show.Add(e);
                  }
               }
            }
         } else {
            show = list;
         }
         Dispatcher.BeginInvoke(new Action(delegate (){
            BookTypesList.ItemsSource = show;
         }));
      }

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

      private void ListViewItem_PreviewMouseUp(object sender, MouseButtonEventArgs e)
      {
         if (sender is ListViewItem item && item.IsSelected)
         {
            SelectedItem = item.DataContext as BookType;
            if (Chooser)
            {
               DialogResult = true;
               Close();
            }
            else
            {
               var editWindow = new BookTypesEdit(item.DataContext as BookType)
               {
                  Owner = this
               };
               editWindow.Show();
            }
         }
      }

      private void Search_Click(object sender, RoutedEventArgs e)
      {
         LoadList();
      }
   }
}
