using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using ManualSoftwareSystem.lib;
using ManualSoftwareSystem.Operation_Manual;
using Microsoft.VisualBasic;
using static System.Windows.Forms.AxHost;

namespace ManualSoftwareSystem
{
    public partial class FrmMasterMaintenanceNew : Form
    {
        string pathFile = string.Empty;
        string pathFileName = string.Empty;
        string pathFileName003 = string.Empty;
        string loginUser = string.Empty;
        public dynamic objLanguage;

        public FrmMasterMaintenanceNew(string loginUser)
        {
            InitializeComponent();
            this.loginUser = loginUser;
            this.objLanguage = FrmOperationManualOperator.objLanguage;
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            //openFileDialog1.AddExtension = true;
            //openFileDialog1.Multiselect = true;
            //openFileDialog1.ReadOnlyChecked = true;
            //openFileDialog1.ShowReadOnly = true;
            //openFileDialog1.Filter = objLanguage.Translate("Master file") + " (*.003)|*.003";           
            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;

                pathFile = Path.GetDirectoryName(openFileDialog1.FileName);

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

        private void FrmMasterMaintenanceNew_Load(object sender, EventArgs e)
        {
            txtExcelOutput.ReadOnly = true;
            FrmOperationManualOperator.excelPathShare = string.Empty;
            pathFile = System.IO.Directory.GetCurrentDirectory() + "\\Master";

            bool exists = System.IO.Directory.Exists(pathFile);

            if (!exists)
                System.IO.Directory.CreateDirectory(pathFile);

            string strMasterMaintenanceNew = "Master Maintenance New";
            string strItwillbe = "It will be the input file name of the product information.";
            //string strBlanksAndDoubleQuotation = "Blanks and double quotation marks cannot be entered.";
            string strBlanksAndDoubleQuotation = "Blanks and double quotation marks are not allowed.";
            string strFilename = "File name";
            string strNameFormat = "Name(Format)";
            //string strProductNo = "Product No";
            string strProductNo = "Product";
            string strVersion = "Version";
            string strIndex = "Index";
            string strInformation1 = "Information1";
            string strInformation2 = "Information2";
            string strInformation3 = "Information3";
            string strInformation4 = "Information4";
            string strInformation5 = "Information5";
            string strExcelOutputFormatSetting = "Excel output format setting";
            string strSelect = "Select";
            string strClear = "Clear";
            string strSave = "Save";
            string strCancel = "Cancel";


            lblMasterMaintenanceNew.Text = objLanguage.Translate(strMasterMaintenanceNew);
            lblItwillbe.Text = objLanguage.Translate(strItwillbe);
            lblBlanksAndDoubleQuotation.Text = "** " + objLanguage.Translate(strBlanksAndDoubleQuotation);
            lblFilename.Text = objLanguage.Translate(strFilename);
            lblNameFormat.Text = objLanguage.Translate(strNameFormat);
            lblProductNo.Text = objLanguage.Translate(strProductNo);
            lblVersion.Text = objLanguage.Translate(strVersion);
            lblIndex.Text = objLanguage.Translate(strIndex);
            lblInformation1.Text = objLanguage.Translate(strInformation1);
            lblInformation2.Text = objLanguage.Translate(strInformation2);
            lblInformation3.Text = objLanguage.Translate(strInformation3);
            lblInformation4.Text = objLanguage.Translate(strInformation4);
            lblInformation5.Text = objLanguage.Translate(strInformation5);
            lblExcelOutputFormatSetting.Text = objLanguage.Translate(strExcelOutputFormatSetting);
            btnSelect.Text = objLanguage.Translate(strSelect);
            btnClear.Text = objLanguage.Translate(strClear);
            btnSave.Text = objLanguage.Translate(strSave);
            btnCancel.Text = objLanguage.Translate(strCancel);

        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtExcelOutput.Text = string.Empty;
            txtNameFormat.Text = string.Empty;
            txtProductNo.Text = string.Empty;
            txtVersion.Text = string.Empty;
            txtIndex.Text = string.Empty;
            txtInformation1.Text = string.Empty;
            txtInformation2.Text = string.Empty;
            txtInformation3.Text = string.Empty;
            txtInformation4.Text = string.Empty;
            txtInformation5.Text = string.Empty;
        }

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

        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
                FrmOperationManualOperator.excelPathShare = txtExcelOutput.Text;
            }
            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 (txtNameFormat.Text.Trim() == string.Empty)
            {
                MessageBox.Show(this, objLanguage.Translate("Name(Format) is Blanks"),
                                       objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                                       MessageBoxIcon.Exclamation,
                                       MessageBoxDefaultButton.Button1, 0);
                return;
            }
            if (txtProductNo.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;
            }
            if (txtVersion.Text.Trim() == string.Empty)
            {
                MessageBox.Show(this, objLanguage.Translate("Version is Blanks"),
                                       objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                                       MessageBoxIcon.Exclamation,
                                       MessageBoxDefaultButton.Button1, 0);
                return;
            }

            if (txtIndex.Text.Trim() == string.Empty)
            {
                MessageBox.Show(this, objLanguage.Translate("Index is Blanks"),
                                       objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                                       MessageBoxIcon.Exclamation,
                                       MessageBoxDefaultButton.Button1, 0);
                return;
            }

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

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

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

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

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

            if (txtNameFormat.Text.Contains('"'))
            {
                MessageBox.Show(this, objLanguage.Translate("Name(Format) : Double quotes are not allowed."),
                                      objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                                      MessageBoxIcon.Exclamation,
                                      MessageBoxDefaultButton.Button1, 0);
                return;
            }

            if (txtProductNo.Text.Contains('"'))
            {
                //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 (txtVersion.Text.Contains('"'))
            {
                MessageBox.Show(this, objLanguage.Translate("Version : Double quotes are not allowed."),
                                      objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                                      MessageBoxIcon.Exclamation,
                                      MessageBoxDefaultButton.Button1, 0);
                return;
            }

            if (txtIndex.Text.Contains('"'))
            {
                MessageBox.Show(this, objLanguage.Translate("Index : Double quotes are not allowed."),
                                      objLanguage.Translate("Warning"), MessageBoxButtons.OK,
                                      MessageBoxIcon.Exclamation,
                                      MessageBoxDefaultButton.Button1, 0);
                return;
            }

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

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

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

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

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

            pathFileName = pathFile + "\\" + txtNameFormat.Text.Replace('"', ' ').Replace(" ", "")
                  + "_" + txtProductNo.Text.Replace('"', ' ').Replace(" ", "")
                  + "_" + txtVersion.Text.Replace('"', ' ').Replace(" ", "");
            if (txtIndex.Text.Trim() != string.Empty)
            {
                pathFileName = pathFileName + "_" + txtIndex.Text.Replace('"', ' ').Replace(" ", "");
            }
            if (txtInformation1.Text.Trim() != string.Empty)
            {
                pathFileName = pathFileName + "_" + txtInformation1.Text.Replace('"', ' ').Replace(" ", "");
            }
            if (txtInformation2.Text.Trim() != string.Empty)
            {
                pathFileName = pathFileName + "_" + txtInformation2.Text.Replace('"', ' ').Replace(" ", "");
            }
            if (txtInformation3.Text.Trim() != string.Empty)
            {
                pathFileName = pathFileName + "_" + txtInformation3.Text.Replace('"', ' ').Replace(" ", "");
            }
            if (txtInformation4.Text.Trim() != string.Empty)
            {
                pathFileName = pathFileName + "_" + txtInformation4.Text.Replace('"', ' ').Replace(" ", "");
            }
            if (txtInformation5.Text.Trim() != string.Empty)
            {
                pathFileName = pathFileName + "_" + txtInformation5.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)
            {
                pathFileName003 = pathFileName + "_" + DateTime.Now.ToString("yyyyMMddHHmm")
                + CheckSerialUSB.SerialUSB.Substring(CheckSerialUSB.SerialUSB.Length - 5, 5) + ".003";

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

            }
            else
            {
                pathFileName003 = pathFileName + "_" + DateTime.Now.ToString("yyyyMMddHHmm") + ".003";

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

            //}

            string path = pathFileName;
            //using (FileStream fs = File.Create(path)) ;
            if (!File.Exists(path))
            {
                using (StreamWriter sw = File.CreateText(path))
                {
                    sw.WriteLine(",,,False,,,,,,,,," + txtExcelOutput.Text);
                }
            }

            string path003 = pathFileName003;
            //using (FileStream fs = File.Create(path)) ;
            if (!File.Exists(path003))
            {
                using (StreamWriter sw = File.CreateText(path003))
                {
                    sw.WriteLine(",,,False,,,,,,,,," + txtExcelOutput.Text);
                }
            }

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

            FrmMasterMaintenanceDetail frmMasterMaintenanceDetail = new FrmMasterMaintenanceDetail(path, loginUser);

            this.Dispose();

            frmMasterMaintenanceDetail.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();
            }
        }

    }
}
