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 INT69DB_2A_ImageDll;
using INT69DB_2A.Controls;
using INT69DB_2A.DialogForms;

namespace INT69DB_2A.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;
        private ControlCalibration7 Calibration7;
        private ControlCalibration8 Calibration8;
        private ControlCalibration10 Calibration10;
        #endregion

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

            this.ParentForm = parent;

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

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

        #endregion

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

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

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

                this.buttonStart.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnFormCalibrationStartDisable));
                this.buttonStart.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnFormCalibrationStartDown));
                this.buttonStart.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnFormCalibrationStartUp));
                this.buttonBalance.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnFormCalibrationWeightDisable));
                this.buttonBalance.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnFormCalibrationWeightDown));
                this.buttonBalance.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnFormCalibrationWeightUp));
                this.buttonCancel.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnFormCalibrationCancelDisable));
                this.buttonCancel.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnFormCalibrationCancelDown));
                this.buttonCancel.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnFormCalibrationCancelUp));
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Japanese)
            {
                this.labelTitle.Text = "キャリブレㅡション";
                this.labelStaticHelp.Text = "ヘルプ";
                this.labelStaticBalanceWeight.Text = "分銅重量";
                this.labelStaticMaxWeight.Text = "最大重量";
                this.labelStaticDigit.Text = "目量";

                this.buttonStart.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.jpnFormCalibrationStartDisable));
                this.buttonStart.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.jpnFormCalibrationStartDown));
                this.buttonStart.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.jpnFormCalibrationStartUp));
                this.buttonBalance.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.jpnFormCalibrationWeightDisable));
                this.buttonBalance.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.jpnFormCalibrationWeightDown));
                this.buttonBalance.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.jpnFormCalibrationWeightUp));
                this.buttonCancel.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.jpnFormCalibrationCancelDisable));
                this.buttonCancel.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.jpnFormCalibrationCancelDown));
                this.buttonCancel.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.jpnFormCalibrationCancelUp));
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.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);

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

                    this.Controls.Add(this.Calibration2);
                    break;
                case 3:
                    this.Calibration3 = new ControlCalibration3(this);
                    this.Calibration3.Location = new Point(0, 70);

                    this.Controls.Add(this.Calibration3);
                    break;
                case 4:
                    this.Calibration4 = new ControlCalibration4(this);
                    this.Calibration4.Location = new Point(0, 70);

                    this.Controls.Add(this.Calibration4);
                    break;
                case 5:
                    this.Calibration5 = new ControlCalibration5(this);
                    this.Calibration5.Location = new Point(0, 70);

                    this.Controls.Add(this.Calibration5);
                    break;
                case 6:
                    this.Calibration6 = new ControlCalibration6(this);
                    this.Calibration6.Location = new Point(0, 70);

                    this.Controls.Add(this.Calibration6);
                    break;
                case 7:
                    this.Calibration7 = new ControlCalibration7(this);
                    this.Calibration7.Location = new Point(0, 70);

                    this.Controls.Add(this.Calibration7);
                    break;
                case 8:
                    this.Calibration8 = new ControlCalibration8(this);
                    this.Calibration8.Location = new Point(0, 70);

                    this.Controls.Add(this.Calibration8);
                    break;
                case 10:
                    this.Calibration10 = new ControlCalibration10(this);
                    this.Calibration10.Location = new Point(0, 70);

                    this.Controls.Add(this.Calibration10);
                    break;
                default:
                    break;
            }

            if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Korean)
            {
                this.listBoxHelp.Items.Add("중량조정");
                this.listBoxHelp.Items.Add("LANE을 선택 후");
                this.listBoxHelp.Items.Add("시작을 누르세요");
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.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.LanguageID.Chinese)
            {
                this.listBoxHelp.Items.Add("校准");
                this.listBoxHelp.Items.Add("选择线后，按[开始]按钮。");
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Japanese)
            {
                this.listBoxHelp.Items.Add("キャリブレㅡション");
                this.listBoxHelp.Items.Add("レㅡンを選択したら、");
                this.listBoxHelp.Items.Add("［スタㅡト］ボタンを押します。");
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.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;
                case 7:
                    this.Calibration7.UpdateConfiguration(status, config);
                    break;
                case 8:
                    this.Calibration8.UpdateConfiguration(status, config);
                    break;
                case 10:
                    this.Calibration10.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;
                case 7:
                    this.Calibration7.UpdateWeightDisplay(status, weightDatas);
                    break;
                case 8:
                    this.Calibration8.UpdateWeightDisplay(status, weightDatas);
                    break;
                case 10:
                    this.Calibration10.UpdateWeightDisplay(status, weightDatas);
                    break;
                default:
                    break;
            }
        }

        public void DisplayRefresh()
        {
            this.ParentForm.SystemConfig.CurrentForm = DataStore.FormStore.FormCalibration;
            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;
                case 7:
                    if (this.Calibration7 != null)
                        this.Calibration7.DisplayRefresh();
                    break;
                case 8:
                    if (this.Calibration8 != null)
                        this.Calibration8.DisplayRefresh();
                    break;
                case 10:
                    if (this.Calibration10 != null)
                        this.Calibration10.DisplayRefresh();
                    break;
                default:
                    break;
            }

            this.CalibrationButtonEnable(false, false, false);

            if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Korean)
            {
                this.listBoxHelp.Items.Add("중량조정");
                this.listBoxHelp.Items.Add("LANE을 선택 후");
                this.listBoxHelp.Items.Add("시작을 누르세요");
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.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.LanguageID.Chinese)
            {
                this.listBoxHelp.Items.Add("校准");
                this.listBoxHelp.Items.Add("选择线后，按[开始]按钮。");
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Japanese)
            {
                this.listBoxHelp.Items.Add("キャリブレㅡション");
                this.listBoxHelp.Items.Add("レㅡンを選択したら、");
                this.listBoxHelp.Items.Add("［スタㅡト］ボタンを押します。");
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Czech)
            {

            }
            else
            {

            }

        }
        #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._3003_Digit, value);
            if (this.ParentForm.SystemConfig.IsPart11 == true)
                this.ParentForm.SetTrackingHistoryData(DataStore.TrackingParameter.CalDigit, "", "", value);
        }

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

            before = this.labelBalanceWeight.Text;
            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
                {
                    after = this.labelBalanceWeight.Text = myKeyPad.StringValue;

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

                    if (this.ParentForm.SystemConfig.IsPart11 == true)
                        this.ParentForm.SetTrackingHistoryData(DataStore.TrackingParameter.CalBalWeight, "", before, after);
                }
            }
        }

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

            before = this.labelMaxWeight.Text;
            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
                {
                    after = this.labelMaxWeight.Text = myKeyPad.StringValue;

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

                    if (this.ParentForm.SystemConfig.IsPart11 == true)
                        this.ParentForm.SetTrackingHistoryData(DataStore.TrackingParameter.CalMaxWeight, "", before, after);
                }
            }
        }

        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;
                case 7:
                    this.Calibration7.CalibrationStart();
                    break;
                case 8:
                    this.Calibration8.CalibrationStart();
                    break;
                case 10:
                    this.Calibration10.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;
                case 7:
                    this.Calibration7.CalibrationBalance();
                    break;
                case 8:
                    this.Calibration8.CalibrationBalance();
                    break;
                case 10:
                    this.Calibration10.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;
                case 7:
                    this.Calibration7.CalibrationCancel();
                    break;
                case 8:
                    this.Calibration8.CalibrationCancel();
                    break;
                case 10:
                    this.Calibration10.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;
                case 7:
                    this.Calibration7.CalibrationCancel();
                    break;
                case 8:
                    this.Calibration8.CalibrationCancel();
                    break;
                case 10:
                    this.Calibration10.CalibrationCancel();
                    break;
                default:
                    break;
            }

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

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

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