using System;
using System.Collections.Generic;
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;

namespace Sched.CustomSubwindows
{
    /// <summary>
    /// Interaction logic for CollisionPanel.xaml
    /// </summary>
    public partial class CollisionPanel : UserControl
    {
        public CollisionPanel()
        {
            InitializeComponent();
            this.Loaded += CollisionCheck_Loaded;

            CB_CollisionCategory.SelectionChanged += CB_CollisionCategory_SelectionChanged;
        }

        private void CollisionCheck_Loaded(object sender, RoutedEventArgs e)
        {
            //Fill CB_CollisionCategory
            List<string> categoryList = new List<string>() {
                "Str_Everything",
                "Str_Room",
                "Str_StudentGroup",
                "Str_Teacher"};

            List<TextBlock> CB_List = new List<TextBlock>();
            foreach (string s in categoryList)
            {
                TextBlock tBlock = new TextBlock();
                tBlock.Name = "TB_" + s;
                tBlock.SetResourceReference(TextBlock.TextProperty, s);
                CB_List.Add(tBlock);
            }

            CB_CollisionCategory.ItemsSource = CB_List;
            CB_CollisionCategory.SelectedIndex = 0;
        }

        private void CB_CollisionCategory_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            FilterLessons();
        }
        public void FilterLessons()
        {
            string filterName = ((TextBlock)CB_CollisionCategory.SelectedItem).Name;

            var DA = DataContext as AssignmentData;
            if (DA != null)
            {
                for (int i = 8; i < MiniTable.Children.Count; i++)    //TimetableDayGrid controls start from index 8 to 14
                {
                    var subgrid = MiniTable.Children[i] as TimetableDayGrid;
                    for (int j = 0; j < subgrid.Children.Count; j++)
                    {
                        var lBox = subgrid.Children[j] as LessonBox;        //Lesson
                        switch (filterName)
                        {
                            case "TB_Str_Everything":
                                lBox.Visibility = Visibility.Visible;
                                break;
                            default:
                                bool isInSubgrid = false;
                                string entityName = filterName.Substring(7);
                                //Debug.WriteLine(lBox.Lssn.ToString() + lBox.Lssn.Id.ToString());
                                foreach (int entityId in DA.Lssn.GetEntities(entityName))
                                {
                                    if (lBox.Lssn.GetEntities(entityName).Contains(entityId))
                                    {
                                        isInSubgrid = true;
                                        break;
                                    }
                                }

                                if (isInSubgrid)
                                {
                                    lBox.Visibility = Visibility.Visible;
                                }
                                else
                                {
                                    lBox.Visibility = Visibility.Hidden;
                                }
                                break;
                        }
                    }
                }
            }
        }
        public void LoadLessons()
        {
            MiniTable.LoadLessonsOfLessonPanelEntities();
            FilterLessons();
        }

        public void LessonAdd(Lesson lssn)
        {
            MiniTable.AddLesson(lssn);
            CollisionCheck(lssn);
            FilterLessons();
        }
        public void LessonEdit(Lesson lssn)
        {
            MiniTable.EditLesson(lssn);
            CollisionCheck(lssn);
            FilterLessons();
        }
        public void LessonDelete(Lesson lssn)
        {
            MiniTable.DeleteLesson(lssn);
        }
        public void CollisionCheck(Lesson lssn)
        {
            string collisionLesson = MiniTable.CheckForCollisions(lssn);
            if (collisionLesson.Length > 0) //if there is a lesson collision
            {
                string title = (string)Application.Current.Resources.MergedDictionaries[App._languageResDictIndex]["Collision_CollisionMsg_Title"];
                string msg = (string)Application.Current.Resources.MergedDictionaries[App._languageResDictIndex]["Collision_CollisionMsg_Msg"];

                MessageBox.Show(msg + collisionLesson, title);
            }
        }
    }
}
