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

using SmartX;
using INT69DB_2A_ImageDll;
using INT69DB_2A.DialogForms;
using INT69DB_2A.Part11_Encryption;
using INT69DB_2A.Part11_UserManager;
using INT69DB_2A.SerialManger;

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

        private Collection<SmartLabel> CollectionControlVerLabel;
        #endregion

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

            this.ParentForm = parent;

            this.InitializeDesign();
            this.InitializeAsInfomation();
            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();

            if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Korean)
            {

            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.English)
            {
                this.labelTitle.Text = "Information";
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Chinese)
            {
                this.labelTitle.Text = "信息";
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Japanese)
            {
                this.labelTitle.Text = "インフォメㅡション";
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Czech)
            {

            }
            else
            {

            }
        }
        private void InitializeAsInfomation()
        {
            bool fileCheck = false;
            string filePath = "";

            filePath = this.ParentForm.PathSystemFileFolder1 + "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(132, 107);
                this.labelWebsite.Location = new Point(132, 133);
            }
        }
        private void DefaultSetting()
        {
            this.labelDisplayVer.Text = "6.7.0";
            this.labelUserMgerVer.Text = "";
            this.labelAes128Ver.Text = "";
            this.labelSerialNo.Text = this.ParentForm.SystemConfig.SerialNumber;

            this.textBoxSerialNo.Location = new Point(260, 142);
            this.textBoxSerialNo.Visible = false;

            this.CollectionControlVerLabel = new Collection<SmartLabel>();
            this.CollectionControlVerLabel.Clear();
            
            switch (this.ParentForm.SystemConfig.EquipmentColumns)
            {
                case 2:
                    this.CollectionControlVerLabel.Add(this.labelMainControlVer);
                    this.CollectionControlVerLabel.Add(this.labelSubControlVer1);
                    this.CollectionControlVerLabel.Add(this.labelSubControlVer2);

                    this.labelSub3.Visible = this.labelSubControlVer3.Visible = false;
                    this.labelSub4.Visible = this.labelSubControlVer4.Visible = false;
                    this.labelSub5.Visible = this.labelSubControlVer5.Visible = false;
                    this.labelSub6.Visible = this.labelSubControlVer6.Visible = false;
                    this.labelSub7.Visible = this.labelSubControlVer7.Visible = false;
                    this.labelSub8.Visible = this.labelSubControlVer8.Visible = false;
                    this.labelSub9.Visible = this.labelSubControlVer9.Visible = false;
                    this.labelSub10.Visible = this.labelSubControlVer10.Visible = false;
                    break;
                case 3:
                    this.CollectionControlVerLabel.Add(this.labelMainControlVer);
                    this.CollectionControlVerLabel.Add(this.labelSubControlVer1);
                    this.CollectionControlVerLabel.Add(this.labelSubControlVer2);
                    this.CollectionControlVerLabel.Add(this.labelSubControlVer3);

                    this.labelSub4.Visible = this.labelSubControlVer4.Visible = false;
                    this.labelSub5.Visible = this.labelSubControlVer5.Visible = false;
                    this.labelSub6.Visible = this.labelSubControlVer6.Visible = false;
                    this.labelSub7.Visible = this.labelSubControlVer7.Visible = false;
                    this.labelSub8.Visible = this.labelSubControlVer8.Visible = false;
                    this.labelSub9.Visible = this.labelSubControlVer9.Visible = false;
                    this.labelSub10.Visible = this.labelSubControlVer10.Visible = false;
                    break;
                case 4:
                    this.CollectionControlVerLabel.Add(this.labelMainControlVer);
                    this.CollectionControlVerLabel.Add(this.labelSubControlVer1);
                    this.CollectionControlVerLabel.Add(this.labelSubControlVer2);
                    this.CollectionControlVerLabel.Add(this.labelSubControlVer3);
                    this.CollectionControlVerLabel.Add(this.labelSubControlVer4);

                    this.labelSub5.Visible = this.labelSubControlVer5.Visible = false;
                    this.labelSub6.Visible = this.labelSubControlVer6.Visible = false;
                    this.labelSub7.Visible = this.labelSubControlVer7.Visible = false;
                    this.labelSub8.Visible = this.labelSubControlVer8.Visible = false;
                    this.labelSub9.Visible = this.labelSubControlVer9.Visible = false;
                    this.labelSub10.Visible = this.labelSubControlVer10.Visible = false;
                    break;
                case 5:
                    this.CollectionControlVerLabel.Add(this.labelMainControlVer);
                    this.CollectionControlVerLabel.Add(this.labelSubControlVer1);
                    this.CollectionControlVerLabel.Add(this.labelSubControlVer2);
                    this.CollectionControlVerLabel.Add(this.labelSubControlVer3);
                    this.CollectionControlVerLabel.Add(this.labelSubControlVer4);
                    this.CollectionControlVerLabel.Add(this.labelSubControlVer5);

                    this.labelSub6.Visible = this.labelSubControlVer6.Visible = false;
                    this.labelSub7.Visible = this.labelSubControlVer7.Visible = false;
                    this.labelSub8.Visible = this.labelSubControlVer8.Visible = false;
                    this.labelSub9.Visible = this.labelSubControlVer9.Visible = false;
                    this.labelSub10.Visible = this.labelSubControlVer10.Visible = false;
                    break;
                case 6:
                    this.CollectionControlVerLabel.Add(this.labelMainControlVer);
                    this.CollectionControlVerLabel.Add(this.labelSubControlVer1);
                    this.CollectionControlVerLabel.Add(this.labelSubControlVer2);
                    this.CollectionControlVerLabel.Add(this.labelSubControlVer3);
                    this.CollectionControlVerLabel.Add(this.labelSubControlVer4);
                    this.CollectionControlVerLabel.Add(this.labelSubControlVer5);
                    this.CollectionControlVerLabel.Add(this.labelSubControlVer6);

                    this.labelSub7.Visible = this.labelSubControlVer7.Visible = false;
                    this.labelSub8.Visible = this.labelSubControlVer8.Visible = false;
                    this.labelSub9.Visible = this.labelSubControlVer9.Visible = false;
                    this.labelSub10.Visible = this.labelSubControlVer10.Visible = false;
                    break;
                case 7:
                    this.CollectionControlVerLabel.Add(this.labelMainControlVer);
                    this.CollectionControlVerLabel.Add(this.labelSubControlVer1);
                    this.CollectionControlVerLabel.Add(this.labelSubControlVer2);
                    this.CollectionControlVerLabel.Add(this.labelSubControlVer3);
                    this.CollectionControlVerLabel.Add(this.labelSubControlVer4);
                    this.CollectionControlVerLabel.Add(this.labelSubControlVer5);
                    this.CollectionControlVerLabel.Add(this.labelSubControlVer6);
                    this.CollectionControlVerLabel.Add(this.labelSubControlVer7);

                    this.labelSub8.Visible = this.labelSubControlVer8.Visible = false;
                    this.labelSub9.Visible = this.labelSubControlVer9.Visible = false;
                    this.labelSub10.Visible = this.labelSubControlVer10.Visible = false;
                    break;
                case 8:
                    this.CollectionControlVerLabel.Add(this.labelMainControlVer);
                    this.CollectionControlVerLabel.Add(this.labelSubControlVer1);
                    this.CollectionControlVerLabel.Add(this.labelSubControlVer2);
                    this.CollectionControlVerLabel.Add(this.labelSubControlVer3);
                    this.CollectionControlVerLabel.Add(this.labelSubControlVer4);
                    this.CollectionControlVerLabel.Add(this.labelSubControlVer5);
                    this.CollectionControlVerLabel.Add(this.labelSubControlVer6);
                    this.CollectionControlVerLabel.Add(this.labelSubControlVer7);
                    this.CollectionControlVerLabel.Add(this.labelSubControlVer8);

                    this.labelSub9.Visible = this.labelSubControlVer9.Visible = false;
                    this.labelSub10.Visible = this.labelSubControlVer10.Visible = false;
                    break;
                case 10:
                    this.CollectionControlVerLabel.Add(this.labelMainControlVer);
                    this.CollectionControlVerLabel.Add(this.labelSubControlVer1);
                    this.CollectionControlVerLabel.Add(this.labelSubControlVer2);
                    this.CollectionControlVerLabel.Add(this.labelSubControlVer3);
                    this.CollectionControlVerLabel.Add(this.labelSubControlVer4);
                    this.CollectionControlVerLabel.Add(this.labelSubControlVer5);
                    this.CollectionControlVerLabel.Add(this.labelSubControlVer6);
                    this.CollectionControlVerLabel.Add(this.labelSubControlVer7);
                    this.CollectionControlVerLabel.Add(this.labelSubControlVer8);
                    this.CollectionControlVerLabel.Add(this.labelSubControlVer9);
                    this.CollectionControlVerLabel.Add(this.labelSubControlVer10);
                    break;
                default:
                    break;
            }

            foreach (SmartLabel label in this.CollectionControlVerLabel)
                label.Text = "-";

            this.labelMainControlVer.Text = "-";
        }
        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.LanguageID language)
        {
            string readText = "";
            bool readCheck = false;
            int index = 0;

            if (this.ParentForm.SystemConfig.Language == DataStore.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, Collection<string> values)
        {
            StringBuilder sb;

            if (values == null || values.Count == 0 || this.CollectionControlVerLabel.Count>values.Count)
                return;

            for (int i = 0; i < this.CollectionControlVerLabel.Count; i++)
            {
                sb = new StringBuilder(values[i].Trim());
                sb.Insert(sb.Length - 1, ".");
                sb.Insert(sb.Length - 3, ".");
                this.CollectionControlVerLabel[i].Text = sb.ToString();
            }
        }
        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()
        {
            this.ParentForm.SystemConfig.CurrentForm = DataStore.FormStore.FormInformation;
            this.ParentForm.TransferDataStream(CommunicationCommand.Read, CommunicationID.MainBoard, CommunicationAddress._1500_ProgramVersion, "");

            if (this.ParentForm.FlagDllSerial == true)
                SerialMgrComm.serialPort_GetVersion();

            if (this.ParentForm.SystemConfig.IsLogin == true)
                UserManager.UserManager_GetVersion();
            if (this.ParentForm.SystemConfig.IsPart11 == true)
                Encryption.AesEncryption_GetVersion();
        }
        #endregion

        #region Event Handler
        private void labelSerialNo_Click(object sender, EventArgs e)
        {
            if (this.ParentForm.SystemConfig.CurrentUser.Group == DataStore.UserGroup.Developer)
            {
                this.textBoxSerialNo.Visible = true;
                this.textBoxSerialNo.BringToFront();

                this.smartKeyboard1.KeyboardType = SmartX.SmartKeyboard.KEYBOARDTYPES.NORMAL;
                this.smartKeyboard1.Location = new Point(0, 174);
                this.smartKeyboard1.Size = new Size(800, 426);
                this.smartKeyboard1.Hide();

                this.smartKeyboard1.TargetInputObject = this.textBoxSerialNo;
                this.textBoxSerialNo.Text = this.labelSerialNo.Text;

                this.smartKeyboard1.Show();
            }
        }

        private void smartKeyboard1_OnXKeyClick(object sender, EventArgs e)
        {
            this.smartKeyboard1.Hide();
            this.textBoxSerialNo.Text = this.labelSerialNo.Text;

            this.textBoxSerialNo.Visible = false;
        }

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

                this.smartKeyboard1.Hide();

                this.labelSerialNo.Text = this.textBoxSerialNo.Text.Trim();
                this.textBoxSerialNo.Visible = false;

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

                this.smartKeyboard1.Hide();
                this.textBoxSerialNo.Text = this.labelSerialNo.Text;

                this.textBoxSerialNo.Visible = false;
            }
        }

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