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.Shapes;
using Sched.CustomClasses;
using Sched.CustomSubwindows;
using Sched.ScheduleAlgorithm;

namespace Sched.CustomDialogs
{
    /// <summary>
    /// Interaction logic for GeneratedTimetableViewDialog.xaml
    /// </summary>
    public partial class GeneratedTimetableViewDialog : Window
    {
        ObservableCollection<object> _assignedLessonsGA;
        ObservableCollection<object> _assignedLessonsSA;

        public GeneratedTimetableViewDialog(ObservableCollection<object> assignedlessons, string unassignedLessons, int definedLssnCount,int assignedLssnCount)
        {
            InitializeComponent();

            Loaded += GeneratedTimetableViewDialog_Loaded;
            SC.SizeChanged += SC_SizeChanged;
            BTN_Print.Click += BTN_Print_Click;
            BTN_SaveToDB.Click += BTN_SaveToDB_Click;
            BTN_Cancel.Click += BTN_Cancel_Click;
            BTN_Optimize.Click += BTN_Optimize_Click;

            _assignedLessonsGA = assignedlessons;
            _assignedLessonsSA = new ObservableCollection<object>(assignedlessons);

            TB_LessonRatio.Text = $"{assignedLssnCount}/{definedLssnCount}";
            TB_UnassignedLessons.Text = " " + unassignedLessons;
            TT.IsEnabled = false;   //to prevent user from editing outside the MainWindow
        }

        private void GeneratedTimetableViewDialog_Loaded(object sender, RoutedEventArgs e)
        {
            TT.LoadLessonsFromDB();
            TT.LoadLessons(_assignedLessonsGA);
        }

        private void SC_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            double newWidth = e.NewSize.Width;
            var SCViewer = sender as ScrollViewer;
            if (SCViewer.VerticalScrollBarVisibility == ScrollBarVisibility.Visible)
            {
                newWidth -= SystemParameters.VerticalScrollBarWidth;
            }
            TT.Width = newWidth;
        }

        private void BTN_Print_Click(object sender, RoutedEventArgs e)
        {
            PrintDialog dlg = new PrintDialog();
            if (dlg.ShowDialog() == true)
            {
                SC.ScrollToTop();
                dlg.PrintVisual(TT, "Timetable");
            }
        }

        private void BTN_SaveToDB_Click(object sender, RoutedEventArgs e)
        {
            foreach (Lesson lssn in _assignedLessonsSA)
            {
                DBI.InsertLesson(lssn);
            }

            DialogResult = true;
            Close();
        }

        private void BTN_Cancel_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            Close();
        }

        private void BTN_Optimize_Click(object sender, RoutedEventArgs e)
        {
            var AD = DataContext as AssignmentData;

            //Run
            List<int> dpRange = AD.Asd.GetDayPeriodRange();
            SimulatedAnnealing SA = new SimulatedAnnealing(dpRange[0], dpRange[1], dpRange[2], dpRange[3], AD.SemesterId, AD.CategoryId, AD.Asd.LessonTypePreference);

            List<Lesson> lessonsReschedule = new List<Lesson>();
            foreach (Lesson lssn in _assignedLessonsSA)
            {
                lessonsReschedule.Add(new Lesson(lssn));
            }

            lessonsReschedule = SA.Run(lessonsReschedule);
            _assignedLessonsSA = new ObservableCollection<object>(lessonsReschedule);

            TT.RemoveAllLessons();
            TT.LoadLessonsFromDB();
            TT.LoadLessons(_assignedLessonsSA);
        }
    }

}
