using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SystemX.Product.ALIS.Interface;

namespace SystemX.Product.ALIS.UI.View.InfoList
{
    public partial class UserInfoEdit : Form
    {
        public eSelectMode CurrentWizardMode { internal set; get; }

        private UcInfoView.selectUserInfo SelUserInfo;

        private IDataController ctrlDB;

        private static object objInfoControlNextLock = new object();

        public enum eSelectMode
        {
            Insert = 0,
            Modify
        }

        private string strTitle = "STAT User";
        private string strWizardControl = "STAT User";
        private string strWelcomeWizard = "Welcome to the STAT User wizard!";
        private string strWizardPage = "STAT User Information";

        public void WizardCancelEvent(object sender, EventArgs e)
        {
            if (((bool)sender) == false)
                this.DialogResult = DialogResult.Cancel;
        }

        public UserInfoEdit(IDataController ctrlDB, eSelectMode setMode, UcInfoView.selectUserInfo getSelUserInfo)
        {
            InitializeComponent();

            this.Text = strTitle;
            wizardControl.Text = strWizardControl;
            welcomeWizardPage.Text = strWelcomeWizard;
            wizardPage.Text = strWizardPage;
            //
            CurrentWizardMode = setMode;

            SelUserInfo = getSelUserInfo;

            this.ctrlDB = ctrlDB;

            switch (CurrentWizardMode)
            {
                case eSelectMode.Insert:
                    welcomeWizardPage.IntroductionText = "This is the procedure to insert new information into the " + strTitle + " table.";
                    wizardControl.Text = strTitle + " - Insert";
                    this.Text = strTitle + " - Insert";
                    break;
                case eSelectMode.Modify:
                    welcomeWizardPage.IntroductionText = "This is the procedure to modify new information in the " + strTitle + " table.";
                    wizardControl.Text = strTitle + " - Modify";
                    this.Text = strTitle + " - Modify";

                    textBoxUserID.Text = SelUserInfo.UserID;
                    textBoxName.Text = SelUserInfo.Name;
                    //textBoxPassword.Text = SelUserInfo.Password;
                    textBoxDept.Text = SelUserInfo.Dept;
                    textBoxEmail.Text = SelUserInfo.Email;
                    textBoxComment.Text = SelUserInfo.Comment;
                    break;
            }
        }

        private void wizardControl_NextClick(object sender, DevExpress.XtraWizard.WizardCommandButtonClickEventArgs e)
        {
            if (wizardControl.SelectedPageIndex == 1)
            {
                lock (objInfoControlNextLock)
                {
                    try
                    {
                        if (CurrentWizardMode == eSelectMode.Insert)
                        {
                            if (ctrlDB.GetDuplicateCheck("STAT_User", "UserID", textBoxPassword.Text) == true)
                                throw new Exception($"HostID is information that already exists. : {textBoxPassword.Text}");
                        }

                        if (Commons.IsValidEmail(textBoxEmail.Text) == false)
                            throw new Exception($"E-Mail format is not correct. : {textBoxEmail.Text}");
                        //
                        string strItemName = "";
                        if (textBoxUserID.Text.Length <= 0) { strItemName = "UserID"; throw new Exception($"The value cannot be null." + strItemName); }
                        if (textBoxName.Text.Length <= 0) { strItemName = "Name"; throw new Exception($"The value cannot be null." + strItemName); }
                        if (textBoxPassword.Text.Length <= 0) { strItemName = "Password"; throw new Exception($"The value cannot be null." + strItemName); }
                        if (textBoxDept.Text.Length <= 0) { strItemName = "Dept"; throw new Exception($"The value cannot be null." + strItemName); }
                        if (textBoxEmail.Text.Length <= 0) { strItemName = "Email"; throw new Exception($"The value cannot be null." + strItemName); }
                        //
                        if (textBoxPasswordConfirm.Text.CompareTo(textBoxPassword.Text) != 0)
                            throw new Exception($"Passwords do not match.");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, strTitle, MessageBoxButtons.OK, MessageBoxIcon.Warning);

                        e.Handled = true;

                        return;
                    }

                    labelControlUserID.Text = textBoxUserID.Text;
                    labelControlName.Text = textBoxName.Text;
                    //labelControlPassword.Text = textBoxPassword.Text;
                    labelControlDept.Text = textBoxDept.Text;
                    labelControlEmail.Text = textBoxEmail.Text;
                    labelControlComment.Text = textBoxComment.Text;

                    switch (CurrentWizardMode)
                    {
                        case eSelectMode.Insert:
                            break;
                        case eSelectMode.Modify:
                            break;
                    }

                    SelUserInfo.UserID = textBoxUserID.Text;
                    SelUserInfo.Name = textBoxName.Text;
                    SelUserInfo.Password = textBoxPassword.Text;
                    SelUserInfo.Dept = textBoxDept.Text;
                    SelUserInfo.Email = textBoxEmail.Text;
                    SelUserInfo.Comment = textBoxComment.Text;
                }
            }
        }

        private void wizardControl_CustomizeCommandButtons(object sender, DevExpress.XtraWizard.CustomizeCommandButtonsEventArgs e)
        {
            if (e.Page == wizardPage)
            {
                this.ActiveControl = textBoxUserID;

                textBoxUserID.Focus();
            }
            else if (e.Page == welcomeWizardPage)
            {
                this.ActiveControl = e.NextButton.Button;

                e.NextButton.Button.Focus();
            }
        }
    }
}
