using BuechermarktClient.Models;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
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 static IMongoDatabase Database;
      public static IMongoCollection<BookType> BookTypeCollection;
      public static IMongoCollection<Book> BookCollection;
      public static IMongoCollection<Student> StudentCollection;
      public static string MongoHost = "localhost";

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

      public MainWindow()
      {
         InitializeComponent();
         OnConnectionEstablised("localhost");
         //ToDo get server informations
      }

      public void OnConnectionEstablised(string hostname)
      {
         MongoHost = hostname;
         Mongo = new MongoClient("mongodb://" + MongoHost + ":27017");
         Database = Mongo.GetDatabase("buechermarkt");
         BookTypeCollection = Database.GetCollection<BookType>("booktypes");
         BookCollection = Database.GetCollection<Book>("books");
         StudentCollection = Database.GetCollection<Student>("students");

         var t = new Thread(BackupService);
         t.Start();
      }

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

      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;
      }
   }
}
