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.Windows.Forms;

using SmartX;
using INT99DC_ImageDll;
using INT99DC_7C.DialogForms;
using INT99DC_7C.Part11_UserManager;
using INT99DC_7C.Part11_Encryption;

namespace INT99DC_7C.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";

                this.buttonScreen.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engScreenDisable));
                this.buttonScreen.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engScreenDown));
                this.buttonScreen.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engScreenUp));
                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));
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Chinese)
            {
                this.labelTitle.Text = "信息";

                this.buttonScreen.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnScreenDisable));
                this.buttonScreen.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnScreenDown));
                this.buttonScreen.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnScreenUp));
                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));
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Czech)
            {
                this.labelTitle.Text = "Informace o systému";

                this.buttonScreen.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.czeScreenDisable));
                this.buttonScreen.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.czeScreenDown));
                this.buttonScreen.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.czeScreenUp));
                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));
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.German)
            {
                this.labelTitle.Text = "Systeminformation";

                this.buttonScreen.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.gerScreenDisable));
                this.buttonScreen.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.gerScreenDown));
                this.buttonScreen.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.gerScreenUp));
                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));
            }
            else
            {

            }
        }
        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.labelDisplayVer.Text = "4.0.0";
            this.labelUserMgerVer.Text = "";
            this.labelAes128Ver.Text = "";
            this.labelSerialNo.Text = this.ParentForm.SystemConfig.SerialNumber;
            this.textBoxSerialNo.Location = new Point(395, 225);
            this.textBoxSerialNo.Visible = false;
            this.tabControlSpecialFunction.Location = new Point(102, 314);
            this.tabControlSpecialFunction.Visible = false;

            this.CollectionControlVerLabel = new Collection<SmartLabel>();
            this.CollectionControlVerLabel.Clear();

            switch (this.ParentForm.SystemConfig.EquipmentColumns)
            {
                case 7:
                    this.CollectionControlVerLabel.Add(this.labelMainControlVer);
                    this.CollectionControlVerLabel.Add(this.labelSubBoardVersion1);
                    this.CollectionControlVerLabel.Add(this.labelSubBoardVersion2);
                    this.CollectionControlVerLabel.Add(this.labelSubBoardVersion3);
                    this.CollectionControlVerLabel.Add(this.labelSubBoardVersion4);
                    this.CollectionControlVerLabel.Add(this.labelSubBoardVersion5);
                    this.CollectionControlVerLabel.Add(this.labelSubBoardVersion6);
                    this.CollectionControlVerLabel.Add(this.labelSubBoardVersion7);

                    this.labelSub8.Visible = this.labelSubBoardVersion8.Visible = false;
                    this.labelSub9.Visible = this.labelSubBoardVersion9.Visible = false;
                    this.labelSub10.Visible = this.labelSubBoardVersion10.Visible = false;
                    this.labelSub11.Visible = this.labelSubBoardVersion11.Visible = false;
                    this.labelSub12.Visible = this.labelSubBoardVersion12.Visible = false;
                    this.labelSub13.Visible = this.labelSubBoardVersion13.Visible = false;
                    this.labelSub14.Visible = this.labelSubBoardVersion14.Visible = false;
                    this.labelSub15.Visible = this.labelSubBoardVersion15.Visible = false;
                    break;
                case 8:
                    this.CollectionControlVerLabel.Add(this.labelMainControlVer);
                    this.CollectionControlVerLabel.Add(this.labelSubBoardVersion1);
                    this.CollectionControlVerLabel.Add(this.labelSubBoardVersion2);
                    this.CollectionControlVerLabel.Add(this.labelSubBoardVersion3);
                    this.CollectionControlVerLabel.Add(this.labelSubBoardVersion4);
                    this.CollectionControlVerLabel.Add(this.labelSubBoardVersion5);
                    this.CollectionControlVerLabel.Add(this.labelSubBoardVersion6);
                    this.CollectionControlVerLabel.Add(this.labelSubBoardVersion7);
                    this.CollectionControlVerLabel.Add(this.labelSubBoardVersion8);

                    this.labelSub9.Visible = this.labelSubBoardVersion9.Visible = false;
                    this.labelSub10.Visible = this.labelSubBoardVersion10.Visible = false;
                    this.labelSub11.Visible = this.labelSubBoardVersion11.Visible = false;
                    this.labelSub12.Visible = this.labelSubBoardVersion12.Visible = false;
                    this.labelSub13.Visible = this.labelSubBoardVersion13.Visible = false;
                    this.labelSub14.Visible = this.labelSubBoardVersion14.Visible = false;
                    this.labelSub15.Visible = this.labelSubBoardVersion15.Visible = false;
                    break;
                case 10:
                    this.CollectionControlVerLabel.Add(this.labelMainControlVer);
                    this.CollectionControlVerLabel.Add(this.labelSubBoardVersion1);
                    this.CollectionControlVerLabel.Add(this.labelSubBoardVersion2);
                    this.CollectionControlVerLabel.Add(this.labelSubBoardVersion3);
                    this.CollectionControlVerLabel.Add(this.labelSubBoardVersion4);
                    this.CollectionControlVerLabel.Add(this.labelSubBoardVersion5);
                    this.CollectionControlVerLabel.Add(this.labelSubBoardVersion6);
                    this.CollectionControlVerLabel.Add(this.labelSubBoardVersion7);
                    this.CollectionControlVerLabel.Add(this.labelSubBoardVersion8);
                    this.CollectionControlVerLabel.Add(this.labelSubBoardVersion9);
                    this.CollectionControlVerLabel.Add(this.labelSubBoardVersion10);

                    this.labelSub11.Visible = this.labelSubBoardVersion11.Visible = false;
                    this.labelSub12.Visible = this.labelSubBoardVersion12.Visible = false;
                    this.labelSub13.Visible = this.labelSubBoardVersion13.Visible = false;
                    this.labelSub14.Visible = this.labelSubBoardVersion14.Visible = false;
                    this.labelSub15.Visible = this.labelSubBoardVersion15.Visible = false;
                    break;
                case 12:
                    this.CollectionControlVerLabel.Add(this.labelMainControlVer);
                    this.CollectionControlVerLabel.Add(this.labelSubBoardVersion1);
                    this.CollectionControlVerLabel.Add(this.labelSubBoardVersion2);
                    this.CollectionControlVerLabel.Add(this.labelSubBoardVersion3);
                    this.CollectionControlVerLabel.Add(this.labelSubBoardVersion4);
                    this.CollectionControlVerLabel.Add(this.labelSubBoardVersion5);
                    this.CollectionControlVerLabel.Add(this.labelSubBoardVersion6);
                    this.CollectionControlVerLabel.Add(this.labelSubBoardVersion7);
                    this.CollectionControlVerLabel.Add(this.labelSubBoardVersion8);
                    this.CollectionControlVerLabel.Add(this.labelSubBoardVersion9);
                    this.CollectionControlVerLabel.Add(this.labelSubBoardVersion10);
                    this.CollectionControlVerLabel.Add(this.labelSubBoardVersion11);
                    this.CollectionControlVerLabel.Add(this.labelSubBoardVersion12);

                    this.labelSub13.Visible = this.labelSubBoardVersion13.Visible = false;
                    this.labelSub14.Visible = this.labelSubBoardVersion14.Visible = false;
                    this.labelSub15.Visible = this.labelSubBoardVersion15.Visible = false;
                    break;
                case 15:
                    this.CollectionControlVerLabel.Add(this.labelMainControlVer);
                    this.CollectionControlVerLabel.Add(this.labelSubBoardVersion1);
                    this.CollectionControlVerLabel.Add(this.labelSubBoardVersion2);
                    this.CollectionControlVerLabel.Add(this.labelSubBoardVersion3);
                    this.CollectionControlVerLabel.Add(this.labelSubBoardVersion4);
                    this.CollectionControlVerLabel.Add(this.labelSubBoardVersion5);
                    this.CollectionControlVerLabel.Add(this.labelSubBoardVersion6);
                    this.CollectionControlVerLabel.Add(this.labelSubBoardVersion7);
                    this.CollectionControlVerLabel.Add(this.labelSubBoardVersion8);
                    this.CollectionControlVerLabel.Add(this.labelSubBoardVersion9);
                    this.CollectionControlVerLabel.Add(this.labelSubBoardVersion10);
                    this.CollectionControlVerLabel.Add(this.labelSubBoardVersion11);
                    this.CollectionControlVerLabel.Add(this.labelSubBoardVersion12);
                    this.CollectionControlVerLabel.Add(this.labelSubBoardVersion13);
                    this.CollectionControlVerLabel.Add(this.labelSubBoardVersion14);
                    this.CollectionControlVerLabel.Add(this.labelSubBoardVersion15);
                    break;
                default:
                    break;
            }

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

            this.labelMainControlVer.Text = "-";
        }

        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.labelUserMgerVer.Text = ver;
            this.labelTitleDllUserMgr.Visible = true;
            this.labelTitleDllUserMgrVer.Visible = true;
            this.labelUserMgerVer.Visible = true;
        }
        public void UpdatePart11AesEncryptionVersionDisplay(string ver)
        {
            this.labelAes128Ver.Text = ver;
            this.labelTitleDllAes128.Visible = true;
            this.labelTitleDllAes128Ver.Visible = true;
            this.labelAes128Ver.Visible = true;
        }

        public void DisplayRefresh()
        {
            this.ParentForm.SystemConfig.CurrentForm = DataStore.FormStore.FormInformation;
            this.ParentForm.TransferDataStream(CommunicationCommand.Read, CommunicationID.MainBoard, CommunicationAddress.ProgramVersion, "");

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

            switch (this.ParentForm.SystemConfig.CurrentUser.Group)
            {
                case DataStore.UserGroup.Admin:
                case DataStore.UserGroup.Developer:
                    this.buttonScreen.Visible = true;
                    break;
                default:
                    this.buttonScreen.Visible = false;
                    break;
            }
        }
        #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.Left = 8;
                this.smartKeyboard1.Top = 280;
                this.smartKeyboard1.Width = 1000;
                this.smartKeyboard1.Height = 480;
                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.textBoxSerialNo.Visible = false;

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

                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 buttonSubBoardVersion_Click(object sender, EventArgs e)
        {
            this.ParentForm.TransferDataStream(CommunicationCommand.Read, CommunicationID.MainBoard, CommunicationAddress.ProgramVersion, "");
        }

        private void buttonBack_Click(object sender, EventArgs e)
        {
            this.tabControlSpecialFunction.Visible = false;

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

            if (this.tabControlSpecialFunction.Visible == false)
                this.tabControlSpecialFunction.Visible = true;
            else
                this.tabControlSpecialFunction.Visible = false;
        }
        #endregion
    }
}