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 ITC81DB.DialogForms;
using ITC81DB.Forms;
using ITC81DB_ImageDll;

namespace ITC81DB.Controls
{
    public partial class ControlCenterInforSystem : UserControl
    {
        #region Field
        private FormMenu m_ParentForm;
        #endregion

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

            this.ParentForm = parent;

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

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

        #region Method
        public void InitializeDesign()
        {
            Class1 images = new Class1();

            if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.English)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Chinese)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Czech)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Russian)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.German)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Spanish)
            {
            }
            else
            {
            }
        }
        private void DefaultSetting()
        {
            this.labelSerialNo.Text = this.ParentForm.ParentForm.SystemConfig1.SerialNumber;
            this.labelDisplayVer.Text = FormMain.DisplayVersion;
        }

        public void UpdateMainBoardVersionDisplay(string version)
        {
            StringBuilder sb;

            if (version.Length < 4)
                return;

            sb = new StringBuilder(version.Trim());
            sb.Insert(sb.Length - 1, ".");
            sb.Insert(sb.Length - 3, ".");

            this.labelControlVer.Text = sb.ToString();
        }

        public void DisplayHiddenMenu(bool bValue)
        {
            this.buttonNext.Visible = bValue;
        }
        private void UpdateDisplayUser(UserItem user)
        {
            if ((user.Group == DataStore.UserGroup.Level3Manager || user.Group == DataStore.UserGroup.Level4Developer)
                && this.ParentForm.ParentForm.SystemConfig1.IsLogin == true)
                this.DisplayHiddenMenu(true);
            else
                this.DisplayHiddenMenu(false);
        }
        public void DisplayRefresh(SystemStatus status)
        {
            this.ParentForm.ParentForm.CurrentSystemStatus.CurrentDisplay = DataStore.DisplayStore.InforSystem;
            this.ParentForm.ParentForm.SetDisplayMode(DataStore.DisplayMode.Menu);
            this.ParentForm.DisplayTitleRoot(this.ParentForm.ParentForm.CurrentSystemStatus);

            if (this.textBoxSerialNo.Visible == true)
                this.textBoxSerialNo.Visible = false;

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

            this.UpdateDisplayUser(status.CurrentUser);

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

        #region Event Handler
        private void labelSerialNo_Click(object sender, EventArgs e)
        {
            DialogFormPasswordKeyPad password = new DialogFormPasswordKeyPad(4, this.ParentForm.ParentForm.SystemConfig1.Language, this.ParentForm.ParentForm.CurrentSystemStatus.CurrentUserPasswordType);
            DialogResult dialogResult = password.ShowDialog();
            if (dialogResult == DialogResult.OK)
            {
                if (password.Group == DataStore.UserGroup.Level4Developer)
                {
                    this.textBoxSerialNo.Visible = true;
                    this.smartKeyboard.KeyboardType = SmartX.SmartKeyboard.KEYBOARDTYPES.NORMAL;
                    this.smartKeyboard.Left = 15;
                    this.smartKeyboard.Top = 118;
                    this.smartKeyboard.Width = 680;
                    this.smartKeyboard.Height = 268;
                    this.smartKeyboard.Hide();

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

                    this.smartKeyboard.Show();
                }
            }
        }

        private void smartKeyboard1_OnXKeyClick(object sender, EventArgs e)
        {
            this.smartKeyboard.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.smartKeyboard.Hide();

                this.textBoxSerialNo.Visible = false;

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

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

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

                this.textBoxSerialNo.Visible = false;
            }
        }

        private void buttonNext_Click(object sender, EventArgs e)
        {
            this.ParentForm.CenterInforSystem2.DisplayRefresh(this.ParentForm.ParentForm.CurrentSystemStatus);
            this.ParentForm.CenterInforSystem2.BringToFront();
        }
        #endregion
    }
}
