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

namespace Sched.CustomDialogs
{
    /// <summary>
    /// Interaction logic for TimetableCourseCheckDialog.xaml
    /// </summary>
    public partial class TimetableCourseCheckDialog : Window
    {
        Semester _semester;
        int _studentGroupId;
        ObservableCollection<object> _lessonColl;

        public TimetableCourseCheckDialog(int semesterId, int studentGroupId, ObservableCollection<Lesson> lessonColl = null)
        {
            InitializeComponent();
            _semester = DBI.GetItemById("Semester", semesterId) as Semester;
            _studentGroupId = studentGroupId;
            _lessonColl = DBI.GetLessonsByStudentGroupId(_studentGroupId);

            if (lessonColl != null)
            {
                foreach(Lesson lesson in lessonColl)
                {
                    _lessonColl.Add(lesson);
                }
            }

            Btn_OK.Click += Btn_OK_Click;

            EvaluateLessons();
        }

        private void EvaluateLessons()
        {
            //Course hours
            DataTable dt = DBI.GetCourseChecklist(_studentGroupId);

            //Get whether conditions have been fulfilled
            foreach (DataRow dr in dt.Rows)
            {
                bool hasLesson = false;
                bool hasTeacher = true;
                bool hasRoom = true;
                bool hoursReached = false;

                int hours = 0;
                foreach (Lesson lsn in _lessonColl)
                {
                    if (dr["CourseId"].ToString() == lsn.CourseId.ToString())
                    {
                        if (dr["LessonTypeId"].ToString() == lsn.LessonTypeId.ToString())
                        {
                            hasLesson = true;
                            hours += _semester.GetHoursTotal(lsn.StartDateDT, lsn.Periodicity, lsn.Hours);

                            //If at least one lesson doesn't have a teacher or room
                            //then conditions has not been fulfilled
                            if (lsn.GetEntities("Teacher").Count == 0)
                            {
                                hasTeacher = false;
                            }
                            if (lsn.GetEntities("Room").Count == 0)
                            {
                                hasRoom = false;
                            }
                        }
                    }
                }

                if (!(hasLesson))
                {
                    hasTeacher = false;
                    hasRoom = false;
                }

                int minCourseHours = (int)dr["MinHours"];
                if (hours >= minCourseHours)
                {
                    hoursReached = true;
                }

                dr.SetField("Hours", hours);
                dr.SetField("HasTeacher", hasTeacher);
                dr.SetField("HasRoom", hasRoom);
                dr.SetField("HoursReached", hoursReached);
            }
            DG_CourseHours.AutoGeneratingColumn += DG_CourseHours_AutoGeneratingColumn;
            DG_CourseHours.ItemsSource = dt.DefaultView;
        }

        private void DG_CourseHours_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
        {
            string colheader = "Str_" + e.Column.Header.ToString();
            e.Column.Header = (string)Application.Current.Resources.MergedDictionaries[App._languageResDictIndex][colheader];
        }

        private void Btn_OK_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = true;
            Close();
        }
    }
}