using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Diagnostics;
using System.Windows.Input;
using Sched.CustomClasses;
using System.Windows.Shapes;
using System.Collections.ObjectModel;

namespace Sched.CustomControls
{
    public class MiniTimetable : Timetable
    {
        private List<string> _dayListAbbreviations = new List<string>() {
            "Str_Week_Monday_Abbreviation",
            "Str_Week_Tuesday_Abbreviation",
            "Str_Week_Wednesday_Abbreviation",
            "Str_Week_Thursday_Abbreviation",
            "Str_Week_Friday_Abbreviation",
            "Str_Week_Saturday_Abbreviation",
            "Str_Week_Sunday_Abbreviation" };

        public MiniTimetable() : base(20.0, 30.0, 60.0, 8.0)
        {
            //Day textblocks
            for (int i = 0; i < 7; i++)
            {
                var el = this.Children[i] as TextBlock;
                string s = _dayListAbbreviations[i];
                el.SetResourceReference(TextBlock.TextProperty, s);
            }

            //Period subgrid
            TimetableDayGrid periodSubgrid = this.Children[7] as TimetableDayGrid;
            for (int i = 0; i < periodSubgrid.Children.Count; i++)
            {
                var el = periodSubgrid.Children[i] as TextBlock;
                if (i < 9)
                {
                    el.Text = el.Text.Substring(0, 1);  //to get period number without time
                }
                else
                {
                    el.Text = el.Text.Substring(0, 2);
                }
            }

            //Day subgrids
            for (int i = 8; i < this.Children.Count; i++)
            {
                var el = this.Children[i] as TimetableDayGrid;
                el.RemoveAllHandlers();     //To disable user being able to manually add/delete/edit lessons
            }

            HorizontalAlignment = HorizontalAlignment.Stretch;
            //VerticalAlignment = VerticalAlignment.Stretch;
        }

        public void LoadLessonsOfLessonPanelEntities()
        {
            var DA = DataContext as AssignmentData;
            if (DA != null)
            {
                RemoveAllLessons();

                ObservableCollection<object> lessons = DBI.LoadCollisionPanelLessons(DA.Lssn);
                foreach (Lesson lssn in lessons)
                {
                    string dayRow = lssn.StartDateDT.DayOfWeek.ToString();
                    for (int i = 8; i < this.Children.Count; i++)    //TimetableDayGrid controls start from index 8 to 14
                    {
                        var subgrid = this.Children[i] as TimetableDayGrid;
                        if (subgrid.Name == dayRow)
                        {
                            subgrid.LessonAddNoDescNoNewRow(lssn, lssn.PeriodStart);
                            break;  //no need to check rest of subgrids
                        }
                    }
                }
            }
        }
        public string CheckForCollisions(Lesson lssn)
        {
            string dayRow = lssn.StartDateDT.DayOfWeek.ToString();
            for (int i = 8; i < this.Children.Count; i++)    //TimetableDayGrid controls start from index 8 to 14
            {
                var subgrid = this.Children[i] as TimetableDayGrid;
                if (subgrid.Name == dayRow)
                {
                    string collision = subgrid.SingleCheckForCollisions(lssn);
                    return collision;
                }
            }

            return string.Empty;
        }
        public void AddLesson(Lesson lssn)
        {
            string dayRow = lssn.StartDateDT.DayOfWeek.ToString();
            for (int i = 8; i < this.Children.Count; i++)    //TimetableDayGrid controls start from index 8 to 14
            {
                var subgrid = this.Children[i] as TimetableDayGrid;
                if (subgrid.Name == dayRow)
                {
                    if ((lssn.GetEntities("Teacher").Count > 0) || (lssn.GetEntities("Room").Count > 0) || (lssn.GetEntities("StudentGroup").Count > 0))
                    {
                        subgrid.LessonAddNoDescNoNewRow(lssn, lssn.PeriodStart);
                        break;
                    }
                    break;
                }
            }
        }
        public void EditLesson(Lesson lssn)
        {
            string dayRow = lssn.StartDateDT.DayOfWeek.ToString();
            for (int i = 8; i < this.Children.Count; i++)    //TimetableDayGrid controls start from index 8 to 14
            {
                var subgrid = this.Children[i] as TimetableDayGrid;
                if (subgrid.Name == dayRow)
                {
                    for (int j = subgrid.Children.Count - 1; j >= 0; j--)
                    {
                        var lBox = subgrid.Children[j] as LessonBox;
                        if (lssn.Id == lBox.Lssn.Id)    //Old lesson replace
                        {
                            subgrid.Children.Remove(lBox);
                            subgrid.LessonAddNoDescNoNewRow(lssn, lssn.PeriodStart);
                            break;
                        }
                    }
                    break;
                }
            }
        }
        public void DeleteLesson(Lesson lssn)
        {
            string dayRow = lssn.StartDateDT.DayOfWeek.ToString();
            for (int i = 8; i < this.Children.Count; i++)    //TimetableDayGrid controls start from index 8 to 14
            {
                var subgrid = this.Children[i] as TimetableDayGrid;
                if (subgrid.Name == dayRow)
                {
                    for (int j = subgrid.Children.Count - 1; j >= 0; j--)
                    {
                        var lBox = subgrid.Children[j] as LessonBox;
                        //Debug.WriteLine("Delete Lessonbox: " + lssn.Id.ToString() + " " + lBox.Lssn.Id.ToString());
                        if (lssn.Id == lBox.Lssn.Id)
                        {
                            subgrid.Children.Remove(lBox);
                            break;
                        }
                    }
                    break;
                }
            }
        }
        
    }
}
