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

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

        #region Constructor
        public ControlCenterSystemAutoZero(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.labelAutoZeroTime.Text = " -";
            //this.labelAutoZeroRange.Text = " -";

            this.comboBoxMode1Variate.SelectedIndexChanged -= new EventHandler(this.comboBoxMode1Variate_SelectedIndexChanged);
            this.comboBoxMode1Variate.Items.Clear();
            this.comboBoxMode1Variate.Items.Add(0.1);
            this.comboBoxMode1Variate.Items.Add(0.2);
            this.comboBoxMode1Variate.Items.Add(0.5);
            this.comboBoxMode1Variate.Items.Add(1.0);
            this.comboBoxMode1Variate.SelectedIndex = 0;
            this.comboBoxMode1Variate.SelectedIndexChanged += new EventHandler(this.comboBoxMode1Variate_SelectedIndexChanged);

            this.comboBoxMode2Variate.SelectedIndexChanged -= new EventHandler(this.comboBoxMode2Variate_SelectedIndexChanged);
            this.comboBoxMode2Variate.Items.Clear();
            this.comboBoxMode2Variate.Items.Add(0.1);
            this.comboBoxMode2Variate.Items.Add(0.2);
            this.comboBoxMode2Variate.Items.Add(0.5);
            this.comboBoxMode2Variate.Items.Add(1.0);
            this.comboBoxMode2Variate.SelectedIndex = 0;
            this.comboBoxMode2Variate.SelectedIndexChanged += new EventHandler(this.comboBoxMode2Variate_SelectedIndexChanged);
        }

        private void ControlEnableMode1(bool time, bool range, bool variate)
        {
            if (time == true)
            {
                if (this.labelMode1Time.ForeColor != Color.White)
                    this.labelMode1Time.ForeColor = Color.White;
            }
            else
            {
                if (this.labelMode1Time.ForeColor != Color.DarkGray)
                    this.labelMode1Time.ForeColor = Color.DarkGray;
            }

            if (range == true)
            {
                if (this.labelMode1Range.ForeColor != Color.White)
                    this.labelMode1Range.ForeColor = Color.White;
            }
            else
            {
                if (this.labelMode1Range.ForeColor != Color.DarkGray)
                    this.labelMode1Range.ForeColor = Color.DarkGray;
            }

            if (this.labelMode1Time.Enabled != time)
                this.labelMode1Time.Enabled = time;

            if (this.labelMode1Range.Enabled != range)
                this.labelMode1Range.Enabled = range;

            if (this.comboBoxMode1Variate.Enabled != variate)
                this.comboBoxMode1Variate.Enabled = variate;
        }
        private void ControlEnableMode2(bool time, bool range, bool variate)
        {
            if (time == true)
            {
                if (this.labelMode2Time.ForeColor != Color.White)
                    this.labelMode2Time.ForeColor = Color.White;
            }
            else
            {
                if (this.labelMode2Time.ForeColor != Color.DarkGray)
                    this.labelMode2Time.ForeColor = Color.DarkGray;
            }

            if (range == true)
            {
                if (this.labelMode2Range.ForeColor != Color.White)
                    this.labelMode2Range.ForeColor = Color.White;
            }
            else
            {
                if (this.labelMode2Range.ForeColor != Color.DarkGray)
                    this.labelMode2Range.ForeColor = Color.DarkGray;
            }

            if (this.labelMode2Time.Enabled != time)
                this.labelMode2Time.Enabled = time;

            if (this.labelMode2Range.Enabled != range)
                this.labelMode2Range.Enabled = range;

            if (this.comboBoxMode2Variate.Enabled != variate)
                this.comboBoxMode2Variate.Enabled = variate;
        }

        private void UpdateZeroParameterDisplay(SystemParameter4 item)
        {
            this.UpdateZero1ParameterDisplay(item);
            this.UpdateZero2ParameterDisplay(item);
        }
        public void UpdateZero1ParameterDisplay(SystemParameter4 item)
        {
            int iValue = 0;
            string value = "";

            // Mode 1
            iValue = int.Parse(item.AutoZero1Mode.Trim());
            if (iValue == 1)
            {
                this.radioButtonMode1Low.Checked = true;
                this.ControlEnableMode1(false, false, false);
            }
            else if (iValue == 2)
            {
                this.radioButtonMode1Middle.Checked = true;
                this.ControlEnableMode1(false, false, false);
            }
            else if (iValue == 3)
            {
                this.radioButtonMode1High.Checked = true;
                this.ControlEnableMode1(false, false, false);
            }
            else if (iValue == 4)
            {
                this.radioButtonMode1UserSetting.Checked = true;
                this.ControlEnableMode1(true, true, true);
            }
            else
            {
                this.radioButtonMode1UserSetting.Checked = true;
                this.ControlEnableMode1(true, true, true);
            }

            // Mode1 Time
            value = item.AutoZero1Time.Trim();
            if (this.labelMode1Time.Text != value)
                this.labelMode1Time.Text = value;

            // Mode1 Range
            value = item.AutoZero1Range.Trim();
            if (this.labelMode1Range.Text != value)
                this.labelMode1Range.Text = value;

            // Mode1 Variate
            iValue = int.Parse(item.AutoZero1Variate.Trim());
            this.comboBoxMode1Variate.SelectedIndexChanged -= new EventHandler(this.comboBoxMode1Variate_SelectedIndexChanged);
            this.comboBoxMode1Variate.SelectedIndex = iValue;
            this.comboBoxMode1Variate.SelectedIndexChanged += new EventHandler(this.comboBoxMode1Variate_SelectedIndexChanged);

        }
        public void UpdateZero2ParameterDisplay(SystemParameter4 item)
        {
            int iValue = 0;
            string value = "";

            // Mode 2
            iValue = int.Parse(item.AutoZero2Mode.Trim());
            if (iValue == 0)
            {
                this.radioButtonMode2None.Checked = true;
                this.ControlEnableMode2(false, false, false);
            }
            else if (iValue == 1)
            {
                this.radioButtonMode2Low.Checked = true;
                this.ControlEnableMode2(false, false, false);
            }
            else if (iValue == 2)
            {
                this.radioButtonMode2Middle.Checked = true;
                this.ControlEnableMode2(false, false, false);
            }
            else if (iValue == 3)
            {
                this.radioButtonMode2High.Checked = true;
                this.ControlEnableMode2(false, false, false);
            }
            else if (iValue == 4)
            {
                this.radioButtonMode2UserSetting.Checked = true;
                this.ControlEnableMode2(true, true, true);
            }
            else
            {
                this.radioButtonMode2None.Checked = true;
                this.ControlEnableMode2(true, true, true);
            }

            // Mode2 Time;
            value = item.AutoZero2Time.Trim();
            if (this.labelMode2Time.Text != value)
                this.labelMode2Time.Text = value;

            // Mode2 Range
            value = item.AutoZero2Range.Trim();
            if (this.labelMode2Range.Text != value)
                this.labelMode2Range.Text = value;

            // Mode2 Variate
            iValue = int.Parse(item.AutoZero2Variate.Trim());
            this.comboBoxMode2Variate.SelectedIndexChanged -= new EventHandler(this.comboBoxMode2Variate_SelectedIndexChanged);
            this.comboBoxMode2Variate.SelectedIndex = iValue;
            this.comboBoxMode2Variate.SelectedIndexChanged += new EventHandler(this.comboBoxMode2Variate_SelectedIndexChanged);
        }

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

            this.UpdateZeroParameterDisplay(this.ParentForm.ParentForm.CurrentSystemParameter4);
        }
        #endregion

        #region Event Handler
        private void radioButtonMode1_Click(object sender, EventArgs e)
        {
            string value = "";
            SmartRadioButton button = sender as SmartRadioButton;

            if (button == null)
                return;

            if (button == this.radioButtonMode1Low)
                value = "0001";
            else if (button == this.radioButtonMode1Middle)
                value = "0002";
            else if (button == this.radioButtonMode1High)
                value = "0003";
            else if (button == this.radioButtonMode1UserSetting)
                value = "0004";

            this.ParentForm.ParentForm.CurrentSystemParameter4.AutoZero1Mode = value;
            this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.Zero1Mode, value);
        }

        private void radioButtonMode2_Click(object sender, EventArgs e)
        {
            string value = "";
            SmartRadioButton button = sender as SmartRadioButton;

            if (button == null)
                return;

            if (button == this.radioButtonMode2None)
                value = "0000";
            else if (button == this.radioButtonMode2Low)
                value = "0001";
            else if (button == this.radioButtonMode2Middle)
                value = "0002";
            else if (button == this.radioButtonMode2High)
                value = "0003";
            else if (button == this.radioButtonMode2UserSetting)
                value = "0004";

            this.ParentForm.ParentForm.CurrentSystemParameter4.AutoZero2Mode = value;
            this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.Zero2Mode, value);
        }

        private void labelMode1Time_Click(object sender, EventArgs e)
        {
            string value = "";
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelMode1Time.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.labelMode1Time.Text = myKeyPad.StringValue;
                    this.ParentForm.ParentForm.CurrentSystemParameter4.AutoZero1Time = myKeyPad.StringValue;

                    value = Helper.StringZeroFillDigits4(this.ParentForm.ParentForm.CurrentSystemParameter4.AutoZero1Time);
                    this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.Zero1Time, value);
                }
            }
        }
        private void labelMode1Range_Click(object sender, EventArgs e)
        {
            string value = "";
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelMode1Range.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.labelMode1Range.Text = myKeyPad.StringValue;
                    this.ParentForm.ParentForm.CurrentSystemParameter4.AutoZero1Range = myKeyPad.StringValue;

                    value = Helper.StringZeroFillDigits4(this.ParentForm.ParentForm.CurrentSystemParameter4.AutoZero1Range);
                    this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.Zero1Range, value);
                }
            }
        }
        private void comboBoxMode1Variate_SelectedIndexChanged(object sender, EventArgs e)
        {
            string value = "";

            value = Helper.StringZeroFillDigits4(this.comboBoxMode1Variate.SelectedIndex.ToString());
            this.ParentForm.ParentForm.CurrentSystemParameter4.AutoZero1Variate = value;

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

        private void labelMode2Time_Click(object sender, EventArgs e)
        {
            string value = "";
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelMode2Time.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.labelMode2Time.Text = myKeyPad.StringValue;
                    this.ParentForm.ParentForm.CurrentSystemParameter4.AutoZero2Time = myKeyPad.StringValue;

                    value = Helper.StringZeroFillDigits4(this.ParentForm.ParentForm.CurrentSystemParameter4.AutoZero2Time);
                    this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.Zero2Time, value);
                }
            }
        }
        private void labelMode2Range_Click(object sender, EventArgs e)
        {
            string value = "";
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelMode2Range.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.labelMode2Range.Text = myKeyPad.StringValue;
                    this.ParentForm.ParentForm.CurrentSystemParameter4.AutoZero2Range = myKeyPad.StringValue;

                    value = Helper.StringZeroFillDigits4(this.ParentForm.ParentForm.CurrentSystemParameter4.AutoZero2Range);
                    this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.Zero2Range, value);
                }
            }
        }
        private void comboBoxMode2Variate_SelectedIndexChanged(object sender, EventArgs e)
        {
            string value = "";

            value = Helper.StringZeroFillDigits4(this.comboBoxMode2Variate.SelectedIndex.ToString());
            this.ParentForm.ParentForm.CurrentSystemParameter4.AutoZero2Variate = value;

            this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.Zero2Variate, value);
        }
        #endregion
    }
}
