using System;
using System.Collections.Generic;
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.CustomControls;
using Sched.CustomClasses;
using Sched.CustomDialogs;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;

namespace Sched.CustomSubwindows
{
    /// <summary>
    /// Interaction logic for AssignmentPanel.xaml
    /// </summary>
    public partial class AssignmentPanel : UserControl
    {
        private class CategoryType : TextBlock
        {
            private string _categoryName;
            public string CategoryName
            {
                get { return _categoryName; }
            }

            public CategoryType(string categoryName)
            {
                this._categoryName = categoryName;
                //this.FontWeight = FontWeights.Bold;
            }
        }

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

        public static RoutedEvent TimetableEditDisableEvent = EventManager.RegisterRoutedEvent("TimetableEditDisable", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(AssignmentPanel));
        public event RoutedEventHandler TimetableEditDisable
        {
            add { AddHandler(TimetableEditDisableEvent, value); }
            remove { RemoveHandler(TimetableEditDisableEvent, value); }
        }
        public AssignmentPanel()
        {
            InitializeComponent();
            //Assignment panel
            CB_Semester.ItemsSource = DBI.GetItemsAndOrderByParameter("Semester", "AcademicStartYear");
            List<string> _categoryList = new List<string>() {
                "Str_Course",
                "Str_Room",
                "Str_StudentGroup",
                "Str_Teacher"};
            ObservableCollection<CategoryType> CB_List = new ObservableCollection<CategoryType>();
            foreach (string s in _categoryList)
            {
                CategoryType tBlock = new CategoryType(s.Substring(4));
                tBlock.Name = "TB_Category_" + s.Substring(4);
                tBlock.SetResourceReference(TextBlock.TextProperty, s);
                CB_List.Add(tBlock);
            }
            CB_Category.ItemsSource = CB_List;

            CB_Semester.SelectionChanged += FilterCB_SelectionChanged;
            CB_Category.SelectionChanged += FilterCB_SelectionChanged;
            CB_ChosenEntity.SelectionChanged += FilterCB_SelectionChanged;

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

            //Lesson Panel
            CB_Course.SelectedValuePath = "Id";
            CB_Course.SetBinding(ComboBox.SelectedValueProperty, new Binding("Lssn.CourseId"));

            CB_LessonType.ItemsSource = DBI.GetItemsAndOrderByParameter("LessonType", "Abbreviation");
            CB_LessonType.SelectedValuePath = "Id";
            CB_LessonType.SetBinding(ComboBox.SelectedValueProperty, new Binding("Lssn.LessonTypeId"));

            TB_Hours.SetBinding(TextBox.TextProperty, new Binding("Lssn.Hours"));
            TB_Periodicity.SetBinding(TextBox.TextProperty, new Binding("Lssn.Periodicity"));
            DP_StartDate.SetBinding(DatePicker.SelectedDateProperty, new Binding("Lssn.StartDateDT"));
            TB_Note.SetBinding(TextBox.TextProperty, new Binding("Lssn.Note"));

            GB_Lesson.Visibility = System.Windows.Visibility.Hidden;
            CB_ChosenEntity.IsEnabled = false;
        }

        private void FilterCB_SelectionChanged(object sender, RoutedEventArgs e)
        {
            var CB = sender as ComboBox;
            if (CB != null)
            {
                if ((CB.Name == "CB_Semester") || (CB.Name == "CB_Category"))
                {
                    if ((CB_Semester.SelectedItem != null) && (CB_Category.SelectedItem != null))
                    {
                        int semesterId = ((Semester)CB_Semester.SelectedItem).Id;
                        string categoryName = ((CategoryType)CB_Category.SelectedItem).CategoryName;
                        ME_Panel.SourceStudentGroup.Coll = DBI.GetItemsBySemesterIdAndOrderByParameter("StudentGroup", semesterId, "Name");

                        if (categoryName == "StudentGroup")
                        {
                            CB_ChosenEntity.ItemsSource = DBI.GetItemsBySemesterIdAndOrderByParameter(categoryName, semesterId, "Name");
                        }
                        else if (categoryName == "Course")
                        {
                            CB_ChosenEntity.ItemsSource = DBI.GetItemsAndOrderByParameter(categoryName, "Abbreviation");

                        }
                        else if (categoryName == "Teacher")
                        {
                            CB_ChosenEntity.ItemsSource = DBI.GetItemsAndOrderByParameter(categoryName, "LastName");
                        }
                        else if (categoryName == "Room")
                        {
                            CB_ChosenEntity.ItemsSource = DBI.GetItemsAndOrderByParameter(categoryName, "Name");
                        }
                        CB_ChosenEntity.IsEnabled = true;


                        DP_StartDate.DisplayDateStart = ((Semester)CB_Semester.SelectedItem).DateStartDT;
                        DP_StartDate.SelectedDate = DP_StartDate.DisplayDateStart;
                        DP_StartDate.DisplayDateEnd = ((Semester)CB_Semester.SelectedItem).DateEndDT;
                    }
                    else
                    {
                        CB_ChosenEntity.IsEnabled = false;
                    }
                }
                else
                {   // (CB.Name == "CB_ChosenEntity")

                    if (CB_ChosenEntity.SelectedItem != null)
                    {
                        //int semesterId = ((Semester)CB_Semester.SelectedItem).Id;
                        string categoryName = ((CategoryType)CB_Category.SelectedItem).CategoryName;
                        int categoryId = (int)CB_ChosenEntity.SelectedItem.GetType().GetProperty("Id").GetValue(CB_ChosenEntity.SelectedItem);

                        //Enable all comboboxes
                        CB_Course.IsEnabled = true;
                        ME_Panel.CB_StudentGroup.IsEnabled = true;
                        ME_Panel.CB_Teacher.IsEnabled = true;
                        ME_Panel.CB_Room.IsEnabled = true;

                        CB_Course.SelectedIndex = -1;
                        ME_Panel.CB_StudentGroup.SelectedIndex = -1;
                        ME_Panel.CB_Teacher.SelectedIndex = -1;
                        ME_Panel.CB_Room.SelectedIndex = -1;

                        CB_Course.ItemsSource = DBI.GetItemsAndOrderByParameter("Course", "Abbreviation");

                        //Category
                        if (categoryName == "StudentGroup")
                        {
                            CB_Course.ItemsSource = DBI.GetCoursesByStudentGroupId(categoryId);
                            //_sourceGroup.Coll = DBI.GetItems("StudentGroup");
                            //CB_Group.ItemsSource = _sourceGroup.Coll;

                            //Changing ItemsSource removes courseId, must be returned if possible
                            var DA = DataContext as AssignmentData;
                            foreach (var item in CB_Course.ItemsSource)
                            {
                                if ((int)item.GetType().GetProperty("Id").GetValue(item) == DA.Lssn.CourseId)
                                {
                                    CB_Course.SelectedItem = item;
                                    break;
                                }
                            }

                            foreach (var item in ME_Panel.CB_StudentGroup.ItemsSource)
                            {
                                if ((int)item.GetType().GetProperty("Id").GetValue(item) == categoryId)
                                {
                                    ME_Panel.CB_StudentGroup.SelectedItem = item;
                                    ME_Panel.CB_StudentGroup.IsEnabled = false;
                                    break;
                                }
                            }
                        }
                        else if (categoryName == "Room") //->A single course was chosen,
                        {
                            foreach (var item in ME_Panel.CB_Room.ItemsSource)
                            {
                                if ((int)item.GetType().GetProperty("Id").GetValue(item) == categoryId)
                                {
                                    ME_Panel.CB_Room.SelectedItem = item;
                                    ME_Panel.CB_Room.IsEnabled = false;
                                    break;
                                }
                            }
                        }
                        else if (categoryName == "Teacher") //->A single course was chosen,
                        {
                            foreach (var item in ME_Panel.CB_Teacher.ItemsSource)
                            {
                                if ((int)item.GetType().GetProperty("Id").GetValue(item) == categoryId)
                                {
                                    ME_Panel.CB_Teacher.SelectedItem = item;
                                    ME_Panel.CB_Teacher.IsEnabled = false;
                                    break;
                                }
                            }
                        }
                        else if (categoryName == "Course") //->A single course was chosen,
                        {
                            ObservableCollection<object> courseColl = new ObservableCollection<object>();
                            courseColl.Add(DBI.GetItemById("Course", categoryId));
                            CB_Course.ItemsSource = courseColl;   //->A single course was chosen, only one item as source

                            foreach (var item in CB_Course.ItemsSource)
                            {
                                if ((int)item.GetType().GetProperty("Id").GetValue(item) == categoryId)
                                {
                                    CB_Course.SelectedItem = item;
                                    CB_Course.IsEnabled = false;
                                    break;
                                }
                            }
                        }

                        if (CB_Course.SelectedIndex == -1)
                        {
                            CB_Course.SelectedIndex = 0;
                        }

                        if (CB_LessonType.SelectedIndex == -1)
                        {
                            CB_LessonType.SelectedIndex = 0;
                        }

                        ME_Panel.FillSubgrids();
                        GB_Lesson.Visibility = System.Windows.Visibility.Visible;
                        RaiseEvent(new RoutedEventArgs(TimetableEditEnableEvent, this));
                    }
                    else
                    {
                        GB_Lesson.Visibility = System.Windows.Visibility.Hidden;
                        RaiseEvent(new RoutedEventArgs(TimetableEditDisableEvent, this));
                    }

                }

            }
        }

    }
}
