using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
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;
using Sched.CustomClasses;
using Sched.CustomControls;
using Sched.CustomSubwindows;
using Sched.CustomDialogs;

namespace Sched
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    /// 

    public partial class MainWindow : Window
    {
        public AssignmentData AD = new AssignmentData();
        public MainWindow()
        {
            this.DataContext = AD;
            InitializeComponent();

            this.Loaded += MainWindow_Loaded;

            //MENU
            Menu_File_Print.Click += Menu_File_Print_Click;
            Menu_File_About.Click += Menu_File_About_Click;
            Menu_File_Exit.Click += Menu_File_Exit_Click;

            Menu_View_Database.Click += Menu_View_Database_Click;
            Menu_View_Autogenerate.Click += Menu_View_Autogenerate_Click;

            Menu_Tools_TimetableCourseCheck.Click += Menu_Tools_TimetableCourseCheck_Click;
            Menu_Tools_TimetableFullCollisionCheck.Click += Menu_Tools_TimetableFullCollisionCheck_Click;
            Menu_Tools_Language.Click += Menu_Tools_Language_Click;

            TimetablePanel.SizeChanged += TimetablePanel_SizeChanged;

            //HADNLERS
            AddHandler(AssignmentPanel.TimetableEditEnableEvent, new RoutedEventHandler(TimetablePanel_EnableToggle));
            AddHandler(AssignmentPanel.TimetableEditDisableEvent, new RoutedEventHandler(TimetablePanel_EnableToggle));
            
            //LessonBoxEvents
            AddHandler(TimetableDayGrid.TimetableLessonAddedEvent, new RoutedEventHandler(CollisionPanel_MainTimetableChanged));
            AddHandler(LessonBox.LessonBoxCopyEvent, new RoutedEventHandler(AssignmentPanel_LessonCopyData));
            AddHandler(LessonBox.LessonBoxEditEvent, new RoutedEventHandler(CollisionPanel_MainTimetableChanged),true);
            AddHandler(LessonBox.LessonBoxDeleteEvent, new RoutedEventHandler(CollisionPanel_MainTimetableChanged), true);
            AddHandler(MultientityPanel.MultientityPanelChangedEvent, new RoutedEventHandler(CollisionPanel_AssignmentPanelChanged));
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            //Set en-US as default language
            ((App)Application.Current).UpdateLanguage(App._languageISOCode);

            //Use the user system language if there is ResourceDict for it
            string IsoCode = System.Globalization.CultureInfo.CurrentUICulture.ToString();       
            ((App)Application.Current).UpdateLanguage(IsoCode);

            //Menu disable
            this.Menu_Tools_TimetableCourseCheck.IsEnabled = false;

            //Panel disable
            this.TimetablePanel.IsEnabled = false;
            this.TimetablePanel.Visibility = Visibility.Hidden;
            this.CollisionPanel.Visibility = Visibility.Hidden;
        }

        private void Menu_File_About_Click(object sender, RoutedEventArgs e)
        {
            string msg = App.AppName + " 1.0.0";
            msg += "\n\n" + "Veronika Macků, 200037@vutbr.cz";
            msg += "\n\n" + "Faculty of Mechanical Engineering\nBrno University Of Technology" + "\n2022";
            MessageBox.Show(msg, App.AppName, MessageBoxButton.OK, MessageBoxImage.Information);
        }

        private void Menu_File_Exit_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
        private void Menu_File_Print_Click(object sender, RoutedEventArgs e)
        {
            PrintDialog dlg = new PrintDialog();
            if (dlg.ShowDialog() == true)
            {
                TimetablePanel.ScrollToTop();
                dlg.PrintVisual(MainTimetable, "Timetable");
            }
        }
        private void Menu_View_Database_Click(object sender, RoutedEventArgs e)
        {
            DatabaseWindow dbWindow = new DatabaseWindow();
            dbWindow.ShowDialog();

            string msg = (string)Application.Current.Resources.MergedDictionaries[App._languageResDictIndex]["Menu_View_Database_msg"];
            MessageBox.Show(msg, App.AppName, MessageBoxButton.OK, MessageBoxImage.Information);
        }
        private void Menu_View_Autogenerate_Click(object sender, RoutedEventArgs e)
        {
            AutoscheduleWindow asWindow = new AutoscheduleWindow();
            asWindow.ShowDialog();
        }
        private void Menu_Tools_Language_Click(object sender, RoutedEventArgs e)
        {
            LanguageSelectionDialog dlg = new LanguageSelectionDialog();
            if (dlg.ShowDialog() == true)
            {
                ((App)Application.Current).UpdateLanguage(dlg.LanguageISOCode);
            }
        }
        private void Menu_Tools_TimetableCourseCheck_Click(object sender, RoutedEventArgs e)
        {
            var DA = DataContext as AssignmentData;
            if (DA != null)
            {
                string categoryName = DA.CategoryName;               
                if (categoryName == "StudentGroup")
                {
                    int semesterId = DA.SemesterId;
                    int studentGroupId = DA.CategoryId;
                    TimetableCourseCheckDialog dlg = new TimetableCourseCheckDialog(semesterId, studentGroupId);
                    dlg.ShowDialog();
                }
                else
                {
                    string s = (string)Application.Current.Resources.MergedDictionaries[App._languageResDictIndex]["TimetableCourseChecklistDlg_Msg"];
                    MessageBox.Show(s, App.AppName, MessageBoxButton.OK, MessageBoxImage.Information);
                }
            }
        }
        private void Menu_Tools_TimetableFullCollisionCheck_Click(object sender, RoutedEventArgs e)
        {
            if (MainTimetable.IsEnabled)
            {
                string msgCollisionExternal = MainTimetable.FullExternalCheckForCollisions();
                TimetableFullCollisionCheckDialog dlg = new TimetableFullCollisionCheckDialog(msgCollisionExternal);
                dlg.Show();
            }
        }
    

        private void TimetablePanel_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            //Debug.WriteLine(e.PreviousSize + " "  + e.NewSize);
            double newWidth = e.NewSize.Width;
            if (TimetablePanel.VerticalScrollBarVisibility == ScrollBarVisibility.Visible)
            {
                newWidth -= SystemParameters.VerticalScrollBarWidth;
            }

            //Debug.WriteLine("TTPanel: " +  newWidth);
            this.MainTimetable.MaxWidth = newWidth;
        }
        private void TimetablePanel_EnableToggle(object sender, RoutedEventArgs e)
        {
            string eventName = e.RoutedEvent.Name;
            if (eventName == "TimetableEditEnable")
            {
                Menu_Tools_TimetableCourseCheck.IsEnabled = true;
                Menu_Tools_TimetableFullCollisionCheck.IsEnabled = true;
                TimetablePanel.IsEnabled = true;
                this.TimetablePanel.Visibility = Visibility.Visible;
                this.CollisionPanel.Visibility = Visibility.Visible;

                MainTimetable.LoadLessonsFromDB();
                
            }
            else if (eventName == "TimetableEditDisable")
            {
                Menu_Tools_TimetableCourseCheck.IsEnabled = false;
                Menu_Tools_TimetableFullCollisionCheck.IsEnabled = false;
                TimetablePanel.IsEnabled = false;
                this.TimetablePanel.Visibility = Visibility.Hidden;
                this.CollisionPanel.Visibility = Visibility.Hidden;

                MainTimetable.RemoveAllLessons();               
            }
        }
        private void AssignmentPanel_LessonCopyData(object sender, RoutedEventArgs e)
        {
            var lBox = e.OriginalSource as LessonBox;
            var AD = this.DataContext as AssignmentData;
            if ((lBox != null) && (AD != null))
            {
                AD.Lssn = new Lesson(lBox.Lssn);
                AD.Lssn.Id = 0;
                AssignmentPanel.ME_Panel.FillSubgrids();
            }
        }

        private void CollisionPanel_MainTimetableChanged(object sender, RoutedEventArgs e)
        {
            if (CollisionPanel.IsEnabled)
            {
                //CollisionPanel.LoadLessons();
                
                string eventName = e.RoutedEvent.Name;
                if (eventName == "TimetableLessonAdded")
                {
                    var grid = e.Source as Grid;
                    var lBox = grid.Children[grid.Children.Count - 1] as LessonBox;
                    CollisionPanel.LessonAdd(lBox.Lssn);
                    
                }else if (eventName == "LessonBoxEdit")
                {
                    var lBox = e.Source as LessonBox;
                    CollisionPanel.LessonEdit(lBox.Lssn);
                }
                else if (eventName == "LessonBoxDelete")
                {
                    var lBox = e.Source as LessonBox;
                    CollisionPanel.LessonDelete(lBox.Lssn);
                }
                
            }
        }
        private void CollisionPanel_AssignmentPanelChanged(object sender, RoutedEventArgs e)
        {
            if (CollisionPanel.IsEnabled)
            {
                CollisionPanel.LoadLessons();
            }
        }
    }
}
