using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Text;
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.CustomDialogs;

namespace Sched.CustomSubwindows
{
    /// <summary>
    /// Interaction logic for ScheduleForPanel.xaml
    /// </summary>
    public partial class ScheduleForPanel : UserControl
    {
        public static RoutedEvent LoadCoursesEvent = EventManager.RegisterRoutedEvent("LoadCourses", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(ScheduleForPanel));
        public event RoutedEventHandler LoadCourses
        {
            add { AddHandler(LoadCoursesEvent, value); }
            remove { RemoveHandler(LoadCoursesEvent, value); }
        }

        public static RoutedEvent UnloadCoursesEvent = EventManager.RegisterRoutedEvent("UnloadCourses", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(ScheduleForPanel));
        public event RoutedEventHandler UnloadCourses
        {
            add { AddHandler(UnloadCoursesEvent, value); }
            remove { RemoveHandler(UnloadCoursesEvent, value); }
        }

        public static RoutedEvent GenerateScheduleEvent = EventManager.RegisterRoutedEvent("GenerateSchedule", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(ScheduleForPanel));
        public event RoutedEventHandler GenerateSchedule
        {
            add { AddHandler(GenerateScheduleEvent, value); }
            remove { RemoveHandler(GenerateScheduleEvent, value); }
        }

        public class WeekDayBlock : TextBlock
        {
            private string _weekDayName;
            public string WeekDayName
            {
                get { return _weekDayName; }
            }

            public WeekDayBlock(string weekDayName)
            {
                _weekDayName = weekDayName;
            }
        }

        public ScheduleForPanel()
        {
            InitializeComponent();
            Loaded += AWScheduleForPanel_Loaded;

            //Assignment
            CB_Semester.ItemsSource = DBI.GetItemsAndOrderByParameter("Semester", "AcademicStartYear");

            CB_Semester.SelectionChanged += FilterCB_SelectionChanged;
            CB_StudentGroup.SelectionChanged += FilterCB_SelectionChanged;

            CB_Semester.SelectedValuePath = "Id";
            CB_Semester.SetBinding(ComboBox.SelectedValueProperty, new Binding("SemesterId"));
            CB_StudentGroup.SelectedValuePath = "Id";
            CB_StudentGroup.SetBinding(ComboBox.SelectedValueProperty, new Binding("CategoryId"));

            BTN_ShowTimetable.Click += BTN_ShowTimetable_Click;

            //ScheduleFor
            CB_DayOfWeek_Start.SelectedValuePath = "WeekDayName";
            CB_DayOfWeek_Start.SetBinding(ComboBox.SelectedValueProperty, new Binding("Asd.DayOfWeekStart"));
            CB_DayOfWeek_End.SelectedValuePath = "WeekDayName";
            CB_DayOfWeek_End.SetBinding(ComboBox.SelectedValueProperty, new Binding("Asd.DayOfWeekEnd"));

            TB_Period_Start.SetBinding(TextBox.TextProperty, new Binding("Asd.PeriodStart"));
            TB_Period_End.SetBinding(TextBox.TextProperty, new Binding("Asd.PeriodEnd"));

            CB_LessonTypePreference.SelectedValuePath = "Id";
            CB_LessonTypePreference.SetBinding(ComboBox.SelectedValueProperty, new Binding("Asd.LessonTypePreference"));

            BTN_Schedule.Click += BTN_Schedule_Click;
        }

        private void AWScheduleForPanel_Loaded(object sender, RoutedEventArgs e)
        {
            //Day List
            List<string> _dayList = new List<string>() {
            "Str_Week_Monday",
            "Str_Week_Tuesday",
            "Str_Week_Wednesday",
            "Str_Week_Thursday",
            "Str_Week_Friday",
            "Str_Week_Saturday",
            "Str_Week_Sunday" };
            ObservableCollection<WeekDayBlock> CB_DayColl_Start = new ObservableCollection<WeekDayBlock>();
            ObservableCollection<WeekDayBlock> CB_DayColl_End = new ObservableCollection<WeekDayBlock>();
            foreach (string s in _dayList)
            {
                WeekDayBlock tBlock = new WeekDayBlock(s.Substring(9));
                tBlock.SetResourceReference(TextBlock.TextProperty, s);
                WeekDayBlock tBlock2 = new WeekDayBlock(s.Substring(9));
                tBlock2.SetResourceReference(TextBlock.TextProperty, s);

                CB_DayColl_Start.Add(tBlock);
                CB_DayColl_End.Add(tBlock2);
            }
            CB_DayOfWeek_Start.ItemsSource = CB_DayColl_Start;
            CB_DayOfWeek_End.ItemsSource = CB_DayColl_End;

            //lesonTypeId
            CB_LessonTypePreference.ItemsSource = DBI.GetItemsAndOrderByParameter("LessonType", "Abbreviation");

            //Default start settings
            BTN_ShowTimetable.IsEnabled = false;
            CB_StudentGroup.IsEnabled = false;
            BTN_Schedule.IsEnabled = false;
        }

        private void BTN_Schedule_Click(object sender, RoutedEventArgs e)
        {
            var AD = DataContext as AssignmentData;
            List<int> dayPeriodRange = AD.Asd.GetDayPeriodRange();
            if ((dayPeriodRange[0] <= dayPeriodRange[1]) && (dayPeriodRange[2] <= dayPeriodRange[3]))
            {
                RaiseEvent(new RoutedEventArgs(GenerateScheduleEvent, this));
            }
            else
            {
                string msg = (string)Application.Current.Resources.MergedDictionaries[App._languageResDictIndex]["AS_ScheduleFor_InvalidRange_msg"];
                MessageBox.Show(msg);
            }
        }

        private void BTN_ShowTimetable_Click(object sender, RoutedEventArgs e)
        {
            int semesterId = ((Semester)CB_Semester.SelectedItem).Id;
            int studentGroupId = ((StudentGroup)CB_StudentGroup.SelectedItem).Id;
            TimetableViewDialog dlg = new TimetableViewDialog(DBI.LoadCategoryLessons(semesterId, "StudentGroup", studentGroupId));
            dlg.Show();
        }

        private void FilterCB_SelectionChanged(object sender, RoutedEventArgs e)
        {
            var CB = sender as ComboBox;
            if (CB != null)
            {
                if (CB.Name == "CB_Semester")
                {
                    if (CB_Semester.SelectedItem != null)
                    {
                        int semesterId = ((Semester)CB_Semester.SelectedItem).Id;
                        CB_StudentGroup.ItemsSource = DBI.GetItemsBySemesterIdAndOrderByParameter("StudentGroup", semesterId, "Name");
                        CB_StudentGroup.IsEnabled = true;
                        var DA = DataContext as AssignmentData;
                        DA.CategoryName = "StudentGroup";
                    }
                    else
                    {
                        CB_StudentGroup.IsEnabled = false;
                    }
                }
                else //CB.Name == "CB_StudentGroup"
                {
                    if (CB_StudentGroup.SelectedItem != null)
                    {
                        RaiseEvent(new RoutedEventArgs(LoadCoursesEvent, this));
                        BTN_ShowTimetable.IsEnabled = true;
                        BTN_Schedule.IsEnabled = true;
                    }
                    else
                    {
                        RaiseEvent(new RoutedEventArgs(UnloadCoursesEvent, this));
                        BTN_ShowTimetable.IsEnabled = false;
                        BTN_Schedule.IsEnabled = false;
                    }
                }
            }
        }
    }
}

