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 INT51DB.Forms;
using INT51DB.DialogForms;
using INT51DB_ImageDll;

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

        #region Constructor
        public ControlCenterEquipFunctionSetting(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.SystemConfig.Language == DataStore.LanguageID.English)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.Chinese)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.Czech)
            {
            }
            else
            {
            }
        }
        private void DefaultSetting()
        {
            this.comboBoxLanguage.SelectedIndexChanged -= new EventHandler(this.comboBoxLanguage_SelectedIndexChanged);
            this.comboBoxLanguage.Items.Clear();
            this.comboBoxLanguage.Items.Add("한국어");
            this.comboBoxLanguage.Items.Add("English");
            this.comboBoxLanguage.SelectedIndexChanged += new EventHandler(this.comboBoxLanguage_SelectedIndexChanged);
        }

        private void UpdateParameterDisplay(SystemConfigurationItem item, SystemParameter1 parameter)
        {
            string value = "";
            int index = 0;

            // 언어
            index = (int)item.Language;
            if (this.comboBoxLanguage.SelectedIndex != index)
            {
                this.comboBoxLanguage.SelectedIndexChanged -= new EventHandler(this.comboBoxLanguage_SelectedIndexChanged);
                this.comboBoxLanguage.SelectedIndex = index;
                this.comboBoxLanguage.SelectedIndexChanged += new EventHandler(this.comboBoxLanguage_SelectedIndexChanged);
            }

            // 부저ON시간
            value = parameter.BuzzerOnTime.Trim();
            if (this.labelBuzzerOnTime.Text != value)
                this.labelBuzzerOnTime.Text = value;

            // 릴레이동작시간
            value = parameter.RelayOnTime.Trim();
            if (this.labelRelayOnTime.Text != value)
                this.labelRelayOnTime.Text = value;

            // 채터링감지
            value = parameter.Chattering.Trim();
            if (this.labelChattering.Text != value)
                this.labelChattering.Text = value;

            // 초기운전
            if (parameter.InitialDrive.Trim() == "0")
            {
                if (this.buttonInitialDrive.ButtonStatus != SmartX.SmartButton.BUTSTATUS.UP)
                    this.buttonInitialDrive.ButtonUp();
            }
            else
            {
                if (this.buttonInitialDrive.ButtonStatus != SmartX.SmartButton.BUTSTATUS.DOWN)
                    this.buttonInitialDrive.ButtonDown();
            }
        }
        private void UpdateParameterDisplay1(SystemStatus status)
        {
            if (this.labelPasswordLevel1.Text != status.CurrentUser.Level1Password)
                this.labelPasswordLevel1.Text = status.CurrentUser.Level1Password;

            if (this.labelPasswordLevel2.Text != status.CurrentUser.Level2Password)
                this.labelPasswordLevel2.Text = status.CurrentUser.Level2Password;

            if (this.labelPasswordLevel3.Text != status.CurrentUser.Level3Password)
                this.labelPasswordLevel3.Text = status.CurrentUser.Level3Password;
        }

        public void DisplayRefresh(DataStore.UserGroup group)
        {
            this.ParentForm.ParentForm.CurrentSystemStatus.CurrentDisplay = DataStore.DisplayStore.EquipFuctionSetting;
            this.ParentForm.ParentForm.SetDisplayMode(DataStore.DisplayMode.Menu);
            this.ParentForm.DisplayTitleRoot(this.ParentForm.ParentForm.CurrentSystemStatus);

            this.UpdateParameterDisplay(this.ParentForm.ParentForm.SystemConfig, this.ParentForm.ParentForm.CurrentSystemParameter1);
            this.UpdateParameterDisplay1(this.ParentForm.ParentForm.CurrentSystemStatus);

            if (this.ParentForm.ParentForm.IsCommunicationLogOpen == false)
                this.buttonCommunicationLog.ButtonUp();
            else
                this.buttonCommunicationLog.ButtonDown();

            //if (group == DataStore.UserGroup.Level4Developer)
            //{
            //    this.buttonCommunicationLog.Visible = true;
            //    this.smartLabel1.Visible = true;
            //}
            //else
            //{
            //    this.buttonCommunicationLog.Visible = false;
            //    this.smartLabel1.Visible = false;
            //}
        }
        #endregion

        #region Event Handler
        private void labelBuzzerOnTime_Click(object sender, EventArgs e)
        {
            string value = "";
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelBuzzerOnTime.Text, 4, 0, false, this.ParentForm.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue < 1 || myKeyPad.doubleValue > 9999)
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.Language);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelBuzzerOnTime.Text = myKeyPad.StringValue;

                    value = Helper.StringZeroFillDigits4(this.labelBuzzerOnTime.Text);
                    this.ParentForm.ParentForm.CurrentSystemParameter1.BuzzerOnTime = myKeyPad.StringValue;

                    this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.BuzzerOnTime, value);
                }
            }
        }

        private void labelRelayOnTime_Click(object sender, EventArgs e)
        {
            string value = "";
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelRelayOnTime.Text, 4, 0, false, this.ParentForm.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue < 1 || myKeyPad.doubleValue > 9999)
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.Language);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelRelayOnTime.Text = myKeyPad.StringValue;

                    value = Helper.StringZeroFillDigits4(this.labelRelayOnTime.Text);
                    this.ParentForm.ParentForm.CurrentSystemParameter1.RelayOnTime = myKeyPad.StringValue;

                    this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.RelayRunTime, value);
                }
            }
        }

        private void labelChattering_Click(object sender, EventArgs e)
        {
            string value = "";
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelChattering.Text, 4, 0, false, this.ParentForm.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue < 1 || myKeyPad.doubleValue > 500)
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.Language);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelChattering.Text = myKeyPad.StringValue;

                    value = Helper.StringZeroFillDigits4(this.labelChattering.Text);
                    this.ParentForm.ParentForm.CurrentSystemParameter1.Chattering = myKeyPad.StringValue;

                    this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.Chattering, value);
                }
            }
        }

        private void buttonInitialDrive_Click(object sender, EventArgs e)
        {
            string value = "";

            if (this.buttonInitialDrive.ButtonStatus == SmartX.SmartButton.BUTSTATUS.UP)
            {
                value = Helper.StringZeroFillDigits4("0");
                this.ParentForm.ParentForm.CurrentSystemParameter1.InitialDrive = "0";
            }
            else
            {
                value = Helper.StringZeroFillDigits4("1");
                this.ParentForm.ParentForm.CurrentSystemParameter1.InitialDrive = "1";
            }

            this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.IsInitialDrive, value);
        }

        private void comboBoxLanguage_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.comboBoxLanguage.SelectedIndex == 0)
                this.ParentForm.ParentForm.SystemConfig.Language = DataStore.LanguageID.Korean;
            else if (this.comboBoxLanguage.SelectedIndex == 1)
                this.ParentForm.ParentForm.SystemConfig.Language = DataStore.LanguageID.English;
            else if (this.comboBoxLanguage.SelectedIndex == 2)
                this.ParentForm.ParentForm.SystemConfig.Language = DataStore.LanguageID.Chinese;
            else if (this.comboBoxLanguage.SelectedIndex == 3)
                this.ParentForm.ParentForm.SystemConfig.Language = DataStore.LanguageID.Czech;
            else
                this.ParentForm.ParentForm.SystemConfig.Language = DataStore.LanguageID.Korean;

            this.ParentForm.ParentForm.SaveSystemConfigurationFile(this.ParentForm.ParentForm.SystemConfig);
            this.InitializeDesign();
        }

        private void labelPasswordLevel1_Click(object sender, EventArgs e)
        {
            DialogFormPasswordKeyPad1 myKeypad = new DialogFormPasswordKeyPad1(this.ParentForm.ParentForm.SystemConfig.Language);

            if (myKeypad.ShowDialog() == DialogResult.OK)
            {
                if (myKeypad.InputKey.Length != 4)
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.Language);
                    myMsg.ShowDialog();
                }
                else
                {
                    if (this.labelPasswordLevel1.Text != myKeypad.InputKey)
                    {
                        this.labelPasswordLevel1.Text = myKeypad.InputKey;

                        this.ParentForm.ParentForm.CurrentSystemStatus.CurrentUser.Level1Password = myKeypad.InputKey;
                        this.ParentForm.ParentForm.SaveUserGroupPasswordFile(this.ParentForm.ParentForm.CurrentSystemStatus.CurrentUser);
                    }
                }
            }
        }

        private void labelPasswordLevel2_Click(object sender, EventArgs e)
        {
            DialogFormPasswordKeyPad1 myKeypad = new DialogFormPasswordKeyPad1(this.ParentForm.ParentForm.SystemConfig.Language);

            if (myKeypad.ShowDialog() == DialogResult.OK)
            {
                if (myKeypad.InputKey.Length != 4)
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.Language);
                    myMsg.ShowDialog();
                }
                else
                {
                    if (this.labelPasswordLevel2.Text != myKeypad.InputKey)
                    {
                        this.labelPasswordLevel2.Text = myKeypad.InputKey;

                        this.ParentForm.ParentForm.CurrentSystemStatus.CurrentUser.Level2Password = myKeypad.InputKey;
                        this.ParentForm.ParentForm.SaveUserGroupPasswordFile(this.ParentForm.ParentForm.CurrentSystemStatus.CurrentUser);
                    }
                }
            }
        }

        private void labelPasswordLevel3_Click(object sender, EventArgs e)
        {
            DialogFormPasswordKeyPad1 myKeypad = new DialogFormPasswordKeyPad1(this.ParentForm.ParentForm.SystemConfig.Language);

            if (myKeypad.ShowDialog() == DialogResult.OK)
            {
                if (myKeypad.InputKey.Length != 4)
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.Language);
                    myMsg.ShowDialog();
                }
                else
                {
                    if (this.labelPasswordLevel3.Text != myKeypad.InputKey)
                    {
                        this.labelPasswordLevel3.Text = myKeypad.InputKey;

                        this.ParentForm.ParentForm.CurrentSystemStatus.CurrentUser.Level3Password = myKeypad.InputKey;
                        this.ParentForm.ParentForm.SaveUserGroupPasswordFile(this.ParentForm.ParentForm.CurrentSystemStatus.CurrentUser);
                    }
                }
            }
        }

        private void buttonCommunicationLog_Click(object sender, EventArgs e)
        {
            if (this.buttonCommunicationLog.ButtonStatus == SmartX.SmartButton.BUTSTATUS.UP)
                this.ParentForm.ParentForm.IsCommunicationLogOpen = false;
            else
                this.ParentForm.ParentForm.IsCommunicationLogOpen = true;
        }
        #endregion
    }
}
