using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.IO;
using System.Text;
using System.Windows.Forms;

using ITC81DB_2H.Forms;
using ITC81DB_2H.DialogForms;
using ITC81DB_2H_ImageDll;
using ITC81DB_2H.Datastore;
using ITC81DB_2H.Part11_UserManager;

namespace ITC81DB_2H.Controls
{
    public partial class ControlCenterEquipInitialize : UserControl
    {
        #region Field
        private FormMenu m_ParentForm;
        private Define.E_Step3 InitializeStep;

        private int FlagTimeout;
        #endregion

        #region Constructor
        public ControlCenterEquipInitialize(FormMenu parent)
        {
            InitializeComponent();

            this.ParentForm = parent;

            this.InitializeDesign();
        }
        #endregion

        #region Property
        public FormMenu ParentForm
        {
            get { return this.m_ParentForm; }
            private set { this.m_ParentForm = value; }
        }
        #endregion

        #region Method
        public void InitializeDesign()
        {
            Class1 images = new Class1();

            if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.English)
            {
                this.buttonBackup.Font = this.buttonMainBoardLcd.Font = this.buttonLcd.Font = new Font("새굴림", 18, FontStyle.Bold);
                this.buttonMainBoard.Font = this.buttonSystemInitialization.Font = new Font("새굴림", 18, FontStyle.Bold);

                this.buttonBackup.Text = "Backup";
                this.buttonMainBoardLcd.Text = "Mainboard+LCD";
                this.buttonMainBoard.Text = "Mainboard";
                this.buttonLcd.Text = "Initialization";
                this.buttonSystemInitialization.Text = "System Init";
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Chinese)
            {
                this.buttonBackup.Font = this.buttonMainBoardLcd.Font = this.buttonLcd.Font = new Font("새굴림", 18, FontStyle.Bold);
                this.buttonMainBoard.Font = this.buttonSystemInitialization.Font = new Font("새굴림", 18, FontStyle.Bold);

                this.buttonBackup.Text = "备份";
                this.buttonMainBoardLcd.Text = "主板+LCD";
                this.buttonMainBoard.Text = "主板";
                this.buttonLcd.Text = "初始化";
                this.buttonSystemInitialization.Text = "所有初始化";
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Czech)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Russian)
            {
                this.buttonBackup.Font = this.buttonMainBoardLcd.Font = this.buttonLcd.Font = new Font("새굴림", 13, FontStyle.Bold);
                this.buttonMainBoard.Font = this.buttonSystemInitialization.Font = new Font("새굴림", 13, FontStyle.Bold);

                this.buttonBackup.Text = "Возврат";
                this.buttonMainBoardLcd.Text = "главной платы+LCD";
                this.buttonMainBoard.Text = "главной платы";
                this.buttonLcd.Text = "Инициализация";
                this.buttonSystemInitialization.Text = "Системы";
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.German)
            {
                this.buttonBackup.Font = this.buttonMainBoardLcd.Font = this.buttonLcd.Font = new Font("새굴림", 18, FontStyle.Bold);
                this.buttonMainBoard.Font = this.buttonSystemInitialization.Font = new Font("새굴림", 18, FontStyle.Bold);

                this.buttonBackup.Text = "Sicherung";
                this.buttonMainBoardLcd.Text = "Mainboard+LCD";
                this.buttonMainBoard.Text = "Mainboard";
                this.buttonLcd.Text = "Initialisierung";
                this.buttonSystemInitialization.Text = "System";
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Spanish)
            {

            }
            else
            {
                this.buttonBackup.Font = this.buttonMainBoardLcd.Font = this.buttonLcd.Font = new Font("새굴림", 18, FontStyle.Bold);
                this.buttonMainBoard.Font = this.buttonSystemInitialization.Font = new Font("새굴림", 18, FontStyle.Bold);

                this.buttonBackup.Text = "Backup";
                this.buttonMainBoardLcd.Text = "Mainboard+LCD";
                this.buttonMainBoard.Text = "Mainboard";
                this.buttonLcd.Text = "Initialization";
                this.buttonSystemInitialization.Text = "System Init";
            }
        }
        private void DefaultSetting()
        {
            this.FlagTimeout = 0;
            this.timer.Interval = 500;

            this.progressBarInitialize.Value = 0;

            this.InitializeStep = Define.E_Step3.Step1;
            this.UpdateLabelStatus(this.InitializeStep, this.ParentForm.ParentForm.SystemConfig1.Language);
        }

        private void DeleteCounterFile()
        {
            DirectoryInfo dir = new DirectoryInfo(this.ParentForm.ParentForm.PathSDCardSystemFile2Folder);
            FileInfo[] files = dir.GetFiles();

            if (files.Length > 0)
            {
                foreach (FileInfo subFile in files)
                {
                    if (subFile.Name.StartsWith("counterItem") == true)
                        subFile.Delete();
                }
            }
        }
        private void DeleteFolder(string address)
        {
            DirectoryInfo dir = new DirectoryInfo(address);
            if (dir.Exists == true)
                dir.Delete(true);
        }
        private void CopyFolder(string departure, string destination)
        {
            DirectoryInfo dirDestination = new DirectoryInfo(destination);
            if (dirDestination.Exists == false)
                dirDestination.Create();

            FileInfo[] files;

            DirectoryInfo dirDeparture = new DirectoryInfo(departure);
            if (dirDeparture.Exists == true)
            {
                files = dirDeparture.GetFiles();
                foreach (FileInfo subFile in files)
                    subFile.CopyTo(destination + subFile.Name, true);
            }
        }
        private void MoveToRemoveFileFolder(string address)
        {

            DirectoryInfo dirDestination = new DirectoryInfo(this.ParentForm.ParentForm.PathSDCardRemoveFileFolder);
            if (dirDestination.Exists == false)
                dirDestination.Create();

            FileInfo[] files;

            DirectoryInfo dirDeparture = new DirectoryInfo(address);
            if (dirDeparture.Exists == true)
            {
                files = dirDeparture.GetFiles();
                foreach (FileInfo subFile in files)
                    subFile.MoveTo(this.ParentForm.ParentForm.PathSDCardRemoveFileFolder + string.Format("{0:yyyyMMddHHmmss}", DateTime.Now) + subFile.Name);
            }
        }

        private void InitializeMainBoard(DialogResult result)
        {
            if (result == DialogResult.Yes)
            {
                this.timer.Enabled = true;
                this.progressBarInitialize.Value = 0;

                this.InitializeStep = Define.E_Step3.Step2;
                this.UpdateLabelStatus(this.InitializeStep, this.ParentForm.ParentForm.SystemConfig1.Language);

                this.ParentForm.ParentForm.TransferData(CommunicationCommand.Initialization, CommunicationID.MainBoard);
            }
        }
        private void InitializeLcd(DialogResult result)
        {
            if (result == DialogResult.Yes)
            {
                this.InitializeStep = Define.E_Step3.Step2;
                this.UpdateLabelStatus(this.InitializeStep, this.ParentForm.ParentForm.SystemConfig1.Language);

                this.progressBarInitialize.Value = 0;
                // User 삭제
                UserManager.UserManager_UserAllDel();
                this.progressBarInitialize.Value += 20;
                // SD Card - SystemFile2를 RemoveFiles로 이동
                this.MoveToRemoveFileFolder(this.ParentForm.ParentForm.PathSDCardSystemFile2Folder);
                this.progressBarInitialize.Value += 20;
                // Flash Disk - SystemFile2 이동
                this.CopyFolder(this.ParentForm.ParentForm.PathFlashDiskSystemFile2Folder, this.ParentForm.ParentForm.PathSDCardSystemFile2Folder);
                this.progressBarInitialize.Value += 20;
                this.DeleteCounterFile();
                this.progressBarInitialize.Value += 10;
                this.InitializeCount(this.ParentForm.ParentForm.SystemConfig1.ProductNumber);
                this.progressBarInitialize.Value = 100;

                this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingOperation.Init_LCD, "");

                this.InitializeStep = Define.E_Step3.Step3;
                this.UpdateLabelStatus(this.InitializeStep, this.ParentForm.ParentForm.SystemConfig1.Language);

                this.ParentForm.Enabled = false;
            }
        }
        private void InitializeMainBoardLcd(DialogResult result)
        {
            if (result == DialogResult.Yes)
            {
                this.InitializeStep = Define.E_Step3.Step2;
                this.UpdateLabelStatus(this.InitializeStep, this.ParentForm.ParentForm.SystemConfig1.Language);

                this.progressBarInitialize.Value = 0;
                this.progressBarInitialize.Value += 10;
                // SD Card - SystemFile2를 RemoveFiles로 이동
                this.MoveToRemoveFileFolder(this.ParentForm.ParentForm.PathSDCardSystemFile2Folder);
                this.progressBarInitialize.Value += 10;
                // Flash Disk - SystemFile2 이동
                this.CopyFolder(this.ParentForm.ParentForm.PathFlashDiskSystemFile2Folder, this.ParentForm.ParentForm.PathSDCardSystemFile2Folder);
                this.progressBarInitialize.Value += 10;
                // User 삭제
                UserManager.UserManager_UserAllDel();
                this.progressBarInitialize.Value += 10;
                this.DeleteCounterFile();
                this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingOperation.Init_LCD, "");
                this.progressBarInitialize.Value += 10;
                this.InitializeCount(this.ParentForm.ParentForm.SystemConfig1.ProductNumber);
                this.progressBarInitialize.Value += 10;

                this.timer.Enabled = true;
                this.ParentForm.ParentForm.TransferData(CommunicationCommand.Initialization, CommunicationID.MainBoard);
            }
        }
        private void BackupSystemFile(DialogResult result)
        {
            if (result == DialogResult.Yes)
            {
                this.InitializeStep = Define.E_Step3.Step2;
                this.UpdateLabelStatus(this.InitializeStep, this.ParentForm.ParentForm.SystemConfig1.Language);

                this.progressBarInitialize.Value = 0;
                // Flash Disk - SystemFile1, 2 삭제
                this.progressBarInitialize.Value += 20;
                this.DeleteFolder(this.ParentForm.ParentForm.PathFlashDiskSystemFile1Folder);
                this.progressBarInitialize.Value += 20;
                this.DeleteFolder(this.ParentForm.ParentForm.PathFlashDiskSystemFile2Folder);
                this.progressBarInitialize.Value += 20;

                // SD Card - SystemFile1, 2 이동
                this.CopyFolder(this.ParentForm.ParentForm.PathSDCardSystemFile1Folder, this.ParentForm.ParentForm.PathFlashDiskSystemFile1Folder);
                this.progressBarInitialize.Value += 20;
                this.CopyFolder(this.ParentForm.ParentForm.PathSDCardSystemFile2Folder, this.ParentForm.ParentForm.PathFlashDiskSystemFile2Folder);
                this.progressBarInitialize.Value = 100;

                this.InitializeStep = Define.E_Step3.Step3;
                this.UpdateLabelStatus(this.InitializeStep, this.ParentForm.ParentForm.SystemConfig1.Language);

                this.ParentForm.Enabled = false;
            }
        }
        private void InitializeSystem(DialogResult result)
        {
            if (result == DialogResult.Yes)
            {
                this.InitializeStep = Define.E_Step3.Step2;
                this.UpdateLabelStatus(this.InitializeStep, this.ParentForm.ParentForm.SystemConfig1.Language);

                this.progressBarInitialize.Value = 0;
                // User 삭제
                UserManager.UserManager_UserAllDel();
                this.progressBarInitialize.Value += 20;
                // SystemFile 1, 2 RemoveFile 폴더로 이동
                this.MoveToRemoveFileFolder(this.ParentForm.ParentForm.PathSDCardSystemFile1Folder);
                this.progressBarInitialize.Value += 30;
                this.MoveToRemoveFileFolder(this.ParentForm.ParentForm.PathSDCardSystemFile2Folder);
                this.progressBarInitialize.Value = 100;

                this.InitializeStep = Define.E_Step3.Step3;
                this.UpdateLabelStatus(this.InitializeStep, this.ParentForm.ParentForm.SystemConfig1.Language);

                this.ParentForm.Enabled = false;
            }
        }
        private void InitializeCount(int productNumber)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(Helper.StringZeroFillDigits4(productNumber.ToString()));      // 품번(4)
            sb.Append("0000000");                                                                                   // ExNG 카운트(7)
            sb.Append("0000000");                                                                                   // 상한 카운트(7)
            sb.Append("0000000");                                                                                   // 기준 카운트(7)
            sb.Append("0000000");                                                                                   // 하한 카운트(7)
            sb.Append("0000000");                                                                                   // 금속 카운트(7)
            sb.Append("0000000");                                                                                   // 이중 카운트(7)
            sb.Append("0000000");                                                                                   // Dummy3(7)
            sb.Append("0000000");                                                                                   // Dummy4(7)
            sb.Append("0000");                                                                                        // Dummy1(4)

            this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.ParameterCount, sb.ToString());
        }

        private void UpdateLabelStatus(Define.E_Step3 step, Define.E_LanguageID language)
        {
            switch (step)
            {
                case Define.E_Step3.Step1:
                    #region Step1
                    switch (language)
                    {
                        case Define.E_LanguageID.Korean:
                            this.labelMessage1.Font = new Font("새굴림", 17, FontStyle.Regular);
                            this.labelMessage1.Text = "하단 'Initialization' 버튼을 눌러주십시오";
                            this.labelMessage2.Text = "";
                            break;
                        case Define.E_LanguageID.English:
                            this.labelMessage1.Font = new Font("새굴림", 17, FontStyle.Regular);
                            this.labelMessage1.Text = "Please click the 'Initialization' button";
                            this.labelMessage2.Text = "at the bottom";
                            break;
                        case Define.E_LanguageID.Chinese:
                            this.labelMessage1.Font = new Font("새굴림", 17, FontStyle.Regular);
                            this.labelMessage1.Text = "请点击底部的'初始化'按钮";
                            this.labelMessage2.Text = "";
                            break;
                        case Define.E_LanguageID.Russian:
                            this.labelMessage1.Font = new Font("새굴림", 12, FontStyle.Regular);
                            this.labelMessage1.Text = "Нажмите кнопку 'Инициализация' внизу";
                            this.labelMessage2.Font = new Font("새굴림", 12, FontStyle.Regular);
                            this.labelMessage2.Text = "";
                            break;
                        case Define.E_LanguageID.German:
                            this.labelMessage1.Font = new Font("새굴림", 12, FontStyle.Regular);
                            this.labelMessage1.Text = "Bitte klicken Sie unten auf die Schaltfläche 'Initialisierung'";
                            this.labelMessage2.Text = "";
                            break;
                        case Define.E_LanguageID.Spanish:
                            this.labelMessage1.Font = new Font("새굴림", 12, FontStyle.Regular);
                            this.labelMessage1.Text = "";
                            this.labelMessage2.Text = "";
                            break;
                        default:
                            break;
                    }
                    #endregion
                    break;
                case Define.E_Step3.Step2:
                    #region Step2
                    switch (language)
                    {
                        case Define.E_LanguageID.Korean:
                            this.labelMessage1.Font = new Font("새굴림", 17, FontStyle.Regular);
                            this.labelMessage1.Text = "초기화 중...";
                            this.labelMessage2.Text = "";
                            break;
                        case Define.E_LanguageID.English:
                            this.labelMessage1.Font = new Font("새굴림", 17, FontStyle.Regular);
                            this.labelMessage1.Text = "Initializing...";
                            this.labelMessage2.Text = "";
                            break;
                        case Define.E_LanguageID.Chinese:
                            this.labelMessage1.Font = new Font("새굴림", 17, FontStyle.Regular);
                            this.labelMessage1.Text = "初始化中...";
                            this.labelMessage2.Text = "";
                            break;
                        case Define.E_LanguageID.Russian:
                            this.labelMessage1.Font = new Font("새굴림", 12, FontStyle.Regular);
                            this.labelMessage1.Text = "Выполняется инициализация...";
                            this.labelMessage2.Text = "";
                            break;
                        case Define.E_LanguageID.German:
                            this.labelMessage1.Font = new Font("새굴림", 12, FontStyle.Regular);
                            this.labelMessage1.Text = "Initialisierung wird ausgeführt...";
                            this.labelMessage2.Text = "";
                            break;
                        case Define.E_LanguageID.Spanish:
                            this.labelMessage1.Font = new Font("새굴림", 12, FontStyle.Regular);
                            this.labelMessage1.Text = "";
                            this.labelMessage2.Text = "";
                            break;
                        default:
                            break;
                    }
                    #endregion
                    break;
                case Define.E_Step3.Step3:
                    #region Step3
                    switch (language)
                    {
                        case Define.E_LanguageID.Korean:
                            this.labelMessage1.Font = new Font("새굴림", 17, FontStyle.Regular);
                            this.labelMessage1.Text = "초기화 완료";
                            this.labelMessage2.Font = new Font("새굴림", 17, FontStyle.Regular);
                            this.labelMessage2.Text = "전원을 끄고 다시 켜주세요.";
                            break;
                        case Define.E_LanguageID.English:
                            this.labelMessage1.Font = new Font("새굴림", 17, FontStyle.Regular);
                            this.labelMessage1.Text = "Initialization complete";
                            this.labelMessage2.Font = new Font("새굴림", 17, FontStyle.Regular);
                            this.labelMessage2.Text = "Reset the power.";
                            break;
                        case Define.E_LanguageID.Chinese:
                            this.labelMessage1.Font = new Font("새굴림", 17, FontStyle.Regular);
                            this.labelMessage1.Text = "初始化完成";
                            this.labelMessage2.Font = new Font("새굴림", 17, FontStyle.Regular);
                            this.labelMessage2.Text = "关掉电源，然后在打开电源";
                            break;
                        case Define.E_LanguageID.Russian:
                            this.labelMessage1.Font = new Font("새굴림", 12, FontStyle.Regular);
                            this.labelMessage1.Text = "Инициализация завершена";
                            this.labelMessage2.Font = new Font("새굴림", 12, FontStyle.Regular);
                            this.labelMessage2.Text = "Выключите питание и снова включите.";
                            break;
                        case Define.E_LanguageID.German:
                            this.labelMessage1.Font = new Font("새굴림", 12, FontStyle.Regular);
                            this.labelMessage1.Text = "Die Initialisierung ist abgeschlossen";
                            this.labelMessage2.Font = new Font("새굴림", 12, FontStyle.Regular);
                            this.labelMessage2.Text = "Mit dem Hauptschalter aus- und einschalten";
                            break;
                        case Define.E_LanguageID.Spanish:
                            this.labelMessage1.Font = new Font("새굴림", 12, FontStyle.Regular);
                            this.labelMessage1.Text = "";
                            this.labelMessage2.Text = "";
                            break;
                        default:
                            break;
                    }
                    #endregion
                    break;
                default:
                    break;
            }

            this.Refresh();
        }
        public void UpdateDisplayMainBoardInitialization()
        {
            this.timer.Enabled = false;

            this.progressBarInitialize.Value = 100;

            this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingOperation.Init_Main, "");

            this.InitializeStep = Define.E_Step3.Step3;
            this.UpdateLabelStatus(this.InitializeStep, this.ParentForm.ParentForm.SystemConfig1.Language);

            this.ParentForm.Enabled = false;
        }

        public void DisplayRefresh(SystemStatus status)
        {
            this.ParentForm.ParentForm.CurrentSystemStatus.CurrentDisplay = Define.E_DisplayStore.EquipInitialize;
            this.ParentForm.ParentForm.SetDisplayMode(Define.E_DisplayMode.Menu);
            this.ParentForm.DisplayTitleRoot(this.ParentForm.ParentForm.CurrentSystemStatus);

            this.DefaultSetting();

            if (status.CurrentUser.Group == Define.E_UserGroup.Admin && this.ParentForm.ParentForm.SystemConfig1.IsLogin == true)
                this.DisplayHiddenMenu(true);
            else
                this.DisplayHiddenMenu(false);
        }
        public void DisplayHiddenMenu(bool visible)
        {
            //if (visible == false)
            //{
            //    this.buttonBackup.Visible = false;
            //    this.buttonSystemInitialization.Visible = false;
            //}
            //else
            //{
            //    this.buttonBackup.Visible = true;
            //    this.buttonSystemInitialization.Visible = true;
            //}
        }

        #region 사용안함
        //private void InitializeLcd(DialogResult result)
        //{
        //    if (result == DialogResult.Yes)
        //    {
        //        this.InitializeStep = Define.E_Step3.Step2;
        //        this.UpdateLabelStatus(this.InitializeStep, this.ParentForm.ParentForm.SystemConfig1.Language);

        //        this.progressBarInitialize.Value = 0;
        //        this.progressBarInitialize.Value += 20;
        //        //this.ResetConfigurationFile();
        //        this.progressBarInitialize.Value += 20;
        //        this.ResetProductFile();
        //        this.progressBarInitialize.Value += 20;
        //        this.ResetJudgmentSetFile();
        //        this.progressBarInitialize.Value += 20;
        //        this.ResetCountFile();
        //        this.progressBarInitialize.Value = 100;

        //        this.InitializeStep = Define.E_Step3.Step3;
        //        this.UpdateLabelStatus(this.InitializeStep, this.ParentForm.ParentForm.SystemConfig1.Language);

        //        this.ParentForm.Enabled = false;
        //    }
        //}
        //private void InitializeMainBoardLcd(DialogResult result)
        //{
        //    if (result == DialogResult.Yes)
        //    {
        //        this.InitializeStep = Define.E_Step3.Step2;
        //        this.UpdateLabelStatus(this.InitializeStep, this.ParentForm.ParentForm.SystemConfig1.Language);

        //        this.progressBarInitialize.Value = 0;
        //        //this.ResetConfigurationFile();
        //        this.progressBarInitialize.Value += 15;
        //        this.ResetProductFile();
        //        this.progressBarInitialize.Value += 15;
        //        this.ResetJudgmentSetFile();
        //        this.progressBarInitialize.Value += 15;
        //        this.ResetCountFile();
        //        this.progressBarInitialize.Value += 15;

        //        this.timer.Enabled = true;
        //        this.ParentForm.ParentForm.TransferData(CommunicationCommand.Initialization, CommunicationID.MainBoard);
        //    }
        //}

        //private void ResetProductFile()
        //{
        //    bool fileCheck = false;
        //    string fullFilePath = "";
        //    StructProductItem structItem;

        //    fullFilePath = this.ParentForm.ParentForm.PathSDCardSystemFile1Folder + "ProductItem.int";
        //    this.ParentForm.ParentForm.smartFileIO.FilePathName = fullFilePath;

        //    FileInfo fileInfo = new FileInfo(fullFilePath);
        //    fileCheck = fileInfo.Exists;

        //    this.ParentForm.ParentForm.smartFileIO.Open(this.ParentForm.ParentForm.BufferSmartUart);

        //    this.ParentForm.ParentForm.CurrentProductItem.Initialization();

        //    for (int i = 0; i < 1000; i++)
        //    {
        //        structItem.Number = i + 1;
        //        structItem.Name = string.Format("Product {0}", i + 1);
        //        structItem.LotNo = string.Format("Lot {0}", i + 1);
        //        structItem.OverRange = this.ParentForm.ParentForm.CurrentProductItem.OverRange;
        //        structItem.PassRange = this.ParentForm.ParentForm.CurrentProductItem.PassRange;
        //        structItem.UnderRange = this.ParentForm.ParentForm.CurrentProductItem.UnderRange;
        //        structItem.TareRange = this.ParentForm.ParentForm.CurrentProductItem.TareRange;
        //        structItem.DummyString1 = "";
        //        structItem.DummyString2 = "";
        //        structItem.DummyString3 = "";
        //        structItem.DummyString4 = "";

        //        this.ParentForm.ParentForm.smartFileIO.WriteStructure_Begin(structItem, i);
        //    }
        //    this.ParentForm.ParentForm.smartFileIO.WriteStructure_End();
        //    this.ParentForm.ParentForm.smartFileIO.Close();
        //}
        //private void ResetJudgmentSetFile()
        //{
        //    bool fileCheck = false;
        //    string fullFilePath = "";
        //    StructJudgmentSetItem structItem;

        //    fullFilePath = this.ParentForm.ParentForm.PathSDCardSystemFile1Folder + "JudgmentSetItem.int";
        //    this.ParentForm.ParentForm.smartFileIO.FilePathName = fullFilePath;

        //    FileInfo fileInfo = new FileInfo(fullFilePath);
        //    fileCheck = fileInfo.Exists;

        //    this.ParentForm.ParentForm.smartFileIO.Open(this.ParentForm.ParentForm.BufferSmartUart);

        //    this.ParentForm.ParentForm.CurrentJudgmentSetItem.Initialization();

        //    for (int i = 0; i < 1000; i++)
        //    {
        //        structItem.Filter = this.ParentForm.ParentForm.CurrentJudgmentSetItem.Filter;
        //        structItem.JudgmentDelayTime = this.ParentForm.ParentForm.CurrentJudgmentSetItem.JudgmentDelayTime;
        //        structItem.DoubleDelayTime = this.ParentForm.ParentForm.CurrentJudgmentSetItem.DoubleDelayTime;
        //        structItem.JudgmentCount = this.ParentForm.ParentForm.CurrentJudgmentSetItem.JudgmentCount;
        //        structItem.FeedSpeed1 = this.ParentForm.ParentForm.CurrentJudgmentSetItem.FeedSpeed1;
        //        structItem.FeedSpeed2 = this.ParentForm.ParentForm.CurrentJudgmentSetItem.FeedSpeed2;
        //        structItem.FeedSpeed3 = this.ParentForm.ParentForm.CurrentJudgmentSetItem.FeedSpeed3;
        //        structItem.DynamicCorrection = this.ParentForm.ParentForm.CurrentJudgmentSetItem.DynamicCorrection;

        //        structItem.Sorter1Mode = this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter1Mode;
        //        structItem.Sorter1DelayTime = this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter1DelayTime;
        //        structItem.Sorter1RunTime = this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter1RunTime;
        //        structItem.Sorter2Mode = this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter2Mode;
        //        structItem.Sorter2DelayTime = this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter2DelayTime;
        //        structItem.Sorter2RunTime = this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter2RunTime;

        //        structItem.AutoJudgmentLength = this.ParentForm.ParentForm.CurrentJudgmentSetItem.AutoJudgmentLength;
        //        structItem.AutoJudgment1 = this.ParentForm.ParentForm.CurrentJudgmentSetItem.AutoJudgment1;
        //        structItem.AutoJudgment2 = this.ParentForm.ParentForm.CurrentJudgmentSetItem.AutoJudgment2;
        //        structItem.AutoJudgment3 = this.ParentForm.ParentForm.CurrentJudgmentSetItem.AutoJudgment3;

        //        structItem.DummyInt1 = 0;
        //        structItem.DummyInt2 = 0;
        //        structItem.DummyInt3 = 0;
        //        structItem.DummyInt4 = 0;
        //        structItem.DummyInt5 = 0;
        //        structItem.DummyString1 = "";
        //        structItem.DummyString2 = "";
        //        structItem.DummyString3 = "";
        //        structItem.DummyString4 = "";
        //        structItem.DummyString5 = "";

        //        this.ParentForm.ParentForm.smartFileIO.WriteStructure_Begin(structItem, i);
        //    }
        //    this.ParentForm.ParentForm.smartFileIO.WriteStructure_End();
        //    this.ParentForm.ParentForm.smartFileIO.Close();
        //}
        //private void ResetCountFile()
        //{
        //    string fullFilePath = "";
        //    bool fileCheck = false;
        //    StructCounterItem structItem;
        //    WeightData item = new WeightData();

        //    fullFilePath = this.ParentForm.ParentForm.PathSDCardSystemFile1Folder + "counterItem.int";
        //    this.ParentForm.ParentForm.smartFileIO.FilePathName = fullFilePath;

        //    FileInfo fileInfo = new FileInfo(fullFilePath);
        //    fileCheck = fileInfo.Exists;

        //    this.ParentForm.ParentForm.smartFileIO.Open(this.ParentForm.ParentForm.BufferSmartUart);

        //    item.ClearCount();

        //    for (int i = 0; i < 1000; i++)
        //    {
        //        structItem.OverCount = item.OverCount;
        //        structItem.OverSumWeight = item.OverSumWeight;
        //        structItem.PassCount = item.PassCount;
        //        structItem.PassSumWeight = item.PassSumWeight;
        //        structItem.UnderCount = item.UnderCount;
        //        structItem.UnderSumWeight = item.UnderSumWeight;
        //        structItem.ExNGCount = item.ExNGCount;

        //        structItem.NormalDistribution1 = item.CollectionNormalDistributionCount[0];
        //        structItem.NormalDistribution2 = item.CollectionNormalDistributionCount[1];
        //        structItem.NormalDistribution3 = item.CollectionNormalDistributionCount[2];
        //        structItem.NormalDistribution4 = item.CollectionNormalDistributionCount[3];
        //        structItem.NormalDistribution5 = item.CollectionNormalDistributionCount[4];
        //        structItem.NormalDistribution6 = item.CollectionNormalDistributionCount[5];
        //        structItem.NormalDistribution7 = item.CollectionNormalDistributionCount[6];
        //        structItem.NormalDistribution8 = item.CollectionNormalDistributionCount[7];
        //        structItem.NormalDistribution9 = item.CollectionNormalDistributionCount[8];
        //        structItem.NormalDistribution10 = item.CollectionNormalDistributionCount[9];

        //        structItem.StartTime = item.StartTime;
        //        structItem.StopTime = item.StopTime;

        //        structItem.DummyInt1 = 0;
        //        structItem.DummyInt2 = 0;
        //        structItem.DummyInt3 = 0;
        //        structItem.DummyInt4 = 0;
        //        structItem.DummyInt5 = 0;

        //        this.ParentForm.ParentForm.smartFileIO.WriteStructure_Begin(structItem, i);
        //    }
        //    this.ParentForm.ParentForm.smartFileIO.WriteStructure_End();
        //    this.ParentForm.ParentForm.smartFileIO.Close();
        //}
        #endregion
        #endregion

        #region Event Handler
        private void buttonMainBoardLcd_Click(object sender, EventArgs e)
        {
            // 설정된 모든 데이터가 초기화됩니다.
            DialogFormYesNo myDlg = new DialogFormYesNo(this.ParentForm.ParentForm.SystemConfig1.Language, 5);
            DialogResult dr = myDlg.ShowDialog();
            myDlg.Close();
            
            this.Refresh();
            this.InitializeMainBoardLcd(dr);
        }

        private void buttonMainBoard_Click(object sender, EventArgs e)
        {
            // 메인보드에 설정된 데이터가 초기화됩니다.
            DialogFormYesNo myDlg = new DialogFormYesNo(this.ParentForm.ParentForm.SystemConfig1.Language, 7);
            DialogResult dr = myDlg.ShowDialog();
            myDlg.Close();

            this.Refresh();
            this.InitializeMainBoard(dr);
        }

        private void buttonLcd_Click(object sender, EventArgs e)
        {
            // LCD에 설정된 데이터가 초기화됩니다.
            DialogFormYesNo myDlg = new DialogFormYesNo(this.ParentForm.ParentForm.SystemConfig1.Language, 6);
            DialogResult dr = myDlg.ShowDialog();
            myDlg.Close();

            this.Refresh();
            this.InitializeLcd(dr);
        }

        private void buttonBackup_Click(object sender, EventArgs e)
        {
            // 현재 데이터를 백업하시겠습니까?
            DialogFormYesNo myDlg = new DialogFormYesNo(this.ParentForm.ParentForm.SystemConfig1.Language, 23);
            DialogResult dr = myDlg.ShowDialog();
            myDlg.Close();

            this.Refresh();
            this.BackupSystemFile(dr);
        }

        private void buttonSystemInitialization_Click(object sender, EventArgs e)
        {
            // 설정된 모든 데이터가 초기화됩니다.
            DialogFormYesNo myDlg = new DialogFormYesNo(this.ParentForm.ParentForm.SystemConfig1.Language, 5);
            DialogResult dr = myDlg.ShowDialog();
            myDlg.Close();

            this.Refresh();
            this.InitializeSystem(dr);
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            this.FlagTimeout++;
            if (this.FlagTimeout == 10)
            {
                this.timer.Enabled = false;

                // 메인보드 통신 상태 확인해주세요
                DialogFormMessage message = new DialogFormMessage(22, this.ParentForm.ParentForm.SystemConfig1.Language);
                message.ShowDialog();
            }

            this.progressBarInitialize.Value += 2;
        }

        private void buttonBackup2_Click(object sender, EventArgs e)
        {
            bool directoryCheck = false;
            string filePathSystemFileBackup = "", filePathSystemFile = "";
            FileInfo[] files;

            filePathSystemFile = this.ParentForm.ParentForm.PathSDCardSystemFile1Folder;
            filePathSystemFileBackup = this.ParentForm.ParentForm.PathSDCardSystemFileBackupFolder;

            DirectoryInfo dirSystemFileFolder = new DirectoryInfo(filePathSystemFile);
            DirectoryInfo dirSystemFileBackupFolder = new DirectoryInfo(filePathSystemFileBackup);

            // SystemFileBackup 폴더 체크
            directoryCheck = dirSystemFileBackupFolder.Exists;
            if (directoryCheck == false)
                dirSystemFileBackupFolder.Create();
            else
            {
                files = dirSystemFileBackupFolder.GetFiles();

                foreach (FileInfo file in files)
                    file.Delete();
            }

            files = dirSystemFileFolder.GetFiles();

            foreach (FileInfo file in files)
            {
                file.CopyTo(filePathSystemFileBackup + file.Name, true);
            }
        }
        #endregion
    }
}
