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_2H.DialogForms;
using ITC81DB_2H.Forms;
using SmartX;
using ITC81DB_2H_ImageDll;
using ITC81DB_2H.Datastore;

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

        private List<string> ListVariate;
        #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 == Define.E_LanguageID.English)
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.engBackgroundAutoZero));
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Chinese)
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.chnBackgroundAutoZero));
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Czech)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Russian)
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.rusBackgroundAutoZero));
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.German)
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.gerBackgroundAutoZero));
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Spanish)
            {

            }
            else
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.korBackgroundAutoZero));
            }
        }
        private void DefaultSetting()
        {
            this.ListVariate = new List<string>();
            this.ListVariate.Clear();
            this.ListVariate.Add("1");
            this.ListVariate.Add("2");
            this.ListVariate.Add("5");
            this.ListVariate.Add("10");

            this.DetermineRange();
        }

        public void DetermineRange()
        {
            this.labelPlusRange.Text = Helper.StringToDecimalPlaces(this.ParentForm.ParentForm.CurrentSystemParameter4.LCDAutoZeroPlusRange, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
            this.labelMinusRange.Text = Helper.StringToDecimalPlaces(this.ParentForm.ParentForm.CurrentSystemParameter4.LCDAutoZeroMinusRange, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);

            this.comboBoxVariate.SelectedIndexChanged -= new EventHandler(this.comboBoxVariate_SelectedIndexChanged);
            this.comboBoxVariate.Items.Clear();
            for (int i = 0; i < this.ListVariate.Count; i++)
                this.comboBoxVariate.Items.Add(Helper.StringToDecimalPlaces(this.ListVariate[i], this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces));
            this.comboBoxVariate.SelectedIndex = 0;
            this.comboBoxVariate.SelectedIndexChanged += new EventHandler(this.comboBoxVariate_SelectedIndexChanged);
        }

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

            // On/Off
            if (item.MainAutoZeroIsAutoZeroUsing == false)
                this.buttonIsAutoZeroUsing.ButtonUp();
            else
                this.buttonIsAutoZeroUsing.ButtonDown();

            // + Range
            value =Helper.StringToDecimalPlaces(item.MainAutoZeroPlusRange, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
            if (this.labelPlusRange.Text != value)
                this.labelPlusRange.Text = value;

            // - Range
            value = Helper.StringToDecimalPlaces(item.MainAutoZeroMinusRange, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces);
            if (this.labelMinusRange.Text != value)
                this.labelMinusRange.Text = value;

            // Variate
            iValue = int.Parse(item.MainAutoZeroVariate);
            this.comboBoxVariate.SelectedIndexChanged -= new EventHandler(this.comboBoxVariate_SelectedIndexChanged);
            this.comboBoxVariate.SelectedIndex = iValue;
            this.comboBoxVariate.SelectedIndexChanged += new EventHandler(this.comboBoxVariate_SelectedIndexChanged);
        }

        public void DisplayRefresh(SystemStatus status)
        {
            this.ParentForm.ParentForm.CurrentSystemStatus.CurrentDisplay = Define.E_DisplayStore.SystemAutoZero;
            this.ParentForm.ParentForm.SetDisplayMode(Define.E_DisplayMode.Menu);
            this.ParentForm.DisplayTitleRoot(this.ParentForm.ParentForm.CurrentSystemStatus);

            //this.UpdateZeroParameterDisplay(this.ParentForm.ParentForm.CurrentSystemParameter4);
            this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Read, CommunicationID.MainBoard, CommunicationAddress.ParameterRead4907, "");
        }
        #endregion

        #region Event Handler
        private void buttonIsAutoZeroUsing_Click(object sender, EventArgs e)
        {
            string value = "";
            string before = "", after = "";

            if (this.ParentForm.ParentForm.CurrentSystemParameter4.LCDAutoZeroIsAutoZeroUsing == true)
                before = "ON";
            else
                before = "OFF";

            if (this.buttonIsAutoZeroUsing.ButtonStatus == SmartButton.BUTSTATUS.DOWN)
            {
                this.ParentForm.ParentForm.CurrentSystemParameter4.LCDAutoZeroIsAutoZeroUsing = true;
                after = "ON";
            }
            else
            {
                this.ParentForm.ParentForm.CurrentSystemParameter4.LCDAutoZeroIsAutoZeroUsing = false;
                after = "OFF";
            }

            this.ParentForm.ParentForm.SaveSystemParameter4File(this.ParentForm.ParentForm.CurrentSystemParameter4);

            value = Helper.StringZeroFillDigits4(Convert.ToInt16(this.ParentForm.ParentForm.CurrentSystemParameter4.LCDAutoZeroIsAutoZeroUsing));
            this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.AutoZeroIsUsing, value);

            if (before != after)
                this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.Auto0IsUsing, "", before, after);
        }
        private void labelPlusRange_Click(object sender, EventArgs e)
        {
            string value = "";
            string before = "", after = "";

            before = this.labelPlusRange.Text;
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelPlusRange.Text, 5, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces, false, this.ParentForm.ParentForm.SystemConfig1.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue > 500.0)
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig1.Language);
                    myMsg.ShowDialog();
                }
                else
                {
                    after = this.labelPlusRange.Text = myKeyPad.StringValue;
                    this.ParentForm.ParentForm.CurrentSystemParameter4.LCDAutoZeroPlusRange = myKeyPad.StringValue.Replace(".","");
                    this.ParentForm.ParentForm.SaveSystemParameter4File(this.ParentForm.ParentForm.CurrentSystemParameter4);

                    value = Helper.StringZeroFillDigits7(this.ParentForm.ParentForm.CurrentSystemParameter4.LCDAutoZeroPlusRange);
                    this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.AutoZeroPlusRange, value);

                    if (before != after)
                        this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.Auto0RangePlus, "", before, after);
                }
            }
        }
        private void labelMinusRange_Click(object sender, EventArgs e)
        {
            string value = "";
            string before = "", after = "";

            before = this.labelMinusRange.Text;
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelMinusRange.Text, 5, this.ParentForm.ParentForm.SystemConfig1.DecimalPlaces, false, this.ParentForm.ParentForm.SystemConfig1.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue > 500.0)
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig1.Language);
                    myMsg.ShowDialog();
                }
                else
                {
                    after = this.labelMinusRange.Text = myKeyPad.StringValue;
                    this.ParentForm.ParentForm.CurrentSystemParameter4.LCDAutoZeroMinusRange = myKeyPad.StringValue.Replace(".", "");
                    this.ParentForm.ParentForm.SaveSystemParameter4File(this.ParentForm.ParentForm.CurrentSystemParameter4);

                    value = Helper.StringZeroFillDigits7(this.ParentForm.ParentForm.CurrentSystemParameter4.LCDAutoZeroMinusRange);
                    this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.AutoZeroMinusRange, value);

                    if (before != after)
                        this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.Auto0RangeMinus, "", before, after);
                }
            }
        }
        private void comboBoxVariate_SelectedIndexChanged(object sender, EventArgs e)
        {
            string value = "";
            string before = "", after = "";

            before = this.ParentForm.ParentForm.CurrentSystemParameter4.LCDAutoZeroVariate;

            after = this.ParentForm.ParentForm.CurrentSystemParameter4.LCDAutoZeroVariate = this.comboBoxVariate.SelectedIndex.ToString();
            this.ParentForm.ParentForm.SaveSystemParameter4File(this.ParentForm.ParentForm.CurrentSystemParameter4);

            value = Helper.StringZeroFillDigits4(this.comboBoxVariate.SelectedIndex.ToString());
            this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.AutoZeroVariate, value);

            if (before != after)
                this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.Auto0Variation, "", before, after);
        }
        #endregion
    }
}
