using System;
using System.Linq;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using INT89DB_26_ImageDll;
using INT89DB_26.Controls;
using INT89DB_26.DialogForms;

namespace INT89DB_26.Forms
{
    public partial class FormCalibration : Form
    {
        #region Field
        private FormMain m_ParentForm;

        private ControlCalibration2 Calibration2;
        private ControlCalibration3 Calibration3;
        private ControlCalibration4 Calibration4;
        private ControlCalibration5 Calibration5;
        private ControlCalibration6 Calibration6;
        #endregion

        #region Constructor
        public FormCalibration(FormMain parent)
        {
            InitializeComponent();

            this.ParentForm = parent;
        } 
        #endregion

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

        #endregion

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

            if (this.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.Korean)
            {

            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.English)
            {
                this.labelTitle.Text = "Calibration";
                this.labelStaticHelp.Text = "Help";
                this.labelStaticBalanceWeight.Text = "Balance.W";
                this.labelStaticMaxWeight.Text = "Max.W";
                this.labelStaticDigit.Text = "Digit";

                this.buttonBack.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engBackDown));
                this.buttonBack.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engBackUp));

                this.buttonStart.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.engFormCalibrationStartDisable));
                this.buttonStart.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engFormCalibrationStartDown));
                this.buttonStart.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engFormCalibrationStartUp));
                this.buttonBalance.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.engFormCalibrationWeightDisable));
                this.buttonBalance.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engFormCalibrationWeightDown));
                this.buttonBalance.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engFormCalibrationWeightUp));
                this.buttonCancel.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.engFormCalibrationCancelDisable));
                this.buttonCancel.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engFormCalibrationCancelDown));
                this.buttonCancel.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engFormCalibrationCancelUp));
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.Chinese)
            {
                this.labelTitle.Text = "校准";
                this.labelStaticHelp.Text = "帮助";
                this.labelStaticBalanceWeight.Text = "砝码重量";
                this.labelStaticMaxWeight.Text = "最大重量";
                this.labelStaticDigit.Text = "数字";

                this.buttonBack.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnBackDown));
                this.buttonBack.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnBackUp));

                this.buttonStart.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnFormCalibrationStartDisable));
                this.buttonStart.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnFormCalibrationStartDown));
                this.buttonStart.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnFormCalibrationStartUp));
                this.buttonBalance.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnFormCalibrationWeightDisable));
                this.buttonBalance.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnFormCalibrationWeightDown));
                this.buttonBalance.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnFormCalibrationWeightUp));
                this.buttonCancel.DisableImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnFormCalibrationCancelDisable));
                this.buttonCancel.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnFormCalibrationCancelDown));
                this.buttonCancel.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnFormCalibrationCancelUp));
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.Czech)
            {

            }
            else
            {

            }
        }
        private void InitializeControl()
        {
            this.listBoxHelp.Items.Clear();

            this.labelBalanceWeight.Text = Helper.StringToDecimalPlaces("0", this.ParentForm.SystemConfig.DecimalPlaces);
            this.labelMaxWeight.Text = Helper.StringToDecimalPlaces("0", this.ParentForm.SystemConfig.DecimalPlaces);

            this.comboBoxDigit.Items.Clear();
            this.comboBoxDigit.Items.Add("1");
            this.comboBoxDigit.Items.Add("2");
            this.comboBoxDigit.Items.Add("5");

            this.comboBoxDigit.SelectedIndexChanged -= new EventHandler(this.comboBoxDigit_SelectedIndexChanged);
            this.comboBoxDigit.SelectedItem = "1";
            this.comboBoxDigit.SelectedIndexChanged += new EventHandler(this.comboBoxDigit_SelectedIndexChanged);

            if (this.ParentForm.SystemConfig.EquipmentColumns == 2)
            {
                this.Calibration2 = new ControlCalibration2(this);
                this.Calibration2.Location = new Point(0, 70);

                this.Controls.Add(this.Calibration2);
            }
            else if (this.ParentForm.SystemConfig.EquipmentColumns == 3)
            {
                this.Calibration3 = new ControlCalibration3(this);
                this.Calibration3.Location = new Point(0, 70);

                this.Controls.Add(this.Calibration3);
            }
            else if (this.ParentForm.SystemConfig.EquipmentColumns == 4)
            {
                this.Calibration4 = new ControlCalibration4(this);
                this.Calibration4.Location = new Point(0, 70);

                this.Controls.Add(this.Calibration4);
            }
            else if (this.ParentForm.SystemConfig.EquipmentColumns == 5)
            {
                this.Calibration5 = new ControlCalibration5(this);
                this.Calibration5.Location = new Point(0, 70);

                this.Controls.Add(this.Calibration5);
            }
            else
            {
                this.Calibration6 = new ControlCalibration6(this);
                this.Calibration6.Location = new Point(0, 70);

                this.Controls.Add(this.Calibration6);
            }


            if (this.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.Korean)
            {
                this.listBoxHelp.Items.Add("중량조정");
                this.listBoxHelp.Items.Add("LANE을 선택 후");
                this.listBoxHelp.Items.Add("시작을 누르세요");
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.English)
            {
                this.listBoxHelp.Items.Add("Calibration");
                this.listBoxHelp.Items.Add("After select Lane,");
                this.listBoxHelp.Items.Add("push the [Start] button.");
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.Chinese)
            {
                this.listBoxHelp.Items.Add("校准");
                this.listBoxHelp.Items.Add("选择线后，按[开始]按钮。");
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.Czech)
            {

            }
            else
            {

            }

        }

        public void DisplayRefresh()
        {
            this.ParentForm.TransferData(CommunicationCommand.ModeCalibration, CommunicationID.MainBoard);
            this.timer.Enabled = true;

            this.ClearListBox();

            switch (this.ParentForm.SystemConfig.EquipmentColumns)
            {
                case 2:
                    if (this.Calibration2 != null)
                        this.Calibration2.DisplayRefresh();
                    break;
                case 3:
                    if (this.Calibration3 != null)
                        this.Calibration3.DisplayRefresh();
                    break;
                case 4:
                    if (this.Calibration4 != null)
                        this.Calibration4.DisplayRefresh();
                    break;
                case 5:
                    if (this.Calibration5 != null)
                        this.Calibration5.DisplayRefresh();
                    break;
                case 6:
                    if (this.Calibration6 != null)
                        this.Calibration6.DisplayRefresh();
                    break;
                default:
                    break;
            }

            this.CalibrationButtonEnable(false, false, false);

            if (this.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.Korean)
            {
                this.listBoxHelp.Items.Add("중량조정");
                this.listBoxHelp.Items.Add("LANE을 선택 후");
                this.listBoxHelp.Items.Add("시작을 누르세요");
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.English)
            {
                this.listBoxHelp.Items.Add("Calibration");
                this.listBoxHelp.Items.Add("After select Lane,");
                this.listBoxHelp.Items.Add("push the [Start] button.");
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.Chinese)
            {
                this.listBoxHelp.Items.Add("校准");
                this.listBoxHelp.Items.Add("选择线后，按[开始]按钮。");
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.Czech)
            {

            }
            else
            {

            }

        }
        public void ClearListBox()
        {
            this.listBoxHelp.Items.Clear();
        }
        public void CalibrationButtonEnable(bool btStart, bool btBalance, bool btCcancel)
        {
            if (this.buttonStart.Enabled != btStart)
                this.buttonStart.Enabled = btStart;
            if (this.buttonBalance.Enabled != btBalance)
                this.buttonBalance.Enabled = btBalance;
            if (this.buttonCancel.Enabled != btCcancel)
                this.buttonCancel.Enabled = btCcancel;
        }

        public void UpdateConfiguration(DataStore.EquipmentStatus status, CalibrationItem config)
        {
            string value = "";

            value = Helper.CommunicationWeightValueToString(config.BalanceWeight, this.ParentForm.SystemConfig.DecimalPlaces);
            if (this.labelBalanceWeight.Text != value)
                this.labelBalanceWeight.Text = value;

            value = Helper.CommunicationWeightValueToString(config.MaxWeight, this.ParentForm.SystemConfig.DecimalPlaces);
            if (this.labelMaxWeight.Text != value)
                this.labelMaxWeight.Text = value;

            value = config.Digit.Trim();
            if (this.comboBoxDigit.SelectedItem != value)
            {
                this.comboBoxDigit.SelectedIndexChanged -= new EventHandler(this.comboBoxDigit_SelectedIndexChanged);
                this.comboBoxDigit.SelectedItem = value;
                this.comboBoxDigit.SelectedIndexChanged += new EventHandler(this.comboBoxDigit_SelectedIndexChanged);
            }

            switch (this.ParentForm.SystemConfig.EquipmentColumns)
            {
                case 2:
                    this.Calibration2.UpdateConfiguration(status, config);
                    break;
                case 3:
                    this.Calibration3.UpdateConfiguration(status, config);
                    break;
                case 4:
                    this.Calibration4.UpdateConfiguration(status, config);
                    break;
                case 5:
                    this.Calibration5.UpdateConfiguration(status, config);
                    break;
                case 6:
                    this.Calibration6.UpdateConfiguration(status, config);
                    break;
                default:
                    break;
            }
        }
        public void UpdateWeightDisplay(DataStore.EquipmentStatus status, Collection<WeightData> weightDatas)
        {
            switch (this.ParentForm.SystemConfig.EquipmentColumns)
            {
                case 2:
                    this.Calibration2.UpdateWeightDisplay(status, weightDatas);
                    break;
                case 3:
                    this.Calibration3.UpdateWeightDisplay(status, weightDatas);
                    break;
                case 4:
                    this.Calibration4.UpdateWeightDisplay(status, weightDatas);
                    break;
                case 5:
                    this.Calibration5.UpdateWeightDisplay(status, weightDatas);
                    break;
                case 6:
                    this.Calibration6.UpdateWeightDisplay(status, weightDatas);
                    break;
                default:
                    break;
            }
        }
        #endregion

        #region Override Member
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

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

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

            value = Helper.StringZeroFillDigits4(this.comboBoxDigit.SelectedItem.ToString());

            this.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.SubBoardAll, CommunicationAddress.Digit, value);
        }

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

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelBalanceWeight.Text, 5, this.ParentForm.SystemConfig.DecimalPlaces, false, this.ParentForm.SystemConfig.Language);
            myKeyPad.Location = new Point(300, 90);

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

                    value = Helper.StringZeroFillDigits7(this.labelBalanceWeight.Text.Replace(".", ""));
                    this.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.SubBoardAll, CommunicationAddress.BalanceWeight, value);
                }
            }
        }

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

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelMaxWeight.Text, 5, this.ParentForm.SystemConfig.DecimalPlaces, false, this.ParentForm.SystemConfig.Language);
            myKeyPad.Location = new Point(300, 90);

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

                    value = Helper.StringZeroFillDigits7(this.labelMaxWeight.Text.Replace(".", ""));
                    this.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.SubBoardAll, CommunicationAddress.MaxWeight, value);
                }
            }
        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            this.ClearListBox();

            switch (this.ParentForm.SystemConfig.EquipmentColumns)
            {
                case 2:
                    this.Calibration2.CalibrationStart();
                    break;
                case 3:
                    this.Calibration3.CalibrationStart();
                    break;
                case 4:
                    this.Calibration4.CalibrationStart();
                    break;
                case 5:
                    this.Calibration5.CalibrationStart();
                    break;
                case 6:
                    this.Calibration6.CalibrationStart();
                    break;
                default:
                    break;
            }
        }

        private void buttonBalance_Click(object sender, EventArgs e)
        {
            this.ClearListBox();

            switch (this.ParentForm.SystemConfig.EquipmentColumns)
            {
                case 2:
                    this.Calibration2.CalibrationBalance();
                    break;
                case 3:
                    this.Calibration3.CalibrationBalance();
                    break;
                case 4:
                    this.Calibration4.CalibrationBalance();
                    break;
                case 5:
                    this.Calibration5.CalibrationBalance();
                    break;
                case 6:
                    this.Calibration6.CalibrationBalance();
                    break;
                default:
                    break;
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.ClearListBox();

            switch (this.ParentForm.SystemConfig.EquipmentColumns)
            {
                case 2:
                    this.Calibration2.CalibrationCancel();
                    break;
                case 3:
                    this.Calibration3.CalibrationCancel();
                    break;
                case 4:
                    this.Calibration4.CalibrationCancel();
                    break;
                case 5:
                    this.Calibration5.CalibrationCancel();
                    break;
                case 6:
                    this.Calibration6.CalibrationCancel();
                    break;
                default:
                    break;
            }
        }

        private void buttonBack_Click(object sender, EventArgs e)
        {
            this.ClearListBox();

            switch (this.ParentForm.SystemConfig.EquipmentColumns)
            {
                case 2:
                    this.Calibration2.CalibrationCancel();
                    break;
                case 3:
                    this.Calibration3.CalibrationCancel();
                    break;
                case 4:
                    this.Calibration4.CalibrationCancel();
                    break;
                case 5:
                    this.Calibration5.CalibrationCancel();
                    break;
                case 6:
                    this.Calibration6.CalibrationCancel();
                    break;
                default:
                    break;
            }

            this.ParentForm.ChildFormMenu.DisplayRefresh();
            ((FormMain)(Owner)).smartForm.Show((int)DataStore.E_FormStore.FormMenu);
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            this.timer.Enabled = false;

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