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.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using SystemX.Product.ALIS.Interface;

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

        private UcInfoView.selectTestCodeInfo SelTestCodeInfo;

        private IDataController ctrlDB;

        private static object objInfoControlNextLock = new object();

        public enum eSelectMode
        {
            Insert = 0,
            Modify
        }

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

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

        public TestCodeInfoEdit(IDataController ctrlDB, eSelectMode setMode, UcInfoView.selectTestCodeInfo getSelTestCodeInfo)
        {
            InitializeComponent();

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

            SelTestCodeInfo = getSelTestCodeInfo;

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

                    textBoxTestCode.Text = SelTestCodeInfo.TestCode;
                    textBoxGate1.Text = SelTestCodeInfo.Gate1;
                    textBoxGate2.Text = SelTestCodeInfo.Gate2;
                    textBoxComment.Text = SelTestCodeInfo.Comment;
                    break;
            }
        }

        private bool CheckGate(out int iOutGatePos)
        {
            iOutGatePos = 0;

            string[] strGetChar1 = textBoxGate1.Text.Split(';');
            string[] strGetChar2 = textBoxGate2.Text.Split(';');

            string[][] strGetGates = new string[2][];
            strGetGates[0] = new string[strGetChar1.Length];
            strGetGates[1] = new string[strGetChar2.Length];

            strGetChar1.CopyTo(strGetGates[0], 0);
            strGetChar2.CopyTo(strGetGates[1], 0);

            try
            {
                for (iOutGatePos = 0; iOutGatePos < strGetGates.Length; iOutGatePos++)
                {
                    for (int j = 0; j < strGetGates[iOutGatePos].Length; j++)
                    {
                        if (strGetGates[iOutGatePos][j].Length == 1)
                        {
                            if (Commons.CheckGateLetter(strGetGates[iOutGatePos][j].ToCharArray()[0]) == false)
                                throw new Exception();
                        }
                        else
                            throw new Exception();
                    }
                }
            }
            catch
            {
                return false;
            }

            return true;
        }

        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_TestCode", "TestCode", textBoxTestCode.Text) == true)
                                throw new Exception($"Test Code is information that already exists. : {textBoxTestCode.Text}");
                        }

                        string strItemName = "";
                        if (textBoxTestCode.Text.Length <= 0) { strItemName = "Test Code"; throw new Exception($"The value cannot be null." + strItemName); }
                        if (textBoxGate1.Text.Length <= 0) { strItemName = "Gate1"; throw new Exception($"The value cannot be null." + strItemName); }
                        if (textBoxGate2.Text.Length <= 0) { strItemName = "Gate2"; throw new Exception($"The value cannot be null." + strItemName); }

                        int iOutGatePos = 0;
                        if (CheckGate(out iOutGatePos) == false)
                            throw new Exception($"Not a normal gate expression [Gate" + (iOutGatePos + 1).ToString() + "]." + strItemName);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, strTitle, MessageBoxButtons.OK, MessageBoxIcon.Warning);

                        e.Handled = true;

                        return;
                    }

                    labelControlTestCode.Text = textBoxTestCode.Text;
                    labelControlGate1.Text = textBoxGate1.Text;
                    labelControlGate2.Text = textBoxGate2.Text;
                    labelControlComment.Text = textBoxComment.Text;

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

                    SelTestCodeInfo.TestCode = textBoxTestCode.Text;
                    SelTestCodeInfo.Gate1 = textBoxGate1.Text;
                    SelTestCodeInfo.Gate2 = textBoxGate2.Text;
                    SelTestCodeInfo.Comment = textBoxComment.Text;
                }
            }
        }

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

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

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