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

using INT_PT002.Forms;
using INT_PT002.DialogForms;
using INT_PT002.DataStore;

namespace INT_PT002.Controls
{
    public partial class ControlMenuSystemInformation : UserControl
    {
        #region Field
        private FormMenu m_ParentForm;

        private string USBPath = "";
        private string UpdateFileDisplayPath = "";
        private string UpdateFileMainboardPath = "";
        private string RunFilePath = "";
        private string RemoveFolderPath = "";
        #endregion

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

            this.ParentForm = parent;
            this.Initialize();
        }
        #endregion

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

        #region Method
        private void Initialize()
        {
            this.smartGroupBox1.Text = "System > Information";

            this.labelDisplayVersion.Text = "1.0.0";

            this.USBPath = "하드 디스크\\";
            this.UpdateFileDisplayPath = this.USBPath + "UpdateFiles\\";
            this.UpdateFileMainboardPath = this.USBPath + "UpdateFilesMain\\";
            this.RunFilePath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\";
            if (this.RunFilePath.StartsWith("\\F") == true)
                this.RemoveFolderPath = "Flash Disk\\RemoveFile\\";
            else
                this.RemoveFolderPath = "SD Card\\RemoveFile\\";

            this.smartKeyboard1.KeyboardType = SmartX.SmartKeyboard.KEYBOARDTYPES.NORMAL;
            this.smartKeyboard1.Left = 40;
            this.smartKeyboard1.Top = 217;
            this.smartKeyboard1.Width = 832;
            this.smartKeyboard1.Height = 327;
            this.smartKeyboard1.Hide();
        }

        public void UpdateMainBoardVersionDisplay(string version)
        {
            if (version.Length < 8)
                return;

            this.labelControlVersion.Text = version;
        }

        private Define.E_UpdateCheck CheckDisplayError()
        {
            Define.E_UpdateCheck ret = Define.E_UpdateCheck.Fail;
            FileInfo[] newFiles;

            DirectoryInfo info = new DirectoryInfo(this.USBPath);
            DirectoryInfo dirFolderUpdateFiles = new DirectoryInfo(this.UpdateFileDisplayPath);

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

                    if (newFiles.Length == 0)
                    {
                        ret = Define.E_UpdateCheck.NoFile;
                    }
                    else
                    {
                        ret = Define.E_UpdateCheck.Success;
                    }
                }
                else
                {
                    ret = Define.E_UpdateCheck.NoUpdateFolder;
                }
            }
            else
            {
                ret = Define.E_UpdateCheck.NoUsbMomery;
            }

            return ret;
        }
        private void DisplayProgramUpdateCheck(DialogResult result)
        {
            Define.E_UpdateCheck check = Define.E_UpdateCheck.Fail;

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

                if (check == Define.E_UpdateCheck.Success)
                {
                    DialogFormProgressBar progressBar = new DialogFormProgressBar(1, this.ParentForm.ParentForm.SystemConfig.LANGUAGE);
                    DialogResult dialogResult = progressBar.ShowDialog();
                }
                else if (check == Define.E_UpdateCheck.NoUsbMomery)
                {
                    DialogFormMessage message = new DialogFormMessage(5, Define.E_LanguageID.Korean);
                    message.ShowDialog();
                }
                else if (check == Define.E_UpdateCheck.NoUpdateFolder)
                {
                    DialogFormMessage message = new DialogFormMessage(9, Define.E_LanguageID.Korean);
                    message.ShowDialog();
                }
                else if (check == Define.E_UpdateCheck.NoFile)
                {
                    DialogFormMessage message = new DialogFormMessage(7, Define.E_LanguageID.Korean);
                    message.ShowDialog();
                }
            }
        }
        private Define.E_UpdateCheck CheckMainboardError()
        {
            Define.E_UpdateCheck ret = Define.E_UpdateCheck.Fail;
            FileInfo[] newFiles;
            bool isUpdateFile = false;
            string fileName = "";

            DirectoryInfo info = new DirectoryInfo(this.USBPath);
            DirectoryInfo dirFolderUpdateFiles = new DirectoryInfo(this.UpdateFileMainboardPath);

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

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

                        if (fileName == "ibd902m.bin")
                        {
                            isUpdateFile = true;
                            this.UpdateFileMainboardPath += "ibd902m.bin";

                            break;
                        }
                    }

                    if (newFiles.Length == 0)
                    {
                        ret = Define.E_UpdateCheck.NoFile;
                    }
                    else
                    {
                        ret = Define.E_UpdateCheck.Success;
                    }
                }
                else
                {
                    ret = Define.E_UpdateCheck.NoUpdateFolder;
                }
            }
            else
            {
                ret = Define.E_UpdateCheck.NoUsbMomery;
            }

            return ret;
        }
        private void MainboardProgramUpdateCheck(DialogResult result)
        {
            Define.E_UpdateCheck check = Define.E_UpdateCheck.Fail;

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

                if (check == Define.E_UpdateCheck.Success)
                {
                    long a = 0;
                    byte[] byteData, version;
                    string sVersion = "";
                    FileStream fs = new FileStream(this.UpdateFileMainboardPath, 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.ParentForm.SystemConfig.LANGUAGE, 3, this.ParentForm.ParentForm.SystemConfig.MAINBOARD_VERSION, sVersion);
                    if (myDlg.ShowDialog() == DialogResult.Yes)
                    {
                        this.ParentForm.ParentForm.CloseSmartUartLink();

                        DialogFormProgressBar progressBar = new DialogFormProgressBar(2, this.ParentForm.ParentForm.SystemConfig.LANGUAGE, this.ParentForm.ParentForm.UpdateForMain);
                        DialogResult dialogResult = progressBar.ShowDialog();
                        if (dialogResult == DialogResult.OK)
                        {
                            this.ParentForm.ParentForm.OpenSmartUartLink();
                        }
                    }
                    else
                        return;
                }
                else if (check == Define.E_UpdateCheck.NoUsbMomery)
                {
                    DialogFormMessage message = new DialogFormMessage(5, Define.E_LanguageID.Korean);
                    message.ShowDialog();
                }
                else if (check == Define.E_UpdateCheck.NoUpdateFolder)
                {
                    DialogFormMessage message = new DialogFormMessage(9, Define.E_LanguageID.Korean);
                    message.ShowDialog();
                }
                else if (check == Define.E_UpdateCheck.NoFile)
                {
                    DialogFormMessage message = new DialogFormMessage(7, Define.E_LanguageID.Korean);
                    message.ShowDialog();
                }
            }
        }

        public void DisplayRefresh()
        {
            this.ParentForm.ParentForm.CurrentSystemStatus.CurrentDisplay = Define.E_FormStore.FormInformation;
            this.ParentForm.ParentForm.SetDisplayMode(Define.E_DisplayMode.Menu);

            this.smartKeyboard1.Hide();

            this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Read, CommunicationID.MainBoard, CommunicationAddress._1502_ProgramVersion, "");
        }
        #endregion

        #region Event Handler
        private void labelSerialNo_Click(object sender, EventArgs e)
        {
            DialogFormPasswordKeyPad password = new DialogFormPasswordKeyPad(4, this.ParentForm.ParentForm.SystemConfig.LANGUAGE);
            DialogResult dialogResult = password.ShowDialog();
            if (dialogResult == DialogResult.OK)
            {
                this.labelSerialNo.BackGroundColor = Color.White;

                this.smartKeyboard1.TargetInputObject = this.labelSerialNo;

                this.smartKeyboard1.Show();
            }
        }

        private void smartKeyboard1_OnLabelKeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                e.Handled = true;

                this.smartKeyboard1.Hide();

                this.labelSerialNo.BackGroundColor = Color.FromArgb(224, 224, 224);

                this.ParentForm.ParentForm.SystemConfig.SERIAL_NUMBER = this.labelSerialNo.Text;
                this.ParentForm.ParentForm.SaveSystemConfigurationFile(this.ParentForm.ParentForm.SystemConfig);
            }
            else if (e.KeyChar == 27) // ESC 눌렀을 때
            {
                e.Handled = true;

                this.labelSerialNo.BackGroundColor = Color.FromArgb(224, 224, 224);
                this.smartKeyboard1.Hide();
            }
        }
        private void smartKeyboard1_OnXKeyClick(object sender, EventArgs e)
        {
            this.labelSerialNo.BackGroundColor = Color.FromArgb(224, 224, 224);
            this.smartKeyboard1.Hide();
        }

        private void buttonUpdateMainboard_Click(object sender, EventArgs e)
        {
            //DialogFormYesNo myDlg = new DialogFormYesNo(this.ParentForm.ParentForm.SystemConfig.LANGUAGE, 3);
            //DialogResult dr = myDlg.ShowDialog();
            //myDlg.Close();

            this.Refresh();

            this.MainboardProgramUpdateCheck(DialogResult.Yes);
        }
        private void buttonUpdateDisplay_Click(object sender, EventArgs e)
        {
            DialogFormYesNo myDlg = new DialogFormYesNo(this.ParentForm.ParentForm.SystemConfig.LANGUAGE, 3);
            DialogResult dr = myDlg.ShowDialog();
            myDlg.Close();

            this.Refresh();

            this.DisplayProgramUpdateCheck(dr);
        }
        #endregion
    }
}
