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

using INT63DC_6CH.DialogForms;
using INT63DC_6CH.In_UserManager;
using INT63DC_ImageDll;

namespace INT63DC_6CH.Forms
{
    public partial class FormProgramUpdate : Form
    {
        #region Field
        private FormMain m_ParentForm;
        private DataStore.E_Step3 UpdateStep;

        private bool IsUpdate = false;

        private int Check;

        private string USBPath = "";
        private string UpdateFilePath = "";
        private string OldFilePath = "";
        private string RemoveFolderPath = "";
        private string KeyFolderPath = "";

        private List<string> DllName = new List<string>();
        private List<long> LongOldDllFileLastWriteTime = new List<long>();
        private List<long> LongNewDllFileLastWriteTime = new List<long>();
        #endregion

        #region Constructor
        public FormProgramUpdate(FormMain parent)
        {
            InitializeComponent();

            this.ParentForm = parent;

            this.InitializeDesign();
            this.DefaultSetting();
        }
        #endregion

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

        #region Method
        private void InitializeDesign()
        {
            ImageDll images = new ImageDll();

            switch (this.ParentForm.SystemConfig.Language)
            {
                case DataStore.E_LanguageID.Korean:
                    this.labelTitle.Text = "업데이트";

                    this.buttonLCD.Text = "LCD";
                    this.buttonMainBoard.Text = "Mainboard";
                    this.buttonServiceUpdate.Font = new Font("새굴림", 16, FontStyle.Bold);
                    this.buttonServiceUpdate.Text = "Service Update";
                    break;
                case DataStore.E_LanguageID.English:
                    this.labelTitle.Text = "Update";

                    this.buttonLCD.Text = "LCD";
                    this.buttonMainBoard.Text = "Mainboard";
                    this.buttonServiceUpdate.Font = new Font("새굴림", 16, FontStyle.Bold);
                    this.buttonServiceUpdate.Text = "Service Update";

                    this.buttonBack.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engBackDisable));
                    this.buttonBack.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engBackDown));
                    this.buttonBack.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engBackUp));
                    break;
                case DataStore.E_LanguageID.Chinese:
                    this.labelTitle.Text = "更新";

                    this.buttonLCD.Text = "液晶显示屏";
                    this.buttonMainBoard.Text = "主板";
                    this.buttonServiceUpdate.Font = new Font("새굴림", 16, FontStyle.Bold);
                    this.buttonServiceUpdate.Text = "服务更新";

                    this.buttonBack.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnBackDisable));
                    this.buttonBack.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnBackDown));
                    this.buttonBack.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnBackUp));
                    break;
                case DataStore.E_LanguageID.Czech:
                    break;
                case DataStore.E_LanguageID.German:
                    this.labelTitle.Text = "Aktualisierung";

                    this.buttonLCD.Text = "LCD";
                    this.buttonMainBoard.Text = "Mainboard";
                    this.buttonServiceUpdate.Font = new Font("새굴림", 10, FontStyle.Regular);
                    this.buttonServiceUpdate.Text = "Service-Aktualisierung";

                    this.buttonBack.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.gerBackDisable));
                    this.buttonBack.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.gerBackDown));
                    this.buttonBack.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.gerBackUp));
                    break;
                case DataStore.E_LanguageID.Japanese:
                    this.labelTitle.Text = "更新";

                    this.buttonLCD.Text = "LCD";
                    this.buttonMainBoard.Text = "Mainboard";
                    this.buttonServiceUpdate.Font = new Font("새굴림", 10, FontStyle.Regular);
                    this.buttonServiceUpdate.Text = "Service-Aktualisierung";

                    this.buttonBack.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.jpnBackDisable));
                    this.buttonBack.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.jpnBackDown));
                    this.buttonBack.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.jpnBackUp));
                    break;
                default:
                    break;
            }
        }
        private void DefaultSetting()
        {
            PayService.ServiceDataEvent += PayService_ServiceDataEvent;
            this.DecideUpdatePath();
        }
        private void DecideUpdatePath()
        {
            if (Directory.Exists("Hard Disk\\") == true)
                this.USBPath = "Hard Disk\\";
            else if (Directory.Exists("하드 디스크\\") == true)
                this.USBPath = "하드 디스크\\";
            else
                this.USBPath = "하드 디스크\\";

            this.UpdateFilePath = this.USBPath + "UpdateFiles\\";
            this.KeyFolderPath = this.USBPath + "KEY\\";
            this.OldFilePath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\";
            if (this.OldFilePath.StartsWith("\\F") == true)
                this.RemoveFolderPath = "Flash Disk\\RemoveFile\\";
            else
                this.RemoveFolderPath = "SD Card\\RemoveFile\\";
        }

        private void InitialStatus()
        {
            this.UpdateStep = DataStore.E_Step3.Step1;
            this.UpdateLabelStatus(this.UpdateStep, this.ParentForm.SystemConfig.Language);
            this.buttonLCD.Enabled = true;
            this.buttonMainBoard.Enabled = true;
            this.buttonServiceUpdate.Enabled = true;
            this.labelMessage2.ForeColor = Color.Black;
            this.labelMessage2.Visible = true;
        }
        private void FinishStatusNeededReBoot()
        {
            this.ParentForm.Enabled = false;
            this.UpdateStep = DataStore.E_Step3.Step3;
            this.UpdateLabelStatus(this.UpdateStep, this.ParentForm.SystemConfig.Language);
        }

        public void EnableBackupButton(bool enable)
        {
            this.buttonLCD.Enabled = enable;
            this.buttonMainBoard.Enabled = enable;
            this.buttonServiceUpdate.Enabled = enable;

            if (enable == true)
            {
                this.buttonLCD.TextColor = Color.Black;
                this.buttonMainBoard.TextColor = Color.Black;
                this.buttonServiceUpdate.TextColor = Color.Black;
            }
            else
            {
                this.buttonLCD.TextColor = Color.DimGray;
                this.buttonMainBoard.TextColor = Color.DimGray;
                this.buttonServiceUpdate.TextColor = Color.DimGray;
            }
            this.Refresh();
        }

        private void ProgramUpdateCheck(DialogResult result)
        {
            PayService service = new PayService();
            DataStore.E_UpdateCheck check = DataStore.E_UpdateCheck.Fail;

            if (result == DialogResult.Yes)
            {
                check = this.CheckError();

                if (check == DataStore.E_UpdateCheck.Success)
                {
                    this.buttonMainBoard.TextColor = Color.DimGray;
                    this.buttonMainBoard.Enabled = false;
                    this.buttonLCD.TextColor = Color.DimGray;
                    this.buttonLCD.Enabled = false;
                    this.buttonServiceUpdate.TextColor = Color.DimGray;
                    this.buttonServiceUpdate.Enabled = false;
                    this.UpdateStep = DataStore.E_Step3.Step2;
                    this.UpdateLabelStatus(this.UpdateStep, this.ParentForm.SystemConfig.Language);

                    if (this.IsUpdate == true)
                        this.ProgramUpdate();
                    else
                    {
                        FileInfo[] newFiles;
                        DirectoryInfo directoryFolder = new DirectoryInfo(this.KeyFolderPath);
                        if (directoryFolder.Exists == false)
                            directoryFolder.Create();

                        newFiles = directoryFolder.GetFiles();

                        //Console.Write("{0}, {1} \n", this.KeyFolderPath + newFiles[0].ToString(), newFiles.Length);

                        ErrorLog_e ret = service.PayServiceOpen(this.KeyFolderPath + newFiles[0].ToString(), this.ParentForm.SystemConfig.SerialNumber);
                        if (ret != ErrorLog_e.NONE)
                        {
                            DialogFormMessage message = new DialogFormMessage((int)ret, this.ParentForm.SystemConfig.Language,
                                this.ParentForm.SystemConfig.CurrentForm);
                            message.ShowDialog();

                            this.InitialStatus();
                        }
                    }
                }
                else if (check == DataStore.E_UpdateCheck.NotUsbMomery)
                {
                    DialogFormMessage message = new DialogFormMessage(5, this.ParentForm.SystemConfig.Language);
                    message.ShowDialog();
                    this.IsUpdate = false;
                }
                else if (check == DataStore.E_UpdateCheck.NotUpdateFolder)
                {
                    DialogFormMessage message = new DialogFormMessage(9, this.ParentForm.SystemConfig.Language);
                    message.ShowDialog();
                    this.IsUpdate = false;
                }
                else if (check == DataStore.E_UpdateCheck.NotFile)
                {
                    DialogFormMessage message = new DialogFormMessage(7, this.ParentForm.SystemConfig.Language);
                    message.ShowDialog();
                    this.IsUpdate = false;
                }
                else if (check == DataStore.E_UpdateCheck.TooManyFiles)
                {
                    DialogFormMessage message = new DialogFormMessage(8, this.ParentForm.SystemConfig.Language);
                    message.ShowDialog();
                    this.IsUpdate = false;
                }
            }
        }
        public DataStore.E_UpdateCheck CheckError()
        {
            DataStore.E_UpdateCheck ret = DataStore.E_UpdateCheck.Fail;
            FileInfo[] newFiles;

            DirectoryInfo info = new DirectoryInfo(this.USBPath);
            DirectoryInfo dirFolderSource;

            if (this.IsUpdate == true)
                dirFolderSource = new DirectoryInfo(this.UpdateFilePath);
            else
                dirFolderSource = new DirectoryInfo(this.KeyFolderPath);

            // USB 장착 유무 확인
            if (info.Exists == true)
            {
                // USB에 UpdateFiles 유무 확인
                if (dirFolderSource.Exists == true)
                {
                    newFiles = dirFolderSource.GetFiles();

                    if (newFiles.Length == 0)
                    {
                        ret = DataStore.E_UpdateCheck.NotFile;
                    }
                    else
                    {
                        if (this.IsUpdate == false)
                        {
                            if (newFiles.Length == 1)
                                ret = DataStore.E_UpdateCheck.Success;
                            else
                                ret = DataStore.E_UpdateCheck.TooManyFiles;
                        }
                        else
                            ret = DataStore.E_UpdateCheck.Success;
                    }
                }
                else
                {
                    ret = DataStore.E_UpdateCheck.NotUpdateFolder;
                }
            }
            else
            {
                ret = DataStore.E_UpdateCheck.NotUsbMomery;
            }

            return ret;
        }
        private void CheckErrorMainBoard(DialogResult result)
        {
            FileInfo[] newFiles;
            bool isUpdateFile = false;
            string fileName = "";

            if (result == DialogResult.Yes)
            {
                this.UpdateFilePath = this.USBPath + "UpdateFilesMain\\";

                DirectoryInfo info = new DirectoryInfo(this.USBPath);
                DirectoryInfo directoryFolderUpdateFiles = new DirectoryInfo(this.UpdateFilePath);

                // USB 장착 유무 확인
                if (info.Exists == true)
                {
                    // USB에 UpdateFiles 유무 확인
                    if (directoryFolderUpdateFiles.Exists == true)
                    {
                        newFiles = directoryFolderUpdateFiles.GetFiles();

                        foreach (FileInfo file in newFiles)
                        {
                            fileName = file.Name.ToLower();

                            if (fileName == "int63m_2c_main.int")
                            {
                                isUpdateFile = true;
                                this.UpdateFilePath += "int63m_2c_main.int";

                                break;
                            }
                        }

                        if (newFiles.Length == 0 || isUpdateFile == false)
                        {
                            // USB에 업데이트 파일이 없습니다
                            DialogFormMessage message = new DialogFormMessage(7, this.ParentForm.SystemConfig.Language);
                            message.ShowDialog();
                            return;
                        }
                        else
                        {
                            long a = 0;
                            byte[] byteData, version;
                            string sVersion = "";
                            FileStream fs = new FileStream(this.UpdateFilePath, FileMode.Open, FileAccess.Read);

                            version = new byte[8];
                            BinaryReader br = new BinaryReader(fs);
                            a = br.BaseStream.Length;
                            int i = int.Parse(a.ToString());
                            byteData = br.ReadBytes(i);
                            version[0] = byteData[20];
                            version[1] = byteData[21];
                            version[2] = byteData[22];
                            version[3] = byteData[23];
                            version[4] = byteData[24];
                            version[5] = byteData[25];
                            version[6] = byteData[26];
                            version[7] = byteData[27];
                            sVersion = Encoding.UTF8.GetString(version, 0, version.Length);

                            DialogFormYesNo myDlg = new DialogFormYesNo(this.ParentForm.SystemConfig.Language, 3, this.ParentForm.SystemConfig.MainBoardVersion, sVersion);
                            if (myDlg.ShowDialog() == DialogResult.Yes)
                            {
                                this.UpdateMainBoard();
                            }
                            else
                                return;
                        }
                    }
                    else
                    {
                        // USB에 업데이트 폴더가 없습니다
                        DialogFormMessage message = new DialogFormMessage(9, this.ParentForm.SystemConfig.Language);
                        message.ShowDialog();
                        return;
                    }
                }
                else
                {
                    // USB메모리가 장착되지 않았습니다
                    DialogFormMessage message = new DialogFormMessage(5, this.ParentForm.SystemConfig.Language);
                    message.ShowDialog();
                    return;
                }
            }
        }

        private void ProgramUpdate()
        {
            this.buttonBack.Enabled = false;

            FileInfo[] newFiles;
            DirectoryInfo directoryFolderUpdateFiles = new DirectoryInfo(this.UpdateFilePath);
            if (directoryFolderUpdateFiles.Exists == false)
                directoryFolderUpdateFiles.Create();

            newFiles = directoryFolderUpdateFiles.GetFiles();
            
            this.progressBarUpdate.Value += 20;

            this.TreatmentRemoveOldFile();

            this.progressBarUpdate.Value += 20;

            this.TreatmentCopyNewFile(newFiles);

            this.progressBarUpdate.Value += 100;
            this.ParentForm.SetTrackingHistoryData(DataStore.E_TrackingOperation.Update, "LCD");
            this.FinishStatusNeededReBoot();
        }
        private void TreatmentRemoveOldFile()
        {
            bool removeFolderCheck = false;
            FileInfo[] oldFiles;
            DirectoryInfo directoryFolderRun = new DirectoryInfo(this.OldFilePath);
            DirectoryInfo directoryFolderRemove = new DirectoryInfo(this.RemoveFolderPath);

            removeFolderCheck = directoryFolderRemove.Exists;
            oldFiles = directoryFolderRun.GetFiles();

            if (removeFolderCheck == false)
                directoryFolderRemove.Create();

            foreach (FileInfo subFile in oldFiles)
                subFile.MoveTo(this.RemoveFolderPath + DateTime.Now.ToString("yyyyMMddHHmmss") + subFile.Name.ToString());

            this.progressBarUpdate.Value += 20;
        }
        private void TreatmentCopyNewFile(FileInfo[] newFile)
        {
            // 새 파일 복사
            foreach (FileInfo subFile in newFile)
                subFile.CopyTo(this.OldFilePath + subFile.Name.ToString());

            this.progressBarUpdate.Value += 20;
        }

        private void UpdateMainBoard()
        {
            long a = 0;
            int count = 0, byteCount = 0;

            //this.ParentForm.Enabled = false;
            this.buttonMainBoard.TextColor = Color.DimGray;
            this.buttonMainBoard.Enabled = false;
            this.buttonLCD.TextColor = Color.DimGray;
            this.buttonLCD.Enabled = false;

            try
            {
                this.buttonBack.Enabled = false;

                FileStream fs = new FileStream(this.UpdateFilePath, FileMode.Open, FileAccess.Read);

                BinaryReader br = new BinaryReader(fs);
                a = br.BaseStream.Length;
                byteCount = int.Parse(a.ToString());
                this.ParentForm.UpdateForMain.ByteData = br.ReadBytes(byteCount);

                count = byteCount / 100;
                this.ParentForm.UpdateForMain.SendDataCount = count;

                this.UpdateStep = DataStore.E_Step3.Step2;
                this.UpdateLabelStatus(this.UpdateStep, this.ParentForm.SystemConfig.Language);

                this.timerForMainBoard.Enabled = true;
            }
            catch
            {
                // 로그
                if (this.ParentForm.IsCommunicationLogOpen == true)
                    this.ParentForm.smartFileCommunicationLog.WriteString(string.Format("UpdateMainBoard ({0:yyyy-MM-dd HH:mm:ss}): {1}", DateTime.Now, "Catch"));

                this.buttonBack.Enabled = true;
            }
        }
        public void SetProgressBarValue(int value)
        {
            this.progressBarUpdate.Value += value;
        }

        public void UpdateMainBoardFail(DataStore.E_LanguageID language)
        {
            this.ParentForm.SetTrackingHistoryData(DataStore.E_TrackingOperation.Update, "Mainboard Fail");

            switch (language)
            {
                case DataStore.E_LanguageID.Korean:
                    this.labelMessage1.Text = "업데이트가 실패하였습니다.";
                    this.labelMessage2.Text = "메인보드를 체크해 주시기 바랍니다.";
                    break;
                case DataStore.E_LanguageID.English:
                    this.labelMessage1.Text = "Update Fail!";
                    this.labelMessage2.Text = "Please Check your MainBoard";
                    break;
                case DataStore.E_LanguageID.Chinese:
                    this.labelMessage1.Text = "更新失败！";
                    this.labelMessage2.Text = "请检查您的主板";
                    break;
                case DataStore.E_LanguageID.German:
                    this.labelMessage1.Font = new Font("새굴림", 18, FontStyle.Bold);
                    this.labelMessage1.Text = "Update fehlgeschlagen!";
                    this.labelMessage2.Font = new Font("새굴림", 18, FontStyle.Bold);
                    this.labelMessage2.Text = "Bitte überprüfen Sie Ihr MainBoard";
                    break;
                default:
                    break;
            }
            this.buttonLCD.TextColor = Color.Black;
            this.buttonLCD.Enabled = true;
            this.buttonMainBoard.TextColor = Color.Black;
            this.buttonMainBoard.Enabled = true;
            this.buttonServiceUpdate.TextColor = Color.Black;
            this.buttonServiceUpdate.Enabled = true;
            this.buttonBack.Enabled = true;
        }
        public void UpdateMainBoardFinish()
        {
            this.UpdateStep = DataStore.E_Step3.Step3;
            this.UpdateLabelStatus(this.UpdateStep, this.ParentForm.SystemConfig.Language);
            this.buttonBack.Visible = false;
            this.ParentForm.SetTrackingHistoryData(DataStore.E_TrackingOperation.Update, "Mainboard Success");
        }
        private void UpdateLabelStatus(DataStore.E_Step3 step, DataStore.E_LanguageID language)
        {
            switch (step)
            {
                case DataStore.E_Step3.Step1:
                    #region Step1
                    switch (language)
                    {
                        case DataStore.E_LanguageID.Korean:
                            this.labelMessage1.Text = "USB를 본체에 삽입 하십시오";
                            this.labelMessage2.Text = "업데이트 할 항목을 선택해 주십시오";
                            break;
                        case DataStore.E_LanguageID.English:
                            this.labelMessage1.Text = "Please insert usb into the device";
                            this.labelMessage2.Text = "Please select an item to update";
                            break;
                        case DataStore.E_LanguageID.Chinese:
                            this.labelMessage1.Text = "请将usb插入设备中。";
                            this.labelMessage2.Text = "请选择一个项目来更新";
                            break;
                        case DataStore.E_LanguageID.German:
                            this.labelMessage1.Font = new Font("새굴림", 18, FontStyle.Bold);
                            this.labelMessage1.Text = "USB nicht vom Gerät trennen";
                            this.labelMessage2.Font = new Font("새굴림", 18, FontStyle.Bold);
                            this.labelMessage2.Text = "Drücken Sie nach dem USB-Anschluss die untere Taste";
                            break;
                        default:
                            break;
                    }
                    #endregion
                    break;
                case DataStore.E_Step3.Step2:
                    #region Step2
                    switch (language)
                    {
                        case DataStore.E_LanguageID.Korean:
                            this.labelMessage1.Text = "업데이트 중...";
                            this.labelMessage2.Text = "USB를 본체에서 분리하지 마십시오";
                            break;
                        case DataStore.E_LanguageID.English:
                            this.labelMessage1.Text = "Updating...";
                            this.labelMessage2.Text = "Do not remove the USB from the device";
                            break;
                        case DataStore.E_LanguageID.Chinese:
                            this.labelMessage1.Text = "更新中...";
                            this.labelMessage2.Text = "请勿断开USB";
                            break;
                        case DataStore.E_LanguageID.German:
                            this.labelMessage1.Font = new Font("새굴림", 18, FontStyle.Bold);
                            this.labelMessage1.Text = "Aktualisierung….";
                            this.labelMessage2.Font = new Font("새굴림", 18, FontStyle.Bold);
                            this.labelMessage2.Text = "USB nicht vom Gerät trennen";
                            break;
                        default:
                            break;
                    }
                    #endregion
                    break;
                case DataStore.E_Step3.Step3:
                    #region Step3
                    this.labelMessage2.ForeColor = Color.Red;
                    switch (language)
                    {
                        case DataStore.E_LanguageID.Korean:
                            this.labelMessage1.Text = "프로그램 업데이트 완료";
                            this.labelMessage2.Text = "전원을 끄고 다시 켜주세요";
                            break;
                        case DataStore.E_LanguageID.English:
                            this.labelMessage1.Text = "Program update complete";
                            this.labelMessage2.Text = "Reset the system";
                            break;
                        case DataStore.E_LanguageID.Chinese:
                            this.labelMessage1.Text = "系统更新完成";
                            this.labelMessage2.Text = "关掉电源，然后在打开电源";
                            break;
                        case DataStore.E_LanguageID.German:
                            this.labelMessage1.Font = new Font("새굴림", 18, FontStyle.Bold);
                            this.labelMessage1.Text = "Programmaktualisierung abgeschlossen";
                            this.labelMessage2.Font = new Font("새굴림", 18, FontStyle.Bold);
                            this.labelMessage2.Text = "Mit dem Hauptschalter aus- und einschalten";
                            break;
                        default:
                            break;
                    }
                    #endregion
                    break;
                default:
                    break;
            }

            this.Refresh();
        }

        private void PayService_ServiceDataEvent(SERVICE_ITEMS_e item, SERVICE_MODE_e value)
        {
            //Console.Write("{0} = {1} \n", item.ToString(), value.ToString());
            this.progressBarUpdate.Value = 50;

            //Console.Write("{0}, {1} \n", item.ToString(), value.ToString());

            switch (item)
            {
                case SERVICE_ITEMS_e.MODBUS:
                    #region Modbus
                    switch (value)
                    {
                        case SERVICE_MODE_e.DISABLE:
                            if (this.ParentForm.SystemConfig.IsModbus == true)
                            {
                                this.ParentForm.SystemConfig.IsModbus = false;

                                if (this.ParentForm.SystemConfig.Serial3Mode == (int)DataStore.E_CommMode.f4_Modbus)
                                    this.ParentForm.SystemConfig.Serial3Mode = 0;
                                if (this.ParentForm.SystemConfig.EthernetMode == (int)DataStore.E_CommMode.f4_Modbus)
                                    this.ParentForm.SystemConfig.EthernetMode = 0;

                                this.ParentForm.SaveSystemConfigurationFile(this.ParentForm.SystemConfig);

                                //this.ParentForm.CenterConfiSerial.ChangeCommunicationMode();
                                //this.ParentForm.CenterConfiEthernet.ChangeCommunicationMode();

                                this.labelUpdateContents.Text += "Modbus OFF";
                                this.ParentForm.SetTrackingHistoryData(DataStore.E_TrackingOperation.FunctionUpdate, "Modbus_OFF");
                            }
                            break;
                        case SERVICE_MODE_e.ENABLE:
                            if (this.ParentForm.SystemConfig.IsModbus == false)
                            {
                                this.ParentForm.SystemConfig.IsModbus = true;
                                this.ParentForm.SaveSystemConfigurationFile(this.ParentForm.SystemConfig);

                                //this.ParentForm.CenterConfiSerial.ChangeCommunicationMode();
                                //this.ParentForm.CenterConfiEthernet.ChangeCommunicationMode();

                                this.labelUpdateContents.Text += "Modbus ON";
                                this.ParentForm.SetTrackingHistoryData(DataStore.E_TrackingOperation.FunctionUpdate, "Modbus_ON");
                            }
                            break;
                        default:
                            break;
                    }
                    #endregion
                    break;
                case SERVICE_ITEMS_e.PART11:
                    #region Part11
                    switch (value)
                    {
                        case SERVICE_MODE_e.DISABLE:
                            if (this.ParentForm.SystemConfig.IsPart11 == true)
                            {
                                this.ParentForm.SystemConfig.IsPart11 = false;
                                this.ParentForm.SystemConfig.IsLogin = false;
                                this.ParentForm.SystemConfig.IsDataStatistics = false;
                                this.ParentForm.SystemConfig.IsDataBackup = false;

                                if (this.ParentForm.SystemConfig.StoragePeriod > 12)
                                    this.ParentForm.SystemConfig.StoragePeriod = 12;

                                // Delete user list
                                UserManager.UserManager_UserAllDel();

                                this.ParentForm.SaveSystemConfigurationFile(this.ParentForm.SystemConfig);

                                if (this.labelUpdateContents.Text.Length != 0)
                                    this.labelUpdateContents.Text += ",  ";
                                this.labelUpdateContents.Text += "Part11 OFF";
                                this.ParentForm.SetTrackingHistoryData(DataStore.E_TrackingOperation.FunctionUpdate, "Part11_OFF");
                            }
                            break;
                        case SERVICE_MODE_e.ENABLE:
                            if (this.ParentForm.SystemConfig.IsPart11 == false)
                            {
                                this.ParentForm.SystemConfig.IsLogin = true;
                                this.ParentForm.SystemConfig.IsPart11 = true;
                                this.ParentForm.SystemConfig.IsDataStatistics = true;
                                this.ParentForm.SystemConfig.IsDataBackup = true;

                                if (this.ParentForm.SystemConfig.StoragePeriod <= 12)
                                    this.ParentForm.SystemConfig.StoragePeriod = 24;

                                this.ParentForm.SaveSystemConfigurationFile(this.ParentForm.SystemConfig);

                                if (this.labelUpdateContents.Text.Length != 0)
                                    this.labelUpdateContents.Text += ",  ";
                                this.labelUpdateContents.Text += "Part11 ON";
                                this.ParentForm.SetTrackingHistoryData(DataStore.E_TrackingOperation.FunctionUpdate, "Part11_ON");
                            }
                            break;
                        default:
                            break;
                    }

                    this.FinishStatusNeededReBoot();
                    this.progressBarUpdate.Value += 100;
                    #endregion
                    break;
                default:
                    break;
            }

            this.labelTitleUpdate.Visible = true;
            this.labelUpdateContents.Visible = true;
        }

        public void DisplayRefresh()
        {
            this.ParentForm.SystemConfig.CurrentForm = DataStore.FormStore.FormProgramUpdate;

            this.InitialStatus();
            this.progressBarUpdate.Value = 0;

            // USB 체크
            DirectoryInfo info = new DirectoryInfo("하드 디스크\\");
            if (info.Exists == true)
            {
                this.buttonLCD.TextColor = Color.Black;
                this.buttonLCD.Enabled = true;
                this.buttonMainBoard.TextColor = Color.Black;
                this.buttonMainBoard.Enabled = true;
                this.buttonServiceUpdate.TextColor = Color.Black;
                this.buttonServiceUpdate.Enabled = true;
            }
            else
            {
                this.buttonLCD.TextColor = Color.DimGray;
                this.buttonLCD.Enabled = false;
                this.buttonMainBoard.TextColor = Color.DimGray;
                this.buttonMainBoard.Enabled = false;
                this.buttonServiceUpdate.TextColor = Color.DimGray;
                this.buttonServiceUpdate.Enabled = false;
            }
        }
        #endregion

        #region Event Handler
        private void buttonLCD_Click(object sender, EventArgs e)
        {
            DialogFormYesNo myDlg = new DialogFormYesNo(this.ParentForm.SystemConfig.Language, 3);
            DialogResult dr = myDlg.ShowDialog();
            if (dr == DialogResult.Yes)
                this.IsUpdate = true;

            myDlg.Close();

            this.Refresh();

            this.labelMessage2.Visible = true;
            this.ProgramUpdateCheck(dr);
        }
        private void buttonMainboard_Click(object sender, EventArgs e)
        {
            DialogFormYesNo myDlg = new DialogFormYesNo(this.ParentForm.SystemConfig.Language, 3);
            DialogResult dr = myDlg.ShowDialog();
            myDlg.Close();

            this.Refresh();

            this.CheckErrorMainBoard(dr);
        }
        private void buttonServiceUpdate_Click(object sender, EventArgs e)
        {
            DialogFormYesNo myDlg = new DialogFormYesNo(this.ParentForm.SystemConfig.Language, 3);
            DialogResult dr = myDlg.ShowDialog();
            myDlg.Close();

            PayService service = new PayService();

            this.labelMessage2.Visible = true;
            this.ProgramUpdateCheck(dr);
        }

        private void timerForMainBoard_Tick(object sender, EventArgs e)
        {
            this.timerForMainBoard.Enabled = false;

            this.ParentForm.SendUpdateForMain(this.ParentForm.UpdateForMain.ByteData);
        }

        private void buttonBack_Click(object sender, EventArgs e)
        {
            this.ParentForm.ChildFormMenu.DisplayRefresh();
            ((FormMain)(Owner)).smartForm.Show((int)DataStore.FormStore.FormMenu);
        }
        #endregion
    }
}