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 GroupInfoEdit : Form
    {
        public eSelectMode CurrentWizardMode { internal set; get; }

        private UcInfoView.selectGroupInfo SelGroupInfo;

        private IDataController ctrlDB;

        private static object objInfoControlNextLock = new object();

        public enum eSelectMode
        {
            Insert = 0,
            Modify
        }

        private string strTitle = "PROD Group";
        private string strWizardControl = "PROD Group";
        private string strWelcomeWizard = "Welcome to the PROD Group wizard!";
        private string strWizardPage = "PROD Group Information";

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

        public GroupInfoEdit(IDataController ctrlDB, eSelectMode setMode, UcInfoView.selectGroupInfo getSelGroupInfo)
        {
            InitializeComponent();

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

            SelGroupInfo = getSelGroupInfo;

            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";

                    textBoxGroupName.Text = SelGroupInfo.GroupName;
                    textBoxModelName.Text = SelGroupInfo.ModelName;
                    textBoxComment.Text = SelGroupInfo.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("PROD_Group", "ModelName", textBoxModelName.Text) == true)
                                throw new Exception($"ModelName is information that already exists. : {textBoxModelName.Text}");
                        }

                        string strItemName = "";
                        if (textBoxGroupName.Text.Length <= 0) { strItemName = "Group Name"; throw new Exception($"The value cannot be null." + strItemName); }
                        if (textBoxModelName.Text.Length <= 0) { strItemName = "Model Name"; throw new Exception($"The value cannot be null." + strItemName); }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, strTitle, MessageBoxButtons.OK, MessageBoxIcon.Warning);

                        e.Handled = true;

                        return;
                    }

                    labelControlGroupName.Text = textBoxGroupName.Text;
                    labelControlModelName.Text = textBoxModelName.Text;
                    labelControlComment.Text = textBoxComment.Text;

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

                    SelGroupInfo.GroupName = textBoxGroupName.Text;
                    SelGroupInfo.ModelName = textBoxModelName.Text;
                    SelGroupInfo.Comment = textBoxComment.Text;
                }
            }
        }

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

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

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