﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Hugg.Logic;
using Hugg.Logic.LogicExceptions;
namespace UI
{
    /// <summary>
    /// This class represents a user control of the client for viewing the history of the lessons 
    /// and subscriptions of the client 
    /// </summary>
    public partial class UserControlClientHistoryLessons : ClearableUserControl
    {
        /// <summary>
        /// Constructor
        /// </summary>
        public UserControlClientHistoryLessons()
        {
            InitializeComponent();
            try
            {

                OnRefresh();
            }
            catch (DatabaseException)
            {
                MessageBox.Show("General DB exception. Please try again later.");
            }
        }

        /// <summary>
        /// This method refreshes the user control.
        /// It updates the presented subscriptions of the client
        /// </summary>
        public override void OnRefresh()
        {
            try
            {
                foreach (DataGridViewRow row in dataGridViewSubscriptions.Rows)
                {
                    try
                    {

                        dataGridViewSubscriptions.Rows.Clear();

                    }
                    catch (InvalidOperationException) { }
                }
                SubscriptionOfClient[] sbClient = SubscriptionOfClient.GetAllSubscriptionsOfClient((PrivilageHandler.loggedInUser as Customer));
                if (sbClient.Length > 0)
                {
                    foreach (SubscriptionOfClient s in sbClient)
                    {
                        string type;
                        string className;
                        SubscriptionType subscriptionType = s.TypeOfSubscription;
                        if (subscriptionType == null)
                        {
                            MessageBox.Show(String.Format("Inconsistent database: could not perform this action.{0}Details:the type of the subscription doesn't exist",
                                Environment.NewLine));
                            break;
                        }
                        if (subscriptionType.ClassesInSub is AllClassesInSubscription)
                        {
                            type = "All classes";
                            className = "--";
                        }
                        else
                        {
                            type = "One class";
                            className = (subscriptionType.ClassesInSub as OneClassesInSubscription).ClassOfSubscription.NameOfClass;
                        }
                        String[] newRow = new String[] { "" + s.IdSubscription, "" + s.BoughtDate, "" + subscriptionType.ClassesInSub.NumOfLessons, "" + s.LessonsLeft, type, className };
                        dataGridViewSubscriptions.Rows.Add(newRow);
                    }
                }
            }
            catch (DatabaseException)
            {
                MessageBox.Show("General DB exception. Please try again later.");
            }
        }

        /// <summary>
        /// This method clears all controls. It clears dataGridViewDetails.
        /// </summary>
        public override void ClearAllControls()
        {
            foreach (DataGridViewRow row in dataGridViewDetails.Rows)
            {
                try
                {

                    dataGridViewDetails.Rows.Clear();

                }
                catch (InvalidOperationException) { }
            }
        }

        /// <summary>
        /// This method presents the lessons details of the selected subscription
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DataGridViewSubscriptionsRowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {
                int idSubscription;
                foreach (DataGridViewRow row in dataGridViewDetails.Rows)
                {
                    try
                    {

                        dataGridViewDetails.Rows.Clear();

                    }
                    catch (InvalidOperationException) { }


                }
                if (!int.TryParse((string)((dataGridViewSubscriptions[0, e.RowIndex]).Value), out idSubscription))
                    MessageBox.Show("Please pick a subscription");
                else
                {
                    SubscriptionOfClient subscriptionOfClient = SubscriptionOfClient.GetSubscriptionById(idSubscription);
                    Lesson[] lessons = Lesson.GetLessonsClientInSubscription((PrivilageHandler.loggedInUser as Customer), subscriptionOfClient);
                    if (lessons.Length > 0)
                    {
                        foreach (Lesson l in lessons)
                        {
                            string instructor = (l.InstructorOfLesson != null) ? Instructor.GetNameOfInsById((l.InstructorOfLesson.Id)) : "Not set yet";
                            String[] newRow = new String[] { "" + l.Time, l.LessonName.NameOfClass, "" + l.HallNumber, instructor };
                            dataGridViewDetails.Rows.Add(newRow);
                        }
                    }
                }
            }
            catch (NotExistIdException exception)
            {
                MessageBox.Show(String.Format("Inconsistent database: could not perform this action.{0}Details: {1}",
                    Environment.NewLine, exception.Message));
            }
            catch (DatabaseException)
            {
                MessageBox.Show("General DB exception. Please try again later.");
            }
        }

       
    }
}
