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

namespace ITC81DB.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.SystemConfig1.Language == DataStore.LanguageID.English)
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.engBackgroundAutoZero));

                this.radioButtonMode2None.Text = "None";
                this.radioButtonMode1High.Text = this.radioButtonMode2High.Text = "High";
                this.radioButtonMode1Middle.Text = this.radioButtonMode2Middle.Text = "Middle";
                this.radioButtonMode1Low.Text = this.radioButtonMode2Low.Text = "Low";
                this.radioButtonMode1UserSetting.Font = this.radioButtonMode2UserSetting.Font = new Font("새굴림", 13, FontStyle.Regular);
                this.radioButtonMode1UserSetting.Text = this.radioButtonMode2UserSetting.Text = "User Setting";
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Chinese)
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.chnBackgroundAutoZero));

                this.radioButtonMode2None.Text = "不使用";
                this.radioButtonMode1High.Text = this.radioButtonMode2High.Text = "高";
                this.radioButtonMode1Middle.Text = this.radioButtonMode2Middle.Text = "中间";
                this.radioButtonMode1Low.Text = this.radioButtonMode2Low.Text = "低";
                this.radioButtonMode1UserSetting.Font = this.radioButtonMode2UserSetting.Font = new Font("새굴림", 13, FontStyle.Regular);
                this.radioButtonMode1UserSetting.Text = this.radioButtonMode2UserSetting.Text = "自定义设置";
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Czech)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Russian)
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.rusBackgroundAutoZero));

                this.radioButtonMode2None.Text = "Нет";
                this.radioButtonMode1High.Text = this.radioButtonMode2High.Text = "Высокий";
                this.radioButtonMode1Middle.Text = this.radioButtonMode2Middle.Text = "Средний";
                this.radioButtonMode1Low.Text = this.radioButtonMode2Low.Text = "Низкий";
                this.radioButtonMode1UserSetting.Font = this.radioButtonMode2UserSetting.Font = new Font("새굴림", 10, FontStyle.Regular);
                this.radioButtonMode1UserSetting.Text = this.radioButtonMode2UserSetting.Text = "Настройки пользователя";
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.German)
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.gerBackgroundAutoZero));

                this.radioButtonMode2None.Text = "Keiner";
                this.radioButtonMode1High.Text = this.radioButtonMode2High.Text = "Hoch";
                this.radioButtonMode1Middle.Text = this.radioButtonMode2Middle.Text = "Mitte";
                this.radioButtonMode1Low.Text = this.radioButtonMode2Low.Text = "Niedrig";
                this.radioButtonMode1UserSetting.Font = this.radioButtonMode2UserSetting.Font = new Font("새굴림", 13, FontStyle.Regular);
                this.radioButtonMode1UserSetting.Text = this.radioButtonMode2UserSetting.Text = "Benutzereinstellungen";
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Spanish)
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.engBackgroundAutoZero));

                this.radioButtonMode2None.Text = "Ninguna";
                this.radioButtonMode1High.Text = this.radioButtonMode2High.Text = "Alto";
                this.radioButtonMode1Middle.Text = this.radioButtonMode2Middle.Text = "Medio";
                this.radioButtonMode1Low.Text = this.radioButtonMode2Low.Text = "Bajo";
                this.radioButtonMode1UserSetting.Font = this.radioButtonMode2UserSetting.Font = new Font("새굴림", 13, FontStyle.Regular);
                this.radioButtonMode1UserSetting.Text = this.radioButtonMode2UserSetting.Text = "Ajuste del usuario";
            }
            else
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.korBackgroundAutoZero));

                this.radioButtonMode2None.Text = "None";
                this.radioButtonMode1High.Text = this.radioButtonMode2High.Text = "High";
                this.radioButtonMode1Middle.Text = this.radioButtonMode2Middle.Text = "Middle";
                this.radioButtonMode1Low.Text = this.radioButtonMode2Low.Text = "Low";
                this.radioButtonMode1UserSetting.Font = this.radioButtonMode2UserSetting.Font = new Font("새굴림", 13, FontStyle.Regular);
                this.radioButtonMode1UserSetting.Text = this.radioButtonMode2UserSetting.Text = "User Setting";
            }
        }
        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.MainAutoZero1Mode.Trim());
            if (iValue == 4)
            {
                this.ControlEnableMode1(true, true, true);
                this.radioButtonMode1UserSetting.Checked = true;
            }
            else
            {
                this.ControlEnableMode1(false, false, false);

                if (iValue == 1)
                    this.radioButtonMode1Low.Checked = true;
                else if (iValue == 2)
                    this.radioButtonMode1Middle.Checked = true;
                else if (iValue == 3)
                    this.radioButtonMode1High.Checked = true;
            }

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

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

            // Mode1 Variate
            iValue = int.Parse(item.MainAutoZero1Variate.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.MainAutoZero2Mode.Trim());
            if (iValue == 4)
            {
                this.ControlEnableMode2(true, true, true);
                this.radioButtonMode2UserSetting.Checked = true;
            }
            else
            {
                this.ControlEnableMode2(false, false, false);

                if (iValue == 1)
                    this.radioButtonMode2Low.Checked = true;
                else if (iValue == 2)
                    this.radioButtonMode2Middle.Checked = true;
                else if (iValue == 3)
                    this.radioButtonMode2High.Checked = true;
                else
                    this.radioButtonMode2None.Checked = true;
            }

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

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

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

        public void DisplayRefresh(SystemStatus status)
        {
            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);
            this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Read, CommunicationID.MainBoard, CommunicationAddress.ParameterRead4901, "");
            this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Read, CommunicationID.MainBoard, CommunicationAddress.ParameterRead4902, "");
        }
        #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.LCDAutoZero1Mode = value;
            this.ParentForm.ParentForm.SaveSystemParameter4File(this.ParentForm.ParentForm.CurrentSystemParameter4);
            this.ParentForm.ParentForm.TransferParameter4();
            this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Read, CommunicationID.MainBoard, CommunicationAddress.ParameterRead4901, "");
        }
        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.LCDAutoZero2Mode = value;
            this.ParentForm.ParentForm.SaveSystemParameter4File(this.ParentForm.ParentForm.CurrentSystemParameter4);
            this.ParentForm.ParentForm.TransferParameter4();
            this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Read, CommunicationID.MainBoard, CommunicationAddress.ParameterRead4902, "");
        }

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

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

                    value = Helper.StringZeroFillDigits4(this.ParentForm.ParentForm.CurrentSystemParameter4.LCDAutoZero1Time);
                    this.ParentForm.ParentForm.SaveSystemParameter4File(this.ParentForm.ParentForm.CurrentSystemParameter4);
                    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.SystemConfig1.Language);

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

                    value = Helper.StringZeroFillDigits4(this.ParentForm.ParentForm.CurrentSystemParameter4.LCDAutoZero1Range);
                    this.ParentForm.ParentForm.SaveSystemParameter4File(this.ParentForm.ParentForm.CurrentSystemParameter4);
                    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.LCDAutoZero1Variate = value;

            this.ParentForm.ParentForm.SaveSystemParameter4File(this.ParentForm.ParentForm.CurrentSystemParameter4);
            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.SystemConfig1.Language);

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

                    value = Helper.StringZeroFillDigits4(this.ParentForm.ParentForm.CurrentSystemParameter4.LCDAutoZero2Time);
                    this.ParentForm.ParentForm.SaveSystemParameter4File(this.ParentForm.ParentForm.CurrentSystemParameter4);
                    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.SystemConfig1.Language);

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

                    value = Helper.StringZeroFillDigits4(this.ParentForm.ParentForm.CurrentSystemParameter4.LCDAutoZero2Range);
                    this.ParentForm.ParentForm.SaveSystemParameter4File(this.ParentForm.ParentForm.CurrentSystemParameter4);
                    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.LCDAutoZero2Variate = value;

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