using System;
using System.Collections.Generic;
using ManualSoftwareSystem.lib;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics.Metrics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using ManualSoftwareSystem.Operation_Manual;
using System.IO;

namespace ManualSoftwareSystem
{
    public partial class FrmInspectionWorkInput : Form
    {
        private string csvPartFile = string.Empty;
        private List<List<string>> Data = new List<List<string>>();
        private List<List<string>> CompleteResult = new List<List<string>>();
        private string csvPartFileInspectionInputInformation = System.IO.Directory.GetCurrentDirectory() + "\\InspectionInputInformation.sys";
        string pathFile = string.Empty;
        string pathFileName = string.Empty;
        public dynamic objLanguage;
        private string[] arrayFileMaster;
        private string strFileMaster = string.Empty;

        public FrmInspectionWorkInput(string pathFile)
        {
            InitializeComponent();
            this.csvPartFile = pathFile;
            this.objLanguage = FrmOperationManualOperator.objLanguage;
            List<string> tmp = null;
            CompleteResult = new List<List<string>>();
            tmp = null;
            Int32 i = 0;
            using (var csv = new CsvReader(csvPartFile))
            {
                while ((tmp = csv.ReadRow()) != null)
                {
                    CompleteResult.Add(tmp);

                    //if (tmp.Count < 12)
                    if (tmp.Count < 14)
                    {
                        //MessageBox.Show(this, objLanguage.Translate("File Format Wrong."),
                        MessageBox.Show(this, objLanguage.Translate("File Format is not correct."),
                               objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                               MessageBoxIcon.Exclamation,
                               MessageBoxDefaultButton.Button1, 0);

                        return;

                    }
                    i++;
                }

                if (CompleteResult.Count < 1)
                {
                    //MessageBox.Show(this, objLanguage.Translate("File Format Wrong."),
                    MessageBox.Show(this, objLanguage.Translate("File Format is not correct."),
                           objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                           MessageBoxIcon.Exclamation,
                           MessageBoxDefaultButton.Button1, 0);

                    return;

                }
            }

            strFileMaster = Path.GetFileName(csvPartFile).Substring(0, Path.GetFileName(csvPartFile).Length - 4);

            arrayFileMaster = this.strFileMaster.Split("_");
            if (arrayFileMaster.Length > 1)
            {
                FrmOperationManualOperator.strFormatName = arrayFileMaster[0].Trim();
            }
            else
            {
                FrmOperationManualOperator.strFormatName = string.Empty;
            }

        }

        private void FrmInspectionWorkInput_Load(object sender, EventArgs e)
        {
            //string strInputInformation = "Input information";
            string strInputInformation = "Input error information";
            string strItWillBe = "It will be the input file name of the product information.";
            //string strBlanks = "** Blanks and double quotation marks cannot be entered.";
            string strBlanks = "** Blanks and double quotation marks are not allowed.";
            string strFilePath = "** File path + file name must be within 200 characters";
            string strAll = "All";
            //string strSerial = "Serial";
            string strSerial = "Serial NO";
            string strAddSerial = "Add";
            string strDelSerial = "Del";
            //string strProductNo = "Product no";
            string strProductNo = "Product";
            string strAddProductNo = "Add";
            string strDelProductNo = "Del";
            string strInformation1 = "Information1";
            string strAddInformation1 = "Add";
            string strDelInformation1 = "Del";
            string strInformation2 = "Information2";
            string strAddInformation2 = "Add";
            string strDelInformation2 = "Del";
            string strInformation3 = "Information3";
            string strAddInformation3 = "Add";
            string strDelInformation3 = "Del";
            string strInformation4 = "Information4";
            string strAddInformation4 = "Add";
            string strDelInformation4 = "Del";
            string strInformation5 = "Information5";
            string strAddInformation5 = "Add";
            string strDelInformation5 = "Del";
            string strExcel = "Excel output format setting";
            string strSelect = "Select";
            string strClear = "Clear";
            string strSave = "Save";
            string strCancel = "Cancel";

            lblInputInformation.Text = objLanguage.Translate(strInputInformation);
            lblItWillBe.Text = objLanguage.Translate(strItWillBe);
            lblBlanks.Text = objLanguage.Translate(strBlanks);
            lblFilePath.Text = objLanguage.Translate(strFilePath);
            cbxAll.Text = objLanguage.Translate(strAll);
            cbxSerial.Text = objLanguage.Translate(strSerial);
            btnAddSerial.Text = objLanguage.Translate(strAddSerial);
            btnDelSerial.Text = objLanguage.Translate(strDelSerial);
            cbxProductNo.Text = objLanguage.Translate(strProductNo);
            btnAddProductNo.Text = objLanguage.Translate(strAddProductNo);
            btnDelProductNo.Text = objLanguage.Translate(strDelProductNo);
            cbxInformation1.Text = objLanguage.Translate(strInformation1);
            btnAddInformation1.Text = objLanguage.Translate(strAddInformation1);
            btnDelInformation1.Text = objLanguage.Translate(strDelInformation1);
            cbxInformation2.Text = objLanguage.Translate(strInformation2);
            btnAddInformation2.Text = objLanguage.Translate(strAddInformation2);
            btnDelInformation2.Text = objLanguage.Translate(strDelInformation2);
            cbxInformation3.Text = objLanguage.Translate(strInformation3);
            btnAddInformation3.Text = objLanguage.Translate(strAddInformation3);
            btnDelInformation3.Text = objLanguage.Translate(strDelInformation3);
            cbxInformation4.Text = objLanguage.Translate(strInformation4);
            btnAddInformation4.Text = objLanguage.Translate(strAddInformation4);
            btnDelInformation4.Text = objLanguage.Translate(strDelInformation4);
            cbxInformation5.Text = objLanguage.Translate(strInformation5);
            btnAddInformation5.Text = objLanguage.Translate(strAddInformation5);
            btnDelInformation5.Text = objLanguage.Translate(strDelInformation5);
            lblExcel.Text = objLanguage.Translate(strExcel);
            btnSelect.Text = objLanguage.Translate(strSelect);
            btnClear.Text = objLanguage.Translate(strClear);
            btnSave.Text = objLanguage.Translate(strSave);
            btnCancel.Text = objLanguage.Translate(strCancel);

            if (!File.Exists(csvPartFileInspectionInputInformation))
            {
                using (StreamWriter sw = File.CreateText(csvPartFileInspectionInputInformation))
                {
                    sw.WriteLine("");
                    sw.WriteLine("");
                    sw.WriteLine("");
                    sw.WriteLine("");
                    sw.WriteLine("");
                    sw.WriteLine("");
                    sw.WriteLine("");
                }
            }

            cmbSerial.Items.Clear();
            cmbProductNo.Items.Clear();
            cmbInformation1.Items.Clear();
            cmbInformation2.Items.Clear();
            cmbInformation3.Items.Clear();
            cmbInformation4.Items.Clear();
            cmbInformation5.Items.Clear();

            txtExcelOutput.ReadOnly = true;
            txtExcelOutput.Text = this.CompleteResult[0][12]; // csvPartFile;

            List<string> tmp = null;
            Data = new List<List<string>>();
            tmp = null;
            Int32 i = 0;
            using (var csv = new CsvReader(csvPartFileInspectionInputInformation))
            {
                while ((tmp = csv.ReadRow()) != null)
                {
                    Data.Add(tmp);

                    if (i == 0)
                    {
                        for (int j = 0; j < tmp.Count; j++)
                        {
                            if (this.Data[i][j] != string.Empty)
                            {
                                cmbSerial.Items.Add(this.Data[i][j]);
                            }

                        }
                    }

                    if (i == 1)
                    {
                        for (int j = 0; j < tmp.Count; j++)
                        {
                            if (this.Data[i][j] != string.Empty)
                            {
                                cmbProductNo.Items.Add(this.Data[i][j]);
                            }

                        }
                    }

                    if (i == 2)
                    {
                        for (int j = 0; j < tmp.Count; j++)
                        {
                            if (this.Data[i][j] != string.Empty)
                            {
                                cmbInformation1.Items.Add(this.Data[i][j]);
                            }

                        }
                    }

                    if (i == 3)
                    {
                        for (int j = 0; j < tmp.Count; j++)
                        {
                            if (this.Data[i][j] != string.Empty)
                            {
                                cmbInformation2.Items.Add(this.Data[i][j]);
                            }

                        }
                    }

                    if (i == 4)
                    {
                        for (int j = 0; j < tmp.Count; j++)
                        {
                            if (this.Data[i][j] != string.Empty)
                            {
                                cmbInformation3.Items.Add(this.Data[i][j]);
                            }

                        }
                    }

                    if (i == 5)
                    {
                        for (int j = 0; j < tmp.Count; j++)
                        {
                            if (this.Data[i][j] != string.Empty)
                            {
                                cmbInformation4.Items.Add(this.Data[i][j]);
                            }

                        }
                    }

                    if (i == 6)
                    {
                        for (int j = 0; j < tmp.Count; j++)
                        {
                            if (this.Data[i][j] != string.Empty)
                            {
                                cmbInformation5.Items.Add(this.Data[i][j]);
                            }

                        }
                    }


                    i = i + 1;
                }

            }//using end
        }

        private void btnAddSerial_Click(object sender, EventArgs e)
        {
            if (cmbSerial.Text.Trim().Replace(",", "") == string.Empty)
            {
                //MessageBox.Show(this, objLanguage.Translate("Serial is Blanks"),
                MessageBox.Show(this, objLanguage.Translate("Serial NO is Blanks"),
                                   objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                                   MessageBoxIcon.Exclamation,
                                   MessageBoxDefaultButton.Button1, 0);
                return;
            }


            int index = cmbSerial.FindStringExact(cmbSerial.Text.Trim().Replace(",", ""));

            if (index == -1)
            {
                cmbSerial.Items.Add(cmbSerial.Text.Trim().Replace(",", ""));

                string strTemp = string.Empty;

                for (int i = 0; i < cmbSerial.Items.Count; i++)
                {
                    if (i == 0)
                    {
                        strTemp = cmbSerial.Items[i].ToString();
                    }
                    else
                    {
                        strTemp = strTemp + "," + cmbSerial.Items[i].ToString();
                    }

                }

                lineChanger(strTemp, csvPartFileInspectionInputInformation, 1);

            }
        }

        static void lineChanger(string newText, string fileName, int line_to_edit)
        {
            string[] arrLine = File.ReadAllLines(fileName);
            arrLine[line_to_edit - 1] = newText;
            File.WriteAllLines(fileName, arrLine);
        }

        private void btnDelSerial_Click(object sender, EventArgs e)
        {
            if (cmbSerial.Text.Trim() == string.Empty)
            {
                //MessageBox.Show(this, objLanguage.Translate("Serial is Blanks"),
                MessageBox.Show(this, objLanguage.Translate("Serial NO is Blanks"),
                                   objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                                   MessageBoxIcon.Exclamation,
                                   MessageBoxDefaultButton.Button1, 0);
                return;
            }

            int index = cmbSerial.FindStringExact(cmbSerial.Text.Trim());

            if (index > -1)
            {
                cmbSerial.Items.RemoveAt(index);

                if (cmbSerial.Items.Count == 0)
                {
                    cmbSerial.Items.Clear();
                    cmbSerial.Text = string.Empty;
                }

                string strTemp = string.Empty;

                for (int i = 0; i < cmbSerial.Items.Count; i++)
                {
                    if (i == 0)
                    {
                        strTemp = cmbSerial.Items[i].ToString();
                    }
                    else
                    {
                        strTemp = strTemp + "," + cmbSerial.Items[i].ToString();
                    }

                }

                lineChanger(strTemp, csvPartFileInspectionInputInformation, 1);
            }
        }

        private void btnOrderAtoZSerial_Click(object sender, EventArgs e)
        {
            cmbSerial.Sorted = true;
        }

        private void btnOrderZtoASerial_Click(object sender, EventArgs e)
        {
            cmbSerial.Sorted = false;

            List<string> TempList = new List<string>();
            for (int i = 0; i < cmbSerial.Items.Count; i++)
            {

                TempList.Add(cmbSerial.Items[i].ToString());

            }

            List<string> sortedDetailedComment = TempList.OrderByDescending(DetailedComment => DetailedComment).ToList();
            cmbSerial.Items.Clear();
            foreach (string DetailedCommentName in sortedDetailedComment)
                cmbSerial.Items.Add(DetailedCommentName);
        }

        private void btnAddProductNo_Click(object sender, EventArgs e)
        {
            if (cmbProductNo.Text.Trim().Replace(",", "") == string.Empty)
            {
                //MessageBox.Show(this, objLanguage.Translate("Product no is Blanks"),
                MessageBox.Show(this, objLanguage.Translate("Product is Blanks"),
                                   objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                                   MessageBoxIcon.Exclamation,
                                   MessageBoxDefaultButton.Button1, 0);
                return;
            }


            int index = cmbProductNo.FindStringExact(cmbProductNo.Text.Trim().Replace(",", ""));

            if (index == -1)
            {
                cmbProductNo.Items.Add(cmbProductNo.Text.Trim().Replace(",", ""));

                string strTemp = string.Empty;

                for (int i = 0; i < cmbProductNo.Items.Count; i++)
                {
                    if (i == 0)
                    {
                        strTemp = cmbProductNo.Items[i].ToString();
                    }
                    else
                    {
                        strTemp = strTemp + "," + cmbProductNo.Items[i].ToString();
                    }

                }

                lineChanger(strTemp, csvPartFileInspectionInputInformation, 2);

            }
        }

        private void btnDelProductNo_Click(object sender, EventArgs e)
        {
            if (cmbProductNo.Text.Trim() == string.Empty)
            {
                //MessageBox.Show(this, objLanguage.Translate("Product no is Blanks"),
                MessageBox.Show(this, objLanguage.Translate("Product is Blanks"),
                                   objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                                   MessageBoxIcon.Exclamation,
                                   MessageBoxDefaultButton.Button1, 0);
                return;
            }

            int index = cmbProductNo.FindStringExact(cmbProductNo.Text.Trim());

            if (index > -1)
            {
                cmbProductNo.Items.RemoveAt(index);

                if (cmbProductNo.Items.Count == 0)
                {
                    cmbProductNo.Items.Clear();
                    cmbProductNo.Text = string.Empty;
                }

                string strTemp = string.Empty;

                for (int i = 0; i < cmbProductNo.Items.Count; i++)
                {
                    if (i == 0)
                    {
                        strTemp = cmbProductNo.Items[i].ToString();
                    }
                    else
                    {
                        strTemp = strTemp + "," + cmbProductNo.Items[i].ToString();
                    }

                }

                lineChanger(strTemp, csvPartFileInspectionInputInformation, 2);
            }
        }

        private void btnOrderAtoZProductNo_Click(object sender, EventArgs e)
        {
            cmbProductNo.Sorted = true;
        }

        private void btnOrderZtoAProductNo_Click(object sender, EventArgs e)
        {
            cmbProductNo.Sorted = false;

            List<string> TempList = new List<string>();
            for (int i = 0; i < cmbProductNo.Items.Count; i++)
            {

                TempList.Add(cmbProductNo.Items[i].ToString());

            }

            List<string> sortedActionContent = TempList.OrderByDescending(ActionContent => ActionContent).ToList();
            cmbProductNo.Items.Clear();
            foreach (string ActionContentName in sortedActionContent)
                cmbProductNo.Items.Add(ActionContentName);
        }

        private void btnAddInformation1_Click(object sender, EventArgs e)
        {
            if (cmbInformation1.Text.Trim().Replace(",", "") == string.Empty)
            {
                MessageBox.Show(this, objLanguage.Translate("Information1 is Blanks"),
                                       objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                                       MessageBoxIcon.Exclamation,
                                       MessageBoxDefaultButton.Button1, 0);
                return;
            }


            int index = cmbInformation1.FindStringExact(cmbInformation1.Text.Trim().Replace(",", ""));

            if (index == -1)
            {
                cmbInformation1.Items.Add(cmbInformation1.Text.Trim().Replace(",", ""));

                string strTemp = string.Empty;

                for (int i = 0; i < cmbInformation1.Items.Count; i++)
                {
                    if (i == 0)
                    {
                        strTemp = cmbInformation1.Items[i].ToString();
                    }
                    else
                    {
                        strTemp = strTemp + "," + cmbInformation1.Items[i].ToString();
                    }

                }

                lineChanger(strTemp, csvPartFileInspectionInputInformation, 3);

            }
        }

        private void btnDelInformation1_Click(object sender, EventArgs e)
        {
            if (cmbInformation1.Text.Trim() == string.Empty)
            {
                MessageBox.Show(this, objLanguage.Translate("Information1 is Blanks"),
                                       objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                                       MessageBoxIcon.Exclamation,
                                       MessageBoxDefaultButton.Button1, 0);
                return;
            }

            int index = cmbInformation1.FindStringExact(cmbInformation1.Text.Trim());

            if (index > -1)
            {
                cmbInformation1.Items.RemoveAt(index);

                if (cmbInformation1.Items.Count == 0)
                {
                    cmbInformation1.Items.Clear();
                    cmbInformation1.Text = string.Empty;
                }

                string strTemp = string.Empty;

                for (int i = 0; i < cmbInformation1.Items.Count; i++)
                {
                    if (i == 0)
                    {
                        strTemp = cmbInformation1.Items[i].ToString();
                    }
                    else
                    {
                        strTemp = strTemp + "," + cmbInformation1.Items[i].ToString();
                    }

                }

                lineChanger(strTemp, csvPartFileInspectionInputInformation, 3);
            }
        }

        private void btnOrderAtoZInformation1_Click(object sender, EventArgs e)
        {
            cmbInformation1.Sorted = true;
        }

        private void btnOrderZtoAInformation1_Click(object sender, EventArgs e)
        {
            cmbInformation1.Sorted = false;

            List<string> TempList = new List<string>();
            for (int i = 0; i < cmbInformation1.Items.Count; i++)
            {

                TempList.Add(cmbInformation1.Items[i].ToString());

            }

            List<string> sortedActionContent = TempList.OrderByDescending(ActionContent => ActionContent).ToList();
            cmbInformation1.Items.Clear();
            foreach (string ActionContentName in sortedActionContent)
                cmbInformation1.Items.Add(ActionContentName);
        }

        private void btnAddInformation2_Click(object sender, EventArgs e)
        {
            if (cmbInformation2.Text.Trim().Replace(",", "") == string.Empty)
            {
                MessageBox.Show(this, objLanguage.Translate("Information2 is Blanks"),
                                       objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                                       MessageBoxIcon.Exclamation,
                                       MessageBoxDefaultButton.Button1, 0);
                return;
            }


            int index = cmbInformation2.FindStringExact(cmbInformation2.Text.Trim().Replace(",", ""));

            if (index == -1)
            {
                cmbInformation2.Items.Add(cmbInformation2.Text.Trim().Replace(",", ""));

                string strTemp = string.Empty;

                for (int i = 0; i < cmbInformation2.Items.Count; i++)
                {
                    if (i == 0)
                    {
                        strTemp = cmbInformation2.Items[i].ToString();
                    }
                    else
                    {
                        strTemp = strTemp + "," + cmbInformation2.Items[i].ToString();
                    }

                }

                lineChanger(strTemp, csvPartFileInspectionInputInformation, 4);

            }
        }

        private void btnDelInformation2_Click(object sender, EventArgs e)
        {
            if (cmbInformation2.Text.Trim() == string.Empty)
            {
                MessageBox.Show(this, objLanguage.Translate("Information2 is Blanks"),
                                       objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                                       MessageBoxIcon.Exclamation,
                                       MessageBoxDefaultButton.Button1, 0);
                return;
            }

            int index = cmbInformation2.FindStringExact(cmbInformation2.Text.Trim());

            if (index > -1)
            {
                cmbInformation2.Items.RemoveAt(index);

                if (cmbInformation2.Items.Count == 0)
                {
                    cmbInformation2.Items.Clear();
                    cmbInformation2.Text = string.Empty;
                }

                string strTemp = string.Empty;

                for (int i = 0; i < cmbInformation2.Items.Count; i++)
                {
                    if (i == 0)
                    {
                        strTemp = cmbInformation2.Items[i].ToString();
                    }
                    else
                    {
                        strTemp = strTemp + "," + cmbInformation2.Items[i].ToString();
                    }

                }

                lineChanger(strTemp, csvPartFileInspectionInputInformation, 4);
            }
        }

        private void btnOrderAtoZInformation2_Click(object sender, EventArgs e)
        {
            cmbInformation2.Sorted = true;
        }

        private void btnOrderZtoAInformation2_Click(object sender, EventArgs e)
        {
            cmbInformation2.Sorted = false;

            List<string> TempList = new List<string>();
            for (int i = 0; i < cmbInformation2.Items.Count; i++)
            {

                TempList.Add(cmbInformation2.Items[i].ToString());

            }

            List<string> sortedActionContent = TempList.OrderByDescending(ActionContent => ActionContent).ToList();
            cmbInformation2.Items.Clear();
            foreach (string ActionContentName in sortedActionContent)
                cmbInformation2.Items.Add(ActionContentName);
        }

        private void btnAddInformation3_Click(object sender, EventArgs e)
        {
            if (cmbInformation3.Text.Trim().Replace(",", "") == string.Empty)
            {
                MessageBox.Show(this, objLanguage.Translate("Information3 is Blanks"),
                                       objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                                       MessageBoxIcon.Exclamation,
                                       MessageBoxDefaultButton.Button1, 0);
                return;
            }


            int index = cmbInformation3.FindStringExact(cmbInformation3.Text.Trim().Replace(",", ""));

            if (index == -1)
            {
                cmbInformation3.Items.Add(cmbInformation3.Text.Trim().Replace(",", ""));

                string strTemp = string.Empty;

                for (int i = 0; i < cmbInformation3.Items.Count; i++)
                {
                    if (i == 0)
                    {
                        strTemp = cmbInformation3.Items[i].ToString();
                    }
                    else
                    {
                        strTemp = strTemp + "," + cmbInformation3.Items[i].ToString();
                    }

                }

                lineChanger(strTemp, csvPartFileInspectionInputInformation, 5);

            }
        }

        private void btnDelInformation3_Click(object sender, EventArgs e)
        {
            if (cmbInformation3.Text.Trim() == string.Empty)
            {
                MessageBox.Show(this, objLanguage.Translate("Information3 is Blanks"),
                                       objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                                       MessageBoxIcon.Exclamation,
                                       MessageBoxDefaultButton.Button1, 0);
                return;
            }

            int index = cmbInformation3.FindStringExact(cmbInformation3.Text.Trim());

            if (index > -1)
            {
                cmbInformation3.Items.RemoveAt(index);

                if (cmbInformation3.Items.Count == 0)
                {
                    cmbInformation3.Items.Clear();
                    cmbInformation3.Text = string.Empty;
                }

                string strTemp = string.Empty;

                for (int i = 0; i < cmbInformation3.Items.Count; i++)
                {
                    if (i == 0)
                    {
                        strTemp = cmbInformation3.Items[i].ToString();
                    }
                    else
                    {
                        strTemp = strTemp + "," + cmbInformation3.Items[i].ToString();
                    }

                }

                lineChanger(strTemp, csvPartFileInspectionInputInformation, 5);
            }
        }

        private void btnOrderAtoZInformation3_Click(object sender, EventArgs e)
        {
            cmbInformation3.Sorted = true;
        }

        private void btnOrderZtoAInformation3_Click(object sender, EventArgs e)
        {
            cmbInformation3.Sorted = false;

            List<string> TempList = new List<string>();
            for (int i = 0; i < cmbInformation3.Items.Count; i++)
            {

                TempList.Add(cmbInformation3.Items[i].ToString());

            }

            List<string> sortedActionContent = TempList.OrderByDescending(ActionContent => ActionContent).ToList();
            cmbInformation3.Items.Clear();
            foreach (string ActionContentName in sortedActionContent)
                cmbInformation3.Items.Add(ActionContentName);
        }

        private void btnAddInformation4_Click(object sender, EventArgs e)
        {
            if (cmbInformation4.Text.Trim().Replace(",", "") == string.Empty)
            {
                MessageBox.Show(this, objLanguage.Translate("Information4 is Blanks"),
                                       objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                                       MessageBoxIcon.Exclamation,
                                       MessageBoxDefaultButton.Button1, 0);
                return;
            }


            int index = cmbInformation4.FindStringExact(cmbInformation4.Text.Trim().Replace(",", ""));

            if (index == -1)
            {
                cmbInformation4.Items.Add(cmbInformation4.Text.Trim().Replace(",", ""));

                string strTemp = string.Empty;

                for (int i = 0; i < cmbInformation4.Items.Count; i++)
                {
                    if (i == 0)
                    {
                        strTemp = cmbInformation4.Items[i].ToString();
                    }
                    else
                    {
                        strTemp = strTemp + "," + cmbInformation4.Items[i].ToString();
                    }

                }

                lineChanger(strTemp, csvPartFileInspectionInputInformation, 6);

            }
        }

        private void btnDelInformation4_Click(object sender, EventArgs e)
        {
            if (cmbInformation4.Text.Trim() == string.Empty)
            {
                MessageBox.Show(this, objLanguage.Translate("Information4 is Blanks"),
                                       objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                                       MessageBoxIcon.Exclamation,
                                       MessageBoxDefaultButton.Button1, 0);
                return;
            }

            int index = cmbInformation4.FindStringExact(cmbInformation4.Text.Trim());

            if (index > -1)
            {
                cmbInformation4.Items.RemoveAt(index);

                if (cmbInformation4.Items.Count == 0)
                {
                    cmbInformation4.Items.Clear();
                    cmbInformation4.Text = string.Empty;
                }

                string strTemp = string.Empty;

                for (int i = 0; i < cmbInformation4.Items.Count; i++)
                {
                    if (i == 0)
                    {
                        strTemp = cmbInformation4.Items[i].ToString();
                    }
                    else
                    {
                        strTemp = strTemp + "," + cmbInformation4.Items[i].ToString();
                    }

                }

                lineChanger(strTemp, csvPartFileInspectionInputInformation, 6);
            }
        }

        private void btnOrderAtoZInformation4_Click(object sender, EventArgs e)
        {
            cmbInformation4.Sorted = true;
        }

        private void btnOrderZtoAInformation4_Click(object sender, EventArgs e)
        {
            cmbInformation4.Sorted = false;

            List<string> TempList = new List<string>();
            for (int i = 0; i < cmbInformation4.Items.Count; i++)
            {

                TempList.Add(cmbInformation4.Items[i].ToString());

            }

            List<string> sortedActionContent = TempList.OrderByDescending(ActionContent => ActionContent).ToList();
            cmbInformation4.Items.Clear();
            foreach (string ActionContentName in sortedActionContent)
                cmbInformation4.Items.Add(ActionContentName);
        }

        private void btnAddInformation5_Click(object sender, EventArgs e)
        {
            if (cmbInformation5.Text.Trim().Replace(",", "") == string.Empty)
            {
                MessageBox.Show(this, objLanguage.Translate("Information5 is Blanks"),
                                       objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                                       MessageBoxIcon.Exclamation,
                                       MessageBoxDefaultButton.Button1, 0);
                return;
            }


            int index = cmbInformation5.FindStringExact(cmbInformation5.Text.Trim().Replace(",", ""));

            if (index == -1)
            {
                cmbInformation5.Items.Add(cmbInformation5.Text.Trim().Replace(",", ""));

                string strTemp = string.Empty;

                for (int i = 0; i < cmbInformation5.Items.Count; i++)
                {
                    if (i == 0)
                    {
                        strTemp = cmbInformation5.Items[i].ToString();
                    }
                    else
                    {
                        strTemp = strTemp + "," + cmbInformation5.Items[i].ToString();
                    }

                }

                lineChanger(strTemp, csvPartFileInspectionInputInformation, 7);

            }
        }

        private void btnDelInformation5_Click(object sender, EventArgs e)
        {
            if (cmbInformation5.Text.Trim() == string.Empty)
            {
                MessageBox.Show(this, objLanguage.Translate("Information5 is Blanks"),
                                       objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                                       MessageBoxIcon.Exclamation,
                                       MessageBoxDefaultButton.Button1, 0);
                return;
            }

            int index = cmbInformation5.FindStringExact(cmbInformation5.Text.Trim());

            if (index > -1)
            {
                cmbInformation5.Items.RemoveAt(index);

                if (cmbInformation5.Items.Count == 0)
                {
                    cmbInformation5.Items.Clear();
                    cmbInformation5.Text = string.Empty;
                }

                string strTemp = string.Empty;

                for (int i = 0; i < cmbInformation5.Items.Count; i++)
                {
                    if (i == 0)
                    {
                        strTemp = cmbInformation5.Items[i].ToString();
                    }
                    else
                    {
                        strTemp = strTemp + "," + cmbInformation5.Items[i].ToString();
                    }

                }

                lineChanger(strTemp, csvPartFileInspectionInputInformation, 7);
            }
        }

        private void btnOrderAtoZInformation5_Click(object sender, EventArgs e)
        {
            cmbInformation5.Sorted = true;
        }

        private void btnOrderZtoAInformation5_Click(object sender, EventArgs e)
        {
            cmbInformation5.Sorted = false;

            List<string> TempList = new List<string>();
            for (int i = 0; i < cmbInformation5.Items.Count; i++)
            {

                TempList.Add(cmbInformation5.Items[i].ToString());

            }

            List<string> sortedActionContent = TempList.OrderByDescending(ActionContent => ActionContent).ToList();
            cmbInformation5.Items.Clear();
            foreach (string ActionContentName in sortedActionContent)
                cmbInformation5.Items.Add(ActionContentName);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtExcelOutput.Text = string.Empty;
            cmbSerial.Text = string.Empty;
            cmbProductNo.Text = string.Empty;
            cmbInformation1.Text = string.Empty;
            cmbInformation2.Text = string.Empty;
            cmbInformation3.Text = string.Empty;
            cmbInformation4.Text = string.Empty;
            cmbInformation5.Text = string.Empty;

        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = objLanguage.Translate("Excel file") + " (*.xlsx)|*.xlsx";
            DialogResult result = openFileDialog1.ShowDialog();


            //FolderBrowserDialog folderDlg = new FolderBrowserDialog();
            //folderDlg.ShowNewFolderButton = true;
            // Show the FolderBrowserDialog.  
            //DialogResult result = folderDlg.ShowDialog();
            if (result == DialogResult.OK)
            {
                txtExcelOutput.Text = openFileDialog1.FileName;

                //if (txtExcelOutput.Text == "System.Windows.Forms.TextBox")
                //{
                //    txtExcelOutput.Text = string.Empty;
                //}

                pathFile = Path.GetDirectoryName(openFileDialog1.FileName);

                //Environment.SpecialFolder root = folderDlg.RootFolder;
            }
        }

        private void cbxAll_CheckedChanged(object sender, EventArgs e)
        {
            if (cbxAll.Checked == true)
            {
                cbxSerial.Checked = true;
                cbxProductNo.Checked = true;
                cbxInformation1.Checked = true;
                cbxInformation2.Checked = true;
                cbxInformation3.Checked = true;
                cbxInformation4.Checked = true;
                cbxInformation5.Checked = true;

            }
            else if (cbxAll.Checked == false)
            {
                cbxSerial.Checked = false;
                cbxProductNo.Checked = false;
                cbxInformation1.Checked = false;
                cbxInformation2.Checked = false;
                cbxInformation3.Checked = false;
                cbxInformation4.Checked = false;
                cbxInformation5.Checked = false;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            //DialogResult dialogResult = MessageBox.Show(objLanguage.Translate("Are you satisfied with this registration information?")
            DialogResult dialogResult = MessageBox.Show(objLanguage.Translate("Are you sure with this registration information?")
            , objLanguage.Translate("Confirmation"),
            MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, 0);
            if (dialogResult == DialogResult.Yes)
            {
                //Next

            }
            else if (dialogResult == DialogResult.No)
            {
                return;
            }
            else if (dialogResult == DialogResult.Cancel)
            {
                this.Close();
                return;
            }


            if (txtExcelOutput.Text.Trim() == string.Empty)
            {
                MessageBox.Show(this, objLanguage.Translate("Excel output path is Blanks"),
                                       objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                                       MessageBoxIcon.Exclamation,
                                       MessageBoxDefaultButton.Button1, 0);
                return;
            }
            if ((cmbSerial.Text.Trim() == string.Empty) && (cbxSerial.Checked == true))
            {
                //MessageBox.Show(this, objLanguage.Translate("Serial is Blanks"),
                MessageBox.Show(this, objLanguage.Translate("Serial NO is Blanks"),
                                   objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                                   MessageBoxIcon.Exclamation,
                                   MessageBoxDefaultButton.Button1, 0);
                return;
            }
            if ((cmbProductNo.Text.Trim() == string.Empty) && (cbxProductNo.Checked == true))
            {
                //MessageBox.Show(this, objLanguage.Translate("Product No is Blanks"),
                MessageBox.Show(this, objLanguage.Translate("Product is Blanks"),
                                   objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                                   MessageBoxIcon.Exclamation,
                                   MessageBoxDefaultButton.Button1, 0);
                return;
            }

            if ((cmbInformation1.Text.Trim() == string.Empty) && (cbxInformation1.Checked == true))
            {
                MessageBox.Show(this, objLanguage.Translate("Information1 is Blanks"),
                                       objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                                       MessageBoxIcon.Exclamation,
                                       MessageBoxDefaultButton.Button1, 0);
                return;
            }

            if ((cmbInformation2.Text.Trim() == string.Empty) && (cbxInformation2.Checked == true))
            {
                MessageBox.Show(this, objLanguage.Translate("Information2 is Blanks"),
                                       objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                                       MessageBoxIcon.Exclamation,
                                       MessageBoxDefaultButton.Button1, 0);
                return;
            }

            if ((cmbInformation3.Text.Trim() == string.Empty) && (cbxInformation3.Checked == true))
            {
                MessageBox.Show(this, objLanguage.Translate("Information3 is Blanks"),
                                       objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                                       MessageBoxIcon.Exclamation,
                                       MessageBoxDefaultButton.Button1, 0);
                return;
            }

            if ((cmbInformation4.Text.Trim() == string.Empty) && (cbxInformation4.Checked == true))
            {
                MessageBox.Show(this, objLanguage.Translate("Information4 is Blanks"),
                                       objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                                       MessageBoxIcon.Exclamation,
                                       MessageBoxDefaultButton.Button1, 0);
                return;
            }

            if ((cmbInformation5.Text.Trim() == string.Empty) && (cbxInformation5.Checked == true))
            {
                MessageBox.Show(this, objLanguage.Translate("Information5 is Blanks"),
                                       objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                                       MessageBoxIcon.Exclamation,
                                       MessageBoxDefaultButton.Button1, 0);
                return;
            }

            if (cmbSerial.Text.Contains('"') && (cbxSerial.Checked == true))
            {
                //MessageBox.Show(this, objLanguage.Translate("Serial : Double quotes are not allowed."),
                MessageBox.Show(this, objLanguage.Translate("Serial NO : Double quotes are not allowed."),
                                  objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                                  MessageBoxIcon.Exclamation,
                                  MessageBoxDefaultButton.Button1, 0);
                return;
            }

            if (cmbProductNo.Text.Contains('"') && (cbxProductNo.Checked == true))
            {
                //MessageBox.Show(this, objLanguage.Translate("Product No : Double quotes are not allowed."),
                MessageBox.Show(this, objLanguage.Translate("Product : Double quotes are not allowed."),
                                  objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                                  MessageBoxIcon.Exclamation,
                                  MessageBoxDefaultButton.Button1, 0);
                return;
            }


            if (cmbInformation1.Text.Contains('"') && (cbxInformation1.Checked == true))
            {
                MessageBox.Show(this, objLanguage.Translate("Information1 : Double quotes are not allowed."),
                                      objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                                      MessageBoxIcon.Exclamation,
                                      MessageBoxDefaultButton.Button1, 0);
                return;
            }

            if (cmbInformation2.Text.Contains('"') && (cbxInformation2.Checked == true))
            {
                MessageBox.Show(this, objLanguage.Translate("Information2 : Double quotes are not allowed."),
                                      objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                                      MessageBoxIcon.Exclamation,
                                      MessageBoxDefaultButton.Button1, 0);
                return;
            }

            if (cmbInformation3.Text.Contains('"') && (cbxInformation3.Checked == true))
            {
                MessageBox.Show(this, objLanguage.Translate("Information3 : Double quotes are not allowed."),
                                      objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                                      MessageBoxIcon.Exclamation,
                                      MessageBoxDefaultButton.Button1, 0);
                return;
            }

            if (cmbInformation4.Text.Contains('"') && (cbxInformation4.Checked == true))
            {
                MessageBox.Show(this, objLanguage.Translate("Information4 : Double quotes are not allowed."),
                                      objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                                      MessageBoxIcon.Exclamation,
                                      MessageBoxDefaultButton.Button1, 0);
                return;
            }

            if (cmbInformation5.Text.Contains('"') && (cbxInformation5.Checked == true))
            {
                MessageBox.Show(this, objLanguage.Translate("Information5 : Double quotes are not allowed."),
                                      objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                                      MessageBoxIcon.Exclamation,
                                      MessageBoxDefaultButton.Button1, 0);
                return;
            }

            pathFile = Path.GetDirectoryName(this.csvPartFile);

            pathFileName = pathFile + "\\";

            if (cbxSerial.Checked == true)
            {
                pathFileName = pathFileName + "" + cmbSerial.Text.Replace('"', ' ').Replace(" ", "");
            }
            if (cbxProductNo.Checked == true)
            {
                pathFileName = pathFileName + "_" + cmbProductNo.Text.Replace('"', ' ').Replace(" ", "");
            }
            if (cbxInformation1.Checked == true)
            {
                pathFileName = pathFileName + "_" + cmbInformation1.Text.Replace('"', ' ').Replace(" ", "");
            }
            if (cbxInformation2.Checked == true)
            {
                pathFileName = pathFileName + "_" + cmbInformation2.Text.Replace('"', ' ').Replace(" ", "");
            }
            if (cbxInformation3.Checked == true)
            {
                pathFileName = pathFileName + "_" + cmbInformation3.Text.Replace('"', ' ').Replace(" ", "");
            }
            if (cbxInformation4.Checked == true)
            {
                pathFileName = pathFileName + "_" + cmbInformation4.Text.Replace('"', ' ').Replace(" ", "");
            }
            if (cbxInformation5.Checked == true)
            {
                pathFileName = pathFileName + "_" + cmbInformation5.Text.Replace('"', ' ').Replace(" ", "");
            }
            //pathFileName = pathFileName + "_" + DateTime.Now.ToString("yyyyMMddHHmmss") 
            //    + CheckSerialUSB.SerialUSBLicense.Substring(CheckSerialUSB.SerialUSBLicense.Length - 5, 5) + ".000";

            CheckSerialUSB.SerialUSB = CheckSerialUSB.CheckSerialUSBLicense();

            CheckSerialUSBWarning();

            if (CheckSerialUSB.SerialUSB.Length >= 5)
            {
                pathFileName = pathFileName + "_" + DateTime.Now.ToString("yyyyMMddHHmm")
                + CheckSerialUSB.SerialUSB.Substring(CheckSerialUSB.SerialUSB.Length - 5, 5) + ".000";

            }
            else
            {
                pathFileName = pathFileName + "_" + DateTime.Now.ToString("yyyyMMddHHmm") + ".000";
            }
            //else
            //{
            //    MessageBox.Show(this, objLanguage.Translate("Serial USB Not correct"),
            //                           objLanguage.Translate("Warning"), MessageBoxButtons.OK,
            //                           MessageBoxIcon.Exclamation,
            //                           MessageBoxDefaultButton.Button1, 0);

            //    return;
            //}

            string path = pathFileName;
            using (FileStream fs = File.Create(path)) ;

            try
            {
                File.Copy(csvPartFile, path, true);

                List<string> tmp = null;
                List<List<string>> Master = new List<List<string>>();
                string strData = string.Empty;
                tmp = null;
                Int32 i = 0;
                using (var csv = new CsvReader(path))
                {
                    while ((tmp = csv.ReadRow()) != null)
                    {

                        Master.Add(tmp);

                        if ((tmp.Count >= 14) && (i == 0))
                        {

                            strData = Master[i][0] + "," + Master[i][1] + "," + Master[i][2] + "," + Master[i][3] + "," + Master[i][4]
                                + "," + Master[i][5] + "," + Master[i][6] + "," + Master[i][7] + "," + Master[i][8] + "," + Master[i][9]
                                + "," + Master[i][10] + "," + Master[i][11] + "," + txtExcelOutput.Text + "," + Master[i][13];

                            break;
                        }

                        i++;
                    }

                }

                lineChanger(strData.Replace("\r", "").Replace("\n", ""), path, 1);

            }
            catch (IOException iox)
            {
                //Console.WriteLine(iox.Message);
                MessageBox.Show(this, iox.Message,
                                       objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                                       MessageBoxIcon.Exclamation,
                                       MessageBoxDefaultButton.Button1, 0);

                return;

            }

            MessageBox.Show(this, objLanguage.Translate("Registration is Complete"),
                                       objLanguage.Translate("Information"), MessageBoxButtons.OK,
                                       MessageBoxIcon.Information,
                                       MessageBoxDefaultButton.Button1, 0);


            FrmOperationManualOperator frmOperationManualOperator = new FrmOperationManualOperator(path, "Inspection Work");

            this.Dispose();

            frmOperationManualOperator.ShowDialog();


        }

        public void CheckSerialUSBWarning()
        {
            if (CheckSerialUSB.SerialUSB == string.Empty)
            {
                MessageBox.Show(this, objLanguage.Translate("Please insert the USB key."),
                                       objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                                       MessageBoxIcon.Exclamation,
                                       MessageBoxDefaultButton.Button1, 0);

                Application.Exit();
            }
            else if (CheckSerialUSB.SerialUSB == "Get")
            {
                MessageBox.Show(this, objLanguage.Translate("Not correct"),
                                       objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                                       MessageBoxIcon.Exclamation,
                                       MessageBoxDefaultButton.Button1, 0);
                Application.Exit();
            }
        }

        private void cmbSerial_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == ',')
            {
                e.Handled = true;
            }
        }

        private void cmbProductNo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == ',')
            {
                e.Handled = true;
            }
        }

        private void cmbInformation1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == ',')
            {
                e.Handled = true;
            }
        }

        private void cmbInformation2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == ',')
            {
                e.Handled = true;
            }
        }

        private void cmbInformation3_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == ',')
            {
                e.Handled = true;
            }
        }

        private void cmbInformation4_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == ',')
            {
                e.Handled = true;
            }
        }

        private void cmbInformation5_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == ',')
            {
                e.Handled = true;
            }
        }
    }
}
