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

using SmartX;
using INT63DC_ImageDll;
using INT63DC_6CH.DialogForms;
using INT63DC_6CH.In_UserManager;
using INT63DC_6CH.In_Encryption;

namespace INT63DC_6CH.Forms
{
    public partial class FormInformation : Form
    {
        #region Field
        private FormMain m_ParentForm;
        #endregion

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

            this.ParentForm = parent;
        }
        #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:
                    break;
                case DataStore.E_LanguageID.English:
                    #region English
                    this.labelTitle.Text = "Information";

                    this.buttonBack.DownImage = 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)); 
                    #endregion
                    break;
                case DataStore.E_LanguageID.Chinese:
                    #region Chinese
                    this.labelTitle.Text = "信息";

                    this.buttonBack.DownImage = 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)); 
                    #endregion
                    break;
                case DataStore.E_LanguageID.Czech:
                    #region Czech
                    this.labelTitle.Text = "Informace o systému";

                    this.buttonBack.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.czeBackDisable));
                    this.buttonBack.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.czeBackDown));
                    this.buttonBack.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.czeBackUp)); 
                    #endregion
                    break;
                case DataStore.E_LanguageID.German:
                    #region German
                    this.labelTitle.Text = "Systeminformation";

                    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)); 
                    #endregion
                    break;
                case DataStore.E_LanguageID.Japanese:
                    #region Japanese
                    this.labelTitle.Text = "情報";

                    this.buttonBack.DownImage = 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)); 
                    #endregion
                    break;
                default:
                    break;
            }
        }
        private void InitializeAsInfomation()
        {
            bool fileCheck = false;
            string filePath = "";

            filePath = this.ParentForm.PathSystemFileFolder + "ASinfo.txt";
            this.ParentForm.smartFileIO.FilePathName = filePath;

            FileInfo fileInfo = new FileInfo(filePath);
            fileCheck = fileInfo.Exists;

            if (fileCheck == true)
            {
                this.ParentForm.smartFileIO.Open(this.ParentForm.BufferSmartUart);
                this.DisplayAsInfo(this.ParentForm.SystemConfig.Language);
            }
            else
            {
                this.ParentForm.smartFileIO.Open(this.ParentForm.BufferSmartUart);
                this.CreateASInfo();
                this.DisplayAsInfo(this.ParentForm.SystemConfig.Language);
            }

            this.ParentForm.smartFileIO.Close();

            //주소가 한줄일 경우 Email,Website라벨 위치 조정
            if (this.labelAddress2.Text == "")
            {
                this.labelAddress2.Visible = false;
                this.labelEmail.Location = new Point(224, 469);
                this.labelWebsite.Location = new Point(224, 515);
            }
            else
            {
                this.labelAddress2.Visible = true;
                this.labelEmail.Location = new Point(224, 515);
                this.labelWebsite.Location = new Point(224, 561);
            }
        }
        private void DefaultSetting()
        {
            this.labelMainControlVer.Text = "-";
            this.labelDisplayVer.Text = "1.1.1";
            this.labelUserMgerVer.Text = "";
            this.labelAes128Ver.Text = "";
            this.labelSerialPortVer.Text = "";
            this.labelSerialNo.Text = this.ParentForm.SystemConfig.SerialNumber;
        }
        private bool UI_Invoke(ThreadStart invoker)
        {
            try
            {
                if (this.InvokeRequired)
                {
                    if (this.IsDisposed)
                        return true;

                    this.Invoke(invoker);
                }
                else
                {
                    invoker();
                }

                return true;

            }
            catch (Exception e)
            {
                return false;
            }
        }

        private void CreateASInfo()
        {
            // ASinfo 생성(기본 : 인테크)
            this.ParentForm.smartFileIO.WriteString("(주)인테크코리아", 0);
            this.ParentForm.smartFileIO.WriteString("Tel.82-32-681-0714 / Fax.82-32-681-0713", 1);
            this.ParentForm.smartFileIO.WriteString("인천광역시 서구 장고개로 217번길 10", 2);
            this.ParentForm.smartFileIO.WriteString("", 3);
            this.ParentForm.smartFileIO.WriteString("sales@intechkorea.com", 4);
            this.ParentForm.smartFileIO.WriteString("http://www.intechkorea.com", 5);

            this.ParentForm.smartFileIO.WriteString("INTECHKOREA CO., LTD", 6);
            this.ParentForm.smartFileIO.WriteString("Tel.82-32-681-0714 / Fax.82-32-681-0713", 7);
            this.ParentForm.smartFileIO.WriteString("10, Janggogae-ro 217beon-gil, Seo-gu,", 8);
            this.ParentForm.smartFileIO.WriteString("Incheon, Republic of Korea", 9);
            this.ParentForm.smartFileIO.WriteString("sales@intechkorea.com", 10);
            this.ParentForm.smartFileIO.WriteString("http://www.intechkorea.com", 11);
        }
        private void DisplayAsInfo(DataStore.E_LanguageID language)
        {
            string readText = "";
            bool readCheck = false;
            int index = 0;

            if (this.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.Korean)
                index = 0;
            else
                index = 6;

            #region 회사별 AS정보 입력
            readText = this.ParentForm.smartFileIO.ReadString(index, ref readCheck);
            if (readCheck == true)
            {
                if (this.labelCompany.Text != readText)
                    this.labelCompany.Text = readText;
            }
            else
                this.labelCompany.Text = "";

            readText = this.ParentForm.smartFileIO.ReadString(index + 1, ref readCheck);
            if (readCheck == true)
            {
                if (this.labelTelNumber.Text != readText)
                    this.labelTelNumber.Text = readText;
            }
            else
                this.labelTelNumber.Text = "";

            readText = this.ParentForm.smartFileIO.ReadString(index + 2, ref readCheck);
            if (readCheck == true)
            {
                if (this.labelAddress1.Text != readText)
                    this.labelAddress1.Text = readText;
            }
            else
                this.labelAddress1.Text = "";


            readText = this.ParentForm.smartFileIO.ReadString(index + 3, ref readCheck);
            if (readCheck == true)
            {
                if (this.labelAddress2.Text != readText)
                    this.labelAddress2.Text = readText;
            }
            else
                this.labelAddress2.Text = "";

            readText = this.ParentForm.smartFileIO.ReadString(index + 4, ref readCheck);
            if (readCheck == true)
            {
                if (this.labelEmail.Text != readText)
                    this.labelEmail.Text = readText;
            }
            else
                this.labelEmail.Text = "";

            readText = this.ParentForm.smartFileIO.ReadString(index + 5, ref readCheck);
            if (readCheck == true)
            {
                if (this.labelWebsite.Text != readText)
                    this.labelWebsite.Text = readText;
            }
            else
                this.labelWebsite.Text = "";
            #endregion
        }

        public void UpdateProgreamVersionDisplay(DataStore.EquipmentStatus status, string version)
        {
            this.labelMainControlVer.Text = version.Trim();
        }
        public void UpdatePart11UserManagerVersionDisplay(string ver)
        {
            this.UI_Invoke(delegate
            {
                this.labelUserMgerVer.Text = ver;
                this.labelTitleDllUserMgr.Visible = true;
                this.labelTitleDllUserMgrVer.Visible = true;
                this.labelUserMgerVer.Visible = true;
            });
        }
        public void UpdatePart11AesEncryptionVersionDisplay(string ver)
        {
            this.UI_Invoke(delegate
            {
                this.labelAes128Ver.Text = ver;
                this.labelTitleDllAes128.Visible = true;
                this.labelTitleDllAes128Ver.Visible = true;
                this.labelAes128Ver.Visible = true;
            });
        }
        public void UpdateSerialportVersionDisplay(string ver)
        {
            this.UI_Invoke(delegate
            {
                this.labelSerialPortVer.Text = ver;
                this.labelTitleDllSerialPort.Visible = true;
                this.labelTitleDllSerialPortVer.Visible = true;
                this.labelSerialPortVer.Visible = true;
            });
        }

        public void DisplayRefresh()
        {
            if (this.ParentForm.SystemConfig.IsLogin == true)
                UserManager.UserManager_GetVersion();
            if (this.ParentForm.SystemConfig.IsPart11 == true)
                Encryption.AesEncryption_GetVersion();

            this.ParentForm.SystemConfig.CurrentForm = DataStore.FormStore.FormInformation;
            this.ParentForm.TransferDataStream(CommunicationCommand.Read, CommunicationID.MainBoard, CommunicationAddress._1502_ProgramVersion, "");
        }
        #endregion

        #region Override Member
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

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

        #region Event Handler
        private void labelSerialNo_Click(object sender, EventArgs e)
        {
            if (ParentForm.SystemConfig.CurrentUser.Group == DataStore.UserGroup.Developer)
            {
                DialogFormKeyboard keyboard = new DialogFormKeyboard(this.labelSerialNo.Text);
                if (keyboard.ShowDialog() == DialogResult.OK)
                {
                    this.labelSerialNo.Text = keyboard.RetStringValue;

                    this.ParentForm.SystemConfig.SerialNumber = this.labelSerialNo.Text;
                    this.ParentForm.SaveSystemConfigurationFile(this.ParentForm.SystemConfig);
                }
            }
        }

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