﻿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 the login user control
    /// </summary>
    public partial class UserControlLogin : ClearableUserControl
    {
        /// <summary>
        /// Constructor
        /// </summary>
        public UserControlLogin()
        {
            InitializeComponent();
        }



        /// <summary>
        /// This method clears all controls
        /// </summary>
        public override void ClearAllControls()
        {
            textBoxUserName.Text = string.Empty;
            textBoxPassword.Text = string.Empty;
        }

        /// <summary>
        ///  This method logs in the user to the system and presents him the corresponding BasicMainUserControl  
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonLoginClick(object sender, EventArgs e)
        {
            try
            {
                //Check password
                int id, pass;
                if (!int.TryParse(textBoxUserName.Text, out id))
                {
                    MessageBox.Show("Invalid id number: : please enter numbers only");
                    return;
                }

                if (!int.TryParse(textBoxPassword.Text, out pass))
                {
                    MessageBox.Show("Invalid password: please enter numbers only");
                    return;
                }
                User user;
                PrivilageHandler.Privilege privilage = PrivilageHandler.GetPrivilage(id, pass, out user);

                
                switch (privilage)
                {
                    case PrivilageHandler.Privilege.None:
                        MessageBox.Show("Invalid id number or password", "error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                        break;
                    case PrivilageHandler.Privilege.Client:
                        PrivilageHandler.loggedInUser = user;
                        ShowMainUserControl(new UserControlClient());
                        break;
                    case PrivilageHandler.Privilege.Secretary:
                        PrivilageHandler.loggedInUser = user;
                        ShowMainUserControl(new UserControlSecretary());
                        break;
                    case PrivilageHandler.Privilege.Instructor:
                        PrivilageHandler.loggedInUser = user;
                        ShowMainUserControl(new UserControlInstructor());
                        break;
                    case PrivilageHandler.Privilege.Manager:
                        PrivilageHandler.loggedInUser = user;
                        ShowMainUserControl(new UserControlManager());
                        break;
                   
                }
            }
            catch (DatabaseException)
            {
                MessageBox.Show("General DB exception. Please try again later.");
            }
        }

        public delegate void SelectedUserControlHandler(object sender, MainUserControlSelectedEventArgs e);
        public event SelectedUserControlHandler SelectedUserControlEvent;

        /// <summary>
        /// This method raises the event which indicates that
        /// we need to show the specific main user control (BasicMainUserControl),
        /// with the specific BasicMainUserControl to be shown
        /// </summary>
        /// <param name="mainUserControl"></param>
        private void ShowMainUserControl(BasicMainUserControl mainUserControl)
        {
            SelectedUserControlEvent(this, new MainUserControlSelectedEventArgs(mainUserControl));
        }



    }
}
