using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.ComboBox;

using SystemX.Product.ALIS.Interface;
using SystemX.Common.Archive;

using CpApplication;
using CpApplication.Manager;
using CpCommon;
using CpTesterPlatform.CpLogUtil;
using static PsCommon.PsCommon;

using PsKGaudi;
using PsKGaudi.Parser.PsCCSArea;
using PsKGaudi.Parser.PsCCSSTDFn;
using PsKGaudi.Parser.MacroModuleSkel;
using PsKGaudi.Parser.PsCCSSTDFn.Parameters;
using static PsKGaudi.Parser.PsCCS;

using static CpCommon.ExceptionHandler;

using static SystemX.Product.ALIS.UI.View.InfoList.UcTestListView;
using SystemX.Common;

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

        private CVariantInformation SelTestListVariantInfo;

        private IDataController ctrlDB;

        private static object objInfoControlNextLock = new object();

        private int nSelectedTestListFileNo;
        private string[] strSelectedTestListFileVariantList;

        private string strGetModifyProdP;
        private string strGetModifyTestType;
        private string strGetModifyFileVersion;
        private string strGetModifyFileName;

        public enum eSelectMode
        {
            Insert = 0,
            Modify
        }

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

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

        private bool TestListFileCheckResult { set; get; } = false;

        private bool GroupCheckResult { set; get; } = false;

        private void GetGroupList()
        {
            SqlDataReader reader;

            ctrlDB.GetConnSqlCmd().CommandText = "SELECT * FROM PROD_Group WITH(NOLOCK) ORDER BY No ASC;";

            reader = ctrlDB.GetConnSqlCmd().ExecuteReader();
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            dt.Load(reader);
            ds.Tables.Add(dt);
            reader.Close();

            comboBoxGroupList.Items.Clear();

            if (XCommons.isHasRow(ds))
            {
                comboBoxGroupList.Items.Add("Select Model Name(Group) ...");
                comboBoxGroupList.SelectedIndex = 0;

                for (int i = 0; i < dt.Rows.Count; i++)
                    comboBoxGroupList.Items.Add(dt.Rows[i]["ModelName"].ToString());
            }
        }

        private bool CheckGroupInfo()
        {
            SqlDataReader reader;
            reader = ctrlDB.GetConnSqlCmd().ExecuteReader();
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            dt.Load(reader);
            ds.Tables.Add(dt);
            reader.Close();

            if (XCommons.isHasRow(ds))
            {
                textBoxDispGroupNo.Text = dt.Rows[0][0].ToString();
                textBoxDispGroupName.Text = dt.Rows[0][1].ToString();
                textBoxDispModelName.Text = dt.Rows[0][2].ToString();
                textBoxDispComment.Text = dt.Rows[0][3].ToString();

                textBoxInputModelName.Text = dt.Rows[0][2].ToString();

                GroupCheckResult = true;

                textBoxInputModelName.BackColor = Color.LimeGreen;

                return true;
            }
            else
            {
                textBoxInputModelName.BackColor = Color.Red;

                return false;
            }
        }

        public TestListVariantInfoEdit(IDataController ctrlDB, eSelectMode setMode, CVariantInformation getSelTestListInfo)
        {
            InitializeComponent();

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

            SelTestListVariantInfo = getSelTestListInfo;

            this.ctrlDB = ctrlDB;

            GetGroupList();

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

                    textBoxItem6_Picker.Value = SelTestListVariantInfo.RegDT;
                    textBoxItem7.Text = SelTestListVariantInfo.RegUser;
                    textBoxItem8_Picker.Value = SelTestListVariantInfo.UpdateDT;
                    textBoxItem9.Text = SelTestListVariantInfo.UpdateUser;
                    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";

                    textBoxItem1.Text = SelTestListVariantInfo.ProdNo_P;

                    textBoxItem6_Picker.Value = SelTestListVariantInfo.RegDT;
                    textBoxItem7.Text = SelTestListVariantInfo.RegUser;
                    textBoxItem8_Picker.Value = SelTestListVariantInfo.UpdateDT;
                    textBoxItem9.Text = SelTestListVariantInfo.UpdateUser;

                    strGetModifyProdP = SelTestListVariantInfo.ProdNo_P;
                    strGetModifyTestType = SelTestListVariantInfo.TestType;
                    strGetModifyFileVersion = SelTestListVariantInfo.Version;
                    strGetModifyFileName = SelTestListVariantInfo.FileName;

                    ctrlDB.GetConnSqlCmd().CommandText = "SELECT * FROM PROD_Group WHERE No = " + SelTestListVariantInfo.GroupNo + " ORDER BY No ASC;";

                    CheckGroupInfo();

                    textBoxItem11.Text = SelTestListVariantInfo.Comment;
                    textBoxItem12.Text = SelTestListVariantInfo.Description;

                    //View Modify SHow
                    textBoxModify1.Text = SelTestListVariantInfo.ProdNo_P;
                    textBoxModify2.Text = SelTestListVariantInfo.TestType;
                    textBoxModify3.Text = SelTestListVariantInfo.Version;
                    textBoxModify4.Text = SelTestListVariantInfo.ProdCode;
                    textBoxModify5.Text = SelTestListVariantInfo.FileName;
                    textBoxModify6.Text = textBoxDispGroupName.Text;
                    textBoxModify7.Text = textBoxDispModelName.Text;
                    textBoxModify8.Text = SelTestListVariantInfo.Comment;
                    textBoxModify9.Text = SelTestListVariantInfo.Description;

                    textBoxTestListFileNameInput.Text = SelTestListVariantInfo.FileName; //SelTestListVariantInfo.TestListName;
                    textBoxDispInTestType.Text = SelTestListVariantInfo.TestType;
                    textBoxDispInVersion.Text = SelTestListVariantInfo.Version;
                    textBoxDispInProdCode.Text = SelTestListVariantInfo.ProdCode;
                    textBoxDispInFileName.Text = SelTestListVariantInfo.FileName;

                    DataTable dtTestList = ScanTestListFileCheck();

                    if (XCommons.isHasRow(dtTestList))
                        if (CheckTestListFileInfo(dtTestList))
                            SetVariantList();

                    panelModify.Visible = true;

                    break;
            }
        }

        private void wizardControl_NextClick(object sender, DevExpress.XtraWizard.WizardCommandButtonClickEventArgs e)
        {
            lock (objInfoControlNextLock)
            {
                if (wizardControl.SelectedPageIndex == 0)
                {
                    if (CurrentWizardMode == eSelectMode.Modify)
                    {
                        ;//
                    }
                }
                else if (wizardControl.SelectedPageIndex == 1)
                {
                    try
                    {
                        groupBoxTestList.Enabled = false;
                        groupBoxGroup.Enabled = false;

                        string strItemName = "";
                        if (textBoxItem1.Text.Length <= 0) { strItemName = "ProdNo_P"; throw new Exception($"The value cannot be null. [" + strItemName + "]"); }

                        if (TestListFileCheckResult == false) { strItemName = "TestList File"; throw new Exception($"The value must be input and check. [" + strItemName + "]"); }
                        if (GroupCheckResult == false) { strItemName = "Group No"; throw new Exception($"The value must be input and check. [" + strItemName + "]"); }

                        bool bFindVariant = false;
                        foreach (string strVariant in strSelectedTestListFileVariantList)
                        {
                            if (strVariant.CompareTo(textBoxItem1.Text) == 0)
                            {
                                bFindVariant = true;

                                break;
                            }
                        }

                        string strGetProdP = textBoxItem1.Text;
                        string strGetTestType = textBoxDispInTestType.Text;
                        string strGetFileVersion = textBoxDispInVersion.Text;
                        string strGetFileName = textBoxDispInFileName.Text;

                        //Variant Duplicate Check
                        string strGetQueryText = "SELECT * FROM [PROD_Variant] WHERE No IN(SELECT No FROM [PROD_Variant] WHERE [ProdNo_P] = '" + strGetProdP + "' " +
                            "AND [TestListFileNo] = " + nSelectedTestListFileNo +
                            ") ORDER BY No ASC;";

                        DataTable dtVariantCheck = ctrlDB.GetTable(strGetQueryText);

                        if (XCommons.isHasRow(dtVariantCheck))
                        {
                            bool bExistException = true;

                            if (CurrentWizardMode == eSelectMode.Modify)
                            {
                                if (strGetProdP.CompareTo(strGetModifyProdP) == 0 &&
                                   strGetTestType.CompareTo(strGetModifyTestType) == 0 &&
                                   strGetFileVersion.CompareTo(strGetModifyFileVersion) == 0 &&
                                   strGetFileName.CompareTo(strGetModifyFileName) == 0)
                                    bExistException = false;
                            }

                            if (bExistException)
                                throw new Exception($"ProdNo_P - TestType - FileVersion - FileName Combine Key is information that already exists. : " +
                                    $"{strGetProdP}" + " " + $"{strGetTestType}" + " " + $"{strGetFileVersion}" + " " + $"{strGetFileName}");
                        }

                        if (bFindVariant == false) { strItemName = "ProdNo_P"; throw new Exception($"P_ProdNo that does not exist in the selected test list." + strItemName); }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, strTitle, MessageBoxButtons.OK, MessageBoxIcon.Warning);

                        e.Handled = true;

                        return;
                    }
                    finally
                    {
                        groupBoxTestList.Enabled = true;
                        groupBoxGroup.Enabled = true;
                    }

                    SelTestListVariantInfo.TestListFileNo = nSelectedTestListFileNo;
                    SelTestListVariantInfo.TestType = textBoxDispInTestType.Text;
                    SelTestListVariantInfo.Version = textBoxDispInVersion.Text;
                    SelTestListVariantInfo.ProdCode = textBoxDispInProdCode.Text;
                    SelTestListVariantInfo.FileName = textBoxDispInFileName.Text;

                    SelTestListVariantInfo.Description = textBoxDispTestListDescription.Text;
                    textBoxItem12.Text = SelTestListVariantInfo.Description;

                    richTextBoxDisp.Clear();
                    int nNumPos = 1;
                    foreach (string strVariant in strSelectedTestListFileVariantList)
                    {
                        richTextBoxDisp.AppendText(nNumPos.ToString("D3") + ": " + strVariant + "\r\n");
                        nNumPos++;
                    }

                    labelControlInfo1.Text = textBoxItem1.Text;
                    labelControlInfo2.Text = SelTestListVariantInfo.TestType;
                    labelControlInfo3.Text = SelTestListVariantInfo.Version;
                    labelControlInfo4.Text = SelTestListVariantInfo.ProdCode;
                    labelControlInfo5.Text = SelTestListVariantInfo.FileName;

                    labelControlInfo6.Text = textBoxItem6_Picker.Value.ToString("yyyy-MM-dd HH:mm:ss.fff");
                    labelControlInfo7.Text = textBoxItem7.Text;

                    labelControlInfo8.Text = textBoxItem8_Picker.Value.ToString("yyyy-MM-dd HH:mm:ss.fff");
                    labelControlInfo8Sub.Text = ctrlDB.GetServerDateTimeString(); //DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
                    labelControlInfo9.Text = textBoxItem9.Text;
                    labelControlInfo9Sub.Text = ctrlDB.GetLoginInfo().UserID;

                    string strGetItemNum = textBoxDispGroupNo.Text; //textBoxItem10_Combo.SelectedItem.ToString().Split('|')[0].Remove(0, 2);
                    int igroupSetNum = Commons.ConvertTextToTryValue<int>(strGetItemNum, -1);

                    //textBoxItem10_Combo.SelectedItem.ToString();
                    labelControlInfo10.Text = igroupSetNum.ToString() + " " +

                    textBoxDispGroupNo.Text + " " +
                    textBoxDispGroupName.Text + " " +
                    textBoxDispModelName.Text + " " +
                    textBoxDispComment.Text;

                    labelControlInfo11.Text = textBoxItem11.Text;
                    labelControlInfo12.Text = textBoxItem12.Text;

                    switch (CurrentWizardMode)
                    {
                        case eSelectMode.Insert:
                            SelTestListVariantInfo.UpdateDT = textBoxItem8_Picker.Value;
                            SelTestListVariantInfo.UpdateUser = textBoxItem9.Text;
                            break;
                        case eSelectMode.Modify:
                            SelTestListVariantInfo.UpdateDT = Convert.ToDateTime(labelControlInfo8Sub.Text);
                            SelTestListVariantInfo.UpdateUser = labelControlInfo9Sub.Text;
                            break;
                    }

                    SelTestListVariantInfo.ProdNo_P = textBoxItem1.Text;

                    SelTestListVariantInfo.RegDT = textBoxItem6_Picker.Value;
                    SelTestListVariantInfo.RegUser = textBoxItem7.Text;
                    SelTestListVariantInfo.GroupNo = igroupSetNum;
                    SelTestListVariantInfo.Comment = textBoxItem11.Text;
                    SelTestListVariantInfo.Description = textBoxItem12.Text;
                }
            }
        }

        private void buttonGroupChk_Click(object sender, EventArgs e)
        {
            GroupCheckResult = false;

            textBoxDispGroupNo.Text = "";
            textBoxDispGroupName.Text = "";
            textBoxDispModelName.Text = "";
            textBoxDispComment.Text = "";

            ctrlDB.GetConnSqlCmd().CommandText = "SELECT * FROM PROD_Group WHERE ModelName = '" + textBoxInputModelName.Text + "' ORDER BY No ASC;";

            CheckGroupInfo();
        }

        private void wizardControl_CancelClick(object sender, CancelEventArgs e)
        {
            ;//
        }

        private void TestListInfoEdit_FormClosing(object sender, FormClosingEventArgs e)
        {
            ;//
        }

        private void comboBoxVariant_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBoxVariant.SelectedIndex > 0)
                textBoxItem1.Text = comboBoxVariant.Items[comboBoxVariant.SelectedIndex].ToString();
        }

        private void textBoxInputModelName_TextChanged(object sender, EventArgs e)
        {
            GroupCheckResult = false;

            textBoxInputModelName.BackColor = Color.Yellow;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBoxGroupList.SelectedIndex > 0)
            {
                textBoxInputModelName.Text = comboBoxGroupList.Items[comboBoxGroupList.SelectedIndex].ToString();

                GroupCheckResult = false;

                textBoxInputModelName.BackColor = Color.Yellow;
            }
        }

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

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

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

        private bool CheckTestListFileInfo(DataTable dt)
        {
            if (XCommons.isHasRow(dt) == false)
            {
                textBoxTestListFileNameInput.BackColor = Color.Red;
                textBoxDispInTestType.BackColor = Color.Red;
                textBoxDispInVersion.BackColor = Color.Red;
                textBoxDispInProdCode.BackColor = Color.Red;
                textBoxDispInFileName.BackColor = Color.Red;

                textBoxDispInTestType.ReadOnly = false;
                textBoxDispInVersion.ReadOnly = false;
                textBoxDispInProdCode.ReadOnly = false;
                textBoxDispInFileName.ReadOnly = false;

                return false;
            }

            DataSet ds = new DataSet();
            ds.Tables.Add(dt);

            textBoxDispTestListNo.Text = dt.Rows[0][0].ToString();

            nSelectedTestListFileNo = int.MaxValue;
            int.TryParse(textBoxDispTestListNo.Text, out nSelectedTestListFileNo);

            //textBoxTestListFileNameInput.Text = dt.Rows[0][5].ToString(); //dt.Rows[0][1].ToString();

            textBoxDispInTestType.Text = dt.Rows[0][2].ToString();
            textBoxDispInVersion.Text = dt.Rows[0][3].ToString();
            textBoxDispInProdCode.Text = dt.Rows[0][4].ToString();
            textBoxDispInFileName.Text = dt.Rows[0][5].ToString();
            textBoxDispTestListComment.Text = dt.Rows[0][6].ToString();
            textBoxDispTestListDescription.Text = dt.Rows[0][7].ToString();

            textBoxTestListFileNameInput.Text = dt.Rows[0][5].ToString(); //dt.Rows[0][1].ToString();

            TestListFileCheckResult = true;

            textBoxTestListFileNameInput.BackColor = Color.LimeGreen;
            textBoxDispInTestType.BackColor = Color.LimeGreen;
            textBoxDispInVersion.BackColor = Color.LimeGreen;
            textBoxDispInProdCode.BackColor = Color.LimeGreen;
            textBoxDispInFileName.BackColor = Color.LimeGreen;

            textBoxDispInTestType.ReadOnly = true;
            textBoxDispInVersion.ReadOnly = true;
            textBoxDispInProdCode.ReadOnly = true;
            textBoxDispInFileName.ReadOnly = true;

            return true;
        }

        private void checkBoxAutoComplete_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxAutoComplete.Checked == false)
            {
                textBoxTestListFileNameInput.AutoCompleteCustomSource = null;
                textBoxTestListFileNameInput.AutoCompleteMode = AutoCompleteMode.None;
                textBoxTestListFileNameInput.AutoCompleteSource = AutoCompleteSource.None;
            }
            else
            {
                //DataTable dtProdNoP = ctrlDB.GetTable("SELECT No, Name FROM [STOR_TestListFile] ORDER BY No ASC;");
                DataTable dtProdNoP = ctrlDB.GetTable("SELECT No, FileName FROM [STOR_TestListFile] ORDER BY No ASC;");

                if (dtProdNoP == null)
                    return;

                /*
                int nDataPos = 0;

                string[] strData = new string[dtProdNoP.Rows.Count];

                foreach(DataRow dr in dtProdNoP.Rows)
                {
                    strData[nDataPos] = dr["Name"].ToString();

                    nDataPos++;
                }
                */

                /*
                string[] postSource = dtProdNoP
                    .AsEnumerable()
                    .Select<System.Data.DataRow, string>(x => x.Field<string>("Name"))
                    .ToArray();
                */

                string[] postSource = dtProdNoP
                    .AsEnumerable()
                    .Select<System.Data.DataRow, string>(x => x.Field<string>("FileName"))
                    .ToArray();

                var source = new AutoCompleteStringCollection();

                source.AddRange(postSource);

                textBoxTestListFileNameInput.AutoCompleteCustomSource = source;

                textBoxTestListFileNameInput.AutoCompleteMode = AutoCompleteMode.Suggest;
                textBoxTestListFileNameInput.AutoCompleteSource = AutoCompleteSource.CustomSource;
            }
        }

        private DataTable ScanTestListFileCheck()
        {
            string strSetQueryText = "SELECT No, Name, TestType, Version, ProdCode, FileName, Comment, Description FROM [STOR_TestListFile] " +
               "WHERE ";

            string strSetQueryEndText = "ORDER BY No ASC;";

            bool bFirstAdded = true;

           if (textBoxTestListFileNameInput.Text.Length > 0)
            {
                //strSetQueryText += "AND ";
                strSetQueryText += "FileName = '" + textBoxTestListFileNameInput.Text + "' ";

                bFirstAdded = false;
            }
            if (textBoxDispInTestType.Text.Length > 0)
            {
                if (bFirstAdded == false)
                    strSetQueryText += "AND ";

                strSetQueryText += "TestType = '" + textBoxDispInTestType.Text + "' ";

                bFirstAdded = false;
            }
            if (textBoxDispInVersion.Text.Length > 0)
            {
                if (bFirstAdded == false)
                    strSetQueryText += "AND ";

                strSetQueryText += "Version = '" + textBoxDispInVersion.Text + "' ";

                bFirstAdded = false;
            }
            if (textBoxDispInProdCode.Text.Length > 0)
            {
                if (bFirstAdded == false)
                    strSetQueryText += "AND ";

                strSetQueryText += "ProdCode = '" + textBoxDispInProdCode.Text + "' ";

                bFirstAdded = false;
            }
            /*
            if (textBoxDispInFileName.Text.Length > 0)
            {
                strSetQueryText += "AND ";
                strSetQueryText += "FileName = '" + textBoxDispInFileName.Text + "' ";
            }
            */
            if (textBoxDispTestListNo.ReadOnly == false)
            {
                if (textBoxDispTestListNo.Text.Length > 0)
                {
                    if (bFirstAdded == false)
                        strSetQueryText += "AND ";

                    strSetQueryText += "No = " + textBoxDispTestListNo.Text + " ";

                    bFirstAdded = false;
                }
            }

            strSetQueryText += strSetQueryEndText;

            DataTable dtTestList = ctrlDB.GetTable(strSetQueryText);

            return dtTestList;
        }

        private void SetVariantList()
        {
            comboBoxVariant.BeginUpdate();
            comboBoxVariant.Items.Clear();
            comboBoxVariant.Refresh();
            comboBoxVariant.Items.Add("Select Variant ...");

            string strSetQueryText = "SELECT * FROM [HIST_TestListFileVariantList] AS X WHERE TestListFileNo = " + nSelectedTestListFileNo + ";";

            DataTable dtVariantList = ctrlDB.GetTable(strSetQueryText);

            if (XCommons.isHasRow(dtVariantList))
            {
                strSelectedTestListFileVariantList = dtVariantList.Rows[0]["VariantList"].ToString().Split(';');
                foreach (string strVariant in strSelectedTestListFileVariantList)
                    comboBoxVariant.Items.Add(strVariant);

                comboBoxVariant.SelectedIndex = 0;
                comboBoxVariant.EndUpdate();

                comboBoxVariant.Visible = true;

                textBoxItem12.Text = textBoxDispTestListDescription.Text;
            }
            else
                TestListFileCheckResult = false;
        }

        private void buttonTestListFileChk_Click(object sender, EventArgs e)
        {
            TestListFileCheckResult = false;

            if (textBoxDispTestListNo.ReadOnly == true)
                textBoxDispTestListNo.Text = "";

            textBoxDispTestListComment.Text = "";
            textBoxDispTestListDescription.Text = "";

            string strSetQueryText = string.Empty;

            DataTable dtTestList = ScanTestListFileCheck();

            if (XCommons.isHasRow(dtTestList))
            {
                if (CheckTestListFileInfo(dtTestList))
                {           
                    SetVariantList();

                    AllTextReadOnlyOn();
                }
            }

            EnabledTestListFileSelect(TestListFileCheckResult);
        }

        private void EnabledTestListFileSelect(bool bSetCheckResult)
        {
            if (bSetCheckResult)
            {
                this.ActiveControl = textBoxItem1;

                textBoxItem1.Focus();
            }
            else
            {
                this.ActiveControl = textBoxTestListFileNameInput;

                textBoxTestListFileNameInput.Focus();
            }
        }

        private void textBoxTestListFileNameInput_TextChanged(object sender, EventArgs e)
        {
            TestListFileCheckResult = false;

            textBoxTestListFileNameInput.BackColor = Color.Yellow;

            textBoxDispInTestType.Text = "";
            textBoxDispInVersion.Text = "";
            textBoxDispInProdCode.Text = "";
            textBoxDispInFileName.Text = "";

            EnabledTestListFileSelect(false);
        }

        private void AllTextReadOnlyOn()
        {
            if (textBoxDispInTestType.ReadOnly == false) textBoxDispInTestType.ReadOnly = true;
            if (textBoxDispInVersion.ReadOnly == false) textBoxDispInVersion.ReadOnly = true;
            if (textBoxDispInProdCode.ReadOnly == false) textBoxDispInProdCode.ReadOnly = true;
            if (textBoxDispInFileName.ReadOnly == false) textBoxDispInFileName.ReadOnly = true;
            if (textBoxDispTestListNo.ReadOnly == false) textBoxDispTestListNo.ReadOnly = true;
        }

        private bool textReadOnlyCheck()
        {
            if (textBoxDispInTestType.ReadOnly == false) return false;
            if (textBoxDispInVersion.ReadOnly == false) return false;
            if (textBoxDispInProdCode.ReadOnly == false) return false;
            if (textBoxDispInFileName.ReadOnly == false) return false;
            if (textBoxDispTestListNo.ReadOnly == false) return false;

            return true;
        }

        private void textBoxDispIn_TextChanged(object sender, EventArgs e)
        {
            TestListFileCheckResult = false;

            textBoxTestListFileNameInput.BackColor = Color.Yellow;
            
            if(textReadOnlyCheck())
                EnabledTestListFileSelect(false);
        }

        private void textBoxDispIn_ReadOnlyChanged(object sender, EventArgs e)
        {
            int nTag = Convert.ToInt32(((Control)sender).Tag);

            bool bReadOnly = ((TextBox)sender).ReadOnly;

            switch (nTag)
            {
                case 0:
                    if (bReadOnly) button1.BackColor = Color.Transparent;
                    else button1.BackColor = Color.Yellow; textBoxDispInTestType.Focus();
                    break;
                case 1:
                    if (bReadOnly) button2.BackColor = Color.Transparent;
                    else button2.BackColor = Color.Yellow; textBoxDispInVersion.Focus();
                    break;
                case 2:
                    if (bReadOnly) button3.BackColor = Color.Transparent;
                    else button3.BackColor = Color.Yellow; textBoxDispInProdCode.Focus();
                    break;
                case 3:
                    if (bReadOnly) button4.BackColor = Color.Transparent;
                    else button4.BackColor = Color.Yellow; textBoxDispInFileName.Focus();
                    break;
                case 4:
                    if (bReadOnly) button5.BackColor = Color.Transparent;
                    else button5.BackColor = Color.Yellow; textBoxDispTestListNo.Focus();
                    break;
            }
        }

        private void button_Click(object sender, EventArgs e)
        {
            int nTag = Convert.ToInt32(((Control)sender).Tag);

            switch (nTag)
            {
                case 0:
                    if (textBoxDispInTestType.ReadOnly) textBoxDispInTestType.ReadOnly = false;
                    else if (textBoxDispInTestType.ReadOnly == false) textBoxDispInTestType.ReadOnly = true;
                    break;
                case 1:
                    if (textBoxDispInVersion.ReadOnly) textBoxDispInVersion.ReadOnly = false;
                    else if (textBoxDispInVersion.ReadOnly == false) textBoxDispInVersion.ReadOnly = true;
                    break;
                case 2:
                    if (textBoxDispInProdCode.ReadOnly) textBoxDispInProdCode.ReadOnly = false;
                    else if (textBoxDispInProdCode.ReadOnly == false) textBoxDispInProdCode.ReadOnly = true;
                    break;
                case 3:
                    if (textBoxDispInFileName.ReadOnly) textBoxDispInFileName.ReadOnly = false;
                    else if (textBoxDispInFileName.ReadOnly == false) textBoxDispInFileName.ReadOnly = true;
                    break;
                case 4:
                    if (textBoxDispTestListNo.ReadOnly) textBoxDispTestListNo.ReadOnly = false;
                    else if (textBoxDispTestListNo.ReadOnly == false) textBoxDispTestListNo.ReadOnly = true;
                    break;
            }
        }
    }
}
