using BuechermarktClient.Models;
using MongoDB.Driver;
using MongoDB.Driver.Core.Clusters;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
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;

namespace BuechermarktClient
{
   /// <summary>
   /// Interaktionslogik für MainWindow.xaml
   /// </summary>
   public partial class MainWindow : Window
   {
      public static MongoClient Mongo;
      public const string DatabaseName = "buechermarkt";
      public static string MongoHost = "localhost";
      public static IMongoCollection<BookType> BookTypeCollection
      {
         get {
            var database = Mongo.GetDatabase(DatabaseName);
            return database.GetCollection<BookType>("booktypes");
         }
      }
      public static IMongoCollection<Book> BookCollection
      {
         get
         {
            var database = Mongo.GetDatabase(DatabaseName);
            return database.GetCollection<Book>("books");
         }
      }
      public static IMongoCollection<Student> StudentCollection
      {
         get
         {
            var database = Mongo.GetDatabase(DatabaseName);
            return database.GetCollection<Student>("students");
         }
      }

      public BookTypes BookTypesWindow = null;
      public Students StudentsWindow = null;
      public Books BooksWindow = null;
      public Sell SellWindow = null;

      public MainWindow()
      {
         var s = new GetConnection();
         s.ShowDialog();
         InitializeComponent();
         OnConnectionEstablised();
         //ToDo get server informations
      }

      public void OnConnectionEstablised()
      {
         Mongo = new MongoClient("mongodb://" + MongoHost + ":27017");
         var t = new Thread(BackupService);
         t.Start();
      }

      private void BackupService()
      {
         while (true)
         {
            Backup.MakeBackup();
            Thread.Sleep(120000);
         }
      }

      private void Window_Loaded(object sender, RoutedEventArgs e)
      {
         
      }

      private void BookTypes_Click(object sender, RoutedEventArgs e)
      {
         if(BookTypesWindow == null)
         {
            BookTypesWindow = new BookTypes()
            {
               Owner = this
            };
            BookTypesWindow.Show();
            BookTypesWindow.Closed += BookTypesWindow_Closed;
         } else {
            BringWindowOnTop(BookTypesWindow); 
         }
      }

      private void BookTypesWindow_Closed(object sender, EventArgs e)
      {
         BookTypesWindow = null;
      }

      private void BringWindowOnTop(Window window)
      {
         if (!window.IsVisible)
         {
            window.Show();
         }

         if (window.WindowState == WindowState.Minimized)
         {
            window.WindowState = WindowState.Normal;
         }

         window.Activate();
         window.Topmost = true;  // important
         window.Topmost = false; // important
         window.Focus();         // important
      }

      private void Students_Click(object sender, RoutedEventArgs e)
      {
         if (StudentsWindow == null)
         {
            StudentsWindow = new Students()
            {
               Owner = this
            };
            StudentsWindow.Show();
            StudentsWindow.Closed += StudentsWindow_Closed;
         }
         else
         {
            BringWindowOnTop(StudentsWindow);
         }
      }

      private void StudentsWindow_Closed(object sender, EventArgs e)
      {
         StudentsWindow = null;
      }

      private void Books_Click(object sender, RoutedEventArgs e)
      {
         if (BooksWindow == null)
         {
            BooksWindow = new Books()
            {
               Owner = this
            };
            BooksWindow.Show();
            BooksWindow.Closed += BooksWindow_Closes;
         }
         else
         {
            BringWindowOnTop(BooksWindow);
         }
      }

      private void BooksWindow_Closes(object sender, EventArgs e)
      {
         BooksWindow = null;
      }

      private void Sell_Click(object sender, RoutedEventArgs e)
      {
         if (SellWindow == null)
         {
            SellWindow = new Sell()
            {
               Owner = this
            };
            SellWindow.Show();
            SellWindow.Closed += SellWindow_Closes;
         }
         else
         {
            BringWindowOnTop(SellWindow);
         }
      }

      private void SellWindow_Closes(object sender, EventArgs e)
      {
         SellWindow = null;
      }
   }
}
