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

using SmartX;
using INT69DC_7C.Forms;

namespace INT69DC_7C.Controls
{
    public partial class ControlCalibration10 : UserControl
    {
        #region Field
        private FormCalibration m_ParentForm;

        private Color NormalColor = Color.Black;
        private Color FinishColor = Color.Blue;

        private Collection<SmartButton> CollButtonLine;
        private Collection<SmartLabel> CollLabelWeight;
        private Collection<SmartLabel> CollLabelADC;
        private Collection<SmartLabel> CollLabelConstant;
        private Collection<CalibrationStatus> CollCalStatus;
        #endregion

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

            this.ParentForm = parent;

            this.CreateCollection();
            this.InitilizeControls();
            this.InitializeDesign();
            this.DisplayRefresh();
        }
        #endregion

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

        #region Method
        private void CreateCollection()
        {
            this.CollButtonLine = new Collection<SmartButton>();
            this.CollLabelWeight = new Collection<SmartLabel>();
            this.CollLabelADC = new Collection<SmartLabel>();
            this.CollLabelConstant = new Collection<SmartLabel>();
            this.CollCalStatus = new Collection<CalibrationStatus>();

            this.CollButtonLine.Clear();
            this.CollLabelWeight.Clear();
            this.CollLabelADC.Clear();
            this.CollLabelConstant.Clear();
            this.CollCalStatus.Clear();

            this.CollButtonLine.Add(this.buttonLine1);
            this.CollButtonLine.Add(this.buttonLine2);
            this.CollButtonLine.Add(this.buttonLine3);
            this.CollButtonLine.Add(this.buttonLine4);
            this.CollButtonLine.Add(this.buttonLine5);
            this.CollButtonLine.Add(this.buttonLine6);
            this.CollButtonLine.Add(this.buttonLine7);
            this.CollButtonLine.Add(this.buttonLine8);
            this.CollButtonLine.Add(this.buttonLine9);
            this.CollButtonLine.Add(this.buttonLine10);

            this.CollLabelWeight.Add(this.labelWeight1);
            this.CollLabelWeight.Add(this.labelWeight2);
            this.CollLabelWeight.Add(this.labelWeight3);
            this.CollLabelWeight.Add(this.labelWeight4);
            this.CollLabelWeight.Add(this.labelWeight5);
            this.CollLabelWeight.Add(this.labelWeight6);
            this.CollLabelWeight.Add(this.labelWeight7);
            this.CollLabelWeight.Add(this.labelWeight8);
            this.CollLabelWeight.Add(this.labelWeight9);
            this.CollLabelWeight.Add(this.labelWeight10);

            this.CollLabelADC.Add(this.labelADC1);
            this.CollLabelADC.Add(this.labelADC2);
            this.CollLabelADC.Add(this.labelADC3);
            this.CollLabelADC.Add(this.labelADC4);
            this.CollLabelADC.Add(this.labelADC5);
            this.CollLabelADC.Add(this.labelADC6);
            this.CollLabelADC.Add(this.labelADC7);
            this.CollLabelADC.Add(this.labelADC8);
            this.CollLabelADC.Add(this.labelADC9);
            this.CollLabelADC.Add(this.labelADC10);

            this.CollLabelConstant.Add(this.labelConstant1);
            this.CollLabelConstant.Add(this.labelConstant2);
            this.CollLabelConstant.Add(this.labelConstant3);
            this.CollLabelConstant.Add(this.labelConstant4);
            this.CollLabelConstant.Add(this.labelConstant5);
            this.CollLabelConstant.Add(this.labelConstant6);
            this.CollLabelConstant.Add(this.labelConstant7);
            this.CollLabelConstant.Add(this.labelConstant8);
            this.CollLabelConstant.Add(this.labelConstant9);
            this.CollLabelConstant.Add(this.labelConstant10);

            for (int i = 0; i < 10; i++)
                this.CollCalStatus.Add(new CalibrationStatus());
        }
        private void InitilizeControls()
        {
            for (int i = 0; i < this.ParentForm.ParentForm.SystemConfig.EquipmentColumns; i++)
            {
                this.CollLabelWeight[i].Text = "0.0";
                this.CollLabelADC[i].Text = "0.0";
                this.CollLabelConstant[i].Text = "0.0";
            }
        }
        private void InitializeDesign()
        {

        }

        public void DisplayRefresh()
        {
            if (this.CollButtonLine == null || this.CollButtonLine.Count == 0)
                return;

            for (int i = 0; i < this.CollButtonLine.Count; i++)
            {
                this.CollButtonLine[i].ButtonUp();
                this.CollCalStatus[i].Initialize();
            }
        }

        public void CalibrationBalance()
        {
            string detail = "";

            if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.Korean)
            {
                this.ParentForm.listBoxHelp.Items.Add("분동 계산중 입니다");
                this.ParentForm.listBoxHelp.Items.Add("잠시만 기다리세요");
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.English)
            {
                this.ParentForm.listBoxHelp.Items.Add("Caculating...");
                this.ParentForm.listBoxHelp.Items.Add("Wait a minute.");
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.Chinese)
            {
                this.ParentForm.listBoxHelp.Items.Add("计算...");
                this.ParentForm.listBoxHelp.Items.Add("请稍等");
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.Czech)
            {
                this.ParentForm.listBoxHelp.Items.Add("výpočet...");
                this.ParentForm.listBoxHelp.Items.Add("Vyčkejte.");
            }

            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.German)
            {
                this.ParentForm.listBoxHelp.Items.Add("Gewichtsüberprüfung,");
                this.ParentForm.listBoxHelp.Items.Add("bitte warten");
            }
            else
            {

            }

            for (int i = 0; i < this.CollButtonLine.Count; i++)
            {
                if (this.CollButtonLine[i].ButtonStatus == SmartX.SmartButton.BUTSTATUS.DOWN)
                {
                    this.ParentForm.ParentForm.TransferData(CommunicationCommand.CalibrationBalance, this.CollButtonLine[i].Tag.ToString());

                    // Part 11
                    if (this.ParentForm.ParentForm.SystemConfig.IsPart11 == true)
                    {
                        detail = string.Format("{0} Lane Balance", i + 1);
                        this.ParentForm.ParentForm.SetTrackingHistoryData(DataStore.TrackingOperation.Calibration, detail);
                    }
                }
            }

            this.ParentForm.CalibrationButtonEnable(false, false, true);
        }
        public void CalibrationStart()
        {
            string detail = "";

            if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.Korean)
            {
                this.ParentForm.listBoxHelp.Items.Add("중량조정을 시작합니다.");
                this.ParentForm.listBoxHelp.Items.Add("잠시만 기다리세요.");
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.English)
            {
                this.ParentForm.listBoxHelp.Items.Add("Calibration start.");
                this.ParentForm.listBoxHelp.Items.Add("Wait a minute.");
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.Chinese)
            {
                this.ParentForm.listBoxHelp.Items.Add("校准开始按钮");
                this.ParentForm.listBoxHelp.Items.Add("请稍等");
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.Czech)
            {
                this.ParentForm.listBoxHelp.Items.Add("START kalibrace");
                this.ParentForm.listBoxHelp.Items.Add("Vyčkejte.");
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.German)
            {
                this.ParentForm.listBoxHelp.Items.Add("Kalibrierungsstart");
                this.ParentForm.listBoxHelp.Items.Add("bitte warten");
            }
            else
            {

            }

            for (int i = 0; i < this.CollButtonLine.Count; i++)
            {
                if (this.CollButtonLine[i].ButtonStatus == SmartButton.BUTSTATUS.DOWN)
                {
                    this.ParentForm.ParentForm.TransferData(CommunicationCommand.CalibrationStart, this.CollButtonLine[i].Tag.ToString());

                    // Part 11
                    if (this.ParentForm.ParentForm.SystemConfig.IsPart11 == true)
                    {
                        this.CollCalStatus[i].Initialize();
                        detail = string.Format("{0} Lane Start", i + 1);
                        this.ParentForm.ParentForm.SetTrackingHistoryData(DataStore.TrackingOperation.Calibration, detail);
                    }
                }

                this.CollButtonLine[i].Enabled = false;
            }

            this.ParentForm.CalibrationButtonEnable(false, false, false);
        }
        public void CalibrationCancel()
        {
            string detail = "";

            if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.Korean)
            {
                this.ParentForm.listBoxHelp.Items.Add("중량조정을 취소합니다");
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.English)
            {
                this.ParentForm.listBoxHelp.Items.Add("Cancel the Calibration");
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.Chinese)
            {
                this.ParentForm.listBoxHelp.Items.Add("取消校准");
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.Czech)
            {
                this.ParentForm.listBoxHelp.Items.Add("Zrušení Kalibrace");
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.German)
            {
                this.ParentForm.listBoxHelp.Items.Add("Brechen Sie die ");
                this.ParentForm.listBoxHelp.Items.Add("Kalibrierung ab");
            }
            else
            {

            }

            for (int i = 0; i < this.CollButtonLine.Count; i++)
            {
                if (this.CollButtonLine[i].ButtonStatus == SmartX.SmartButton.BUTSTATUS.DOWN)
                {
                    this.ParentForm.ParentForm.TransferData(CommunicationCommand.CalibrationCancel, this.CollButtonLine[i].Tag.ToString());

                    // Part 11
                    if (this.ParentForm.ParentForm.SystemConfig.IsPart11 == true)
                    {
                        if (this.CollCalStatus[i].IsCancel == false)
                        {
                            this.CollCalStatus[i].IsCancel = true;
                            detail = string.Format("{0} Lane Cancel", i + 1);
                            this.ParentForm.ParentForm.SetTrackingHistoryData(DataStore.TrackingOperation.Calibration, detail);
                        }
                    }
                }

                this.CollButtonLine[i].Enabled = true;
            }

            this.ParentForm.CalibrationButtonEnable(true, false, false);
        }

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

            value = Helper.StringToDecimalPlaces(config.Constant1.Trim(), 6);
            if (this.labelConstant1.Text != value)
                this.labelConstant1.Text = value;

            value = Helper.StringToDecimalPlaces(config.Constant2.Trim(), 6);
            if (this.labelConstant2.Text != value)
                this.labelConstant2.Text = value;

            value = Helper.StringToDecimalPlaces(config.Constant3.Trim(), 6);
            if (this.labelConstant3.Text != value)
                this.labelConstant3.Text = value;

            value = Helper.StringToDecimalPlaces(config.Constant4.Trim(), 6);
            if (this.labelConstant4.Text != value)
                this.labelConstant4.Text = value;

            value = Helper.StringToDecimalPlaces(config.Constant5.Trim(), 6);
            if (this.labelConstant5.Text != value)
                this.labelConstant5.Text = value;

            value = Helper.StringToDecimalPlaces(config.Constant6.Trim(), 6);
            if (this.labelConstant6.Text != value)
                this.labelConstant6.Text = value;

            value = Helper.StringToDecimalPlaces(config.Constant7.Trim(), 6);
            if (this.labelConstant7.Text != value)
                this.labelConstant7.Text = value;

            value = Helper.StringToDecimalPlaces(config.Constant8.Trim(), 6);
            if (this.labelConstant8.Text != value)
                this.labelConstant8.Text = value;

            value = Helper.StringToDecimalPlaces(config.Constant9.Trim(), 6);
            if (this.labelConstant9.Text != value)
                this.labelConstant9.Text = value;

            value = Helper.StringToDecimalPlaces(config.Constant10.Trim(), 6);
            if (this.labelConstant10.Text != value)
                this.labelConstant10.Text = value;
        }
        public void UpdateWeightDisplay(DataStore.EquipmentStatus status, Collection<WeightData> weights)
        {
            if (this.CollLabelWeight == null || weights.Count < this.CollLabelWeight.Count || status == DataStore.EquipmentStatus.Start)
                return;

            for (int i = 0; i < weights.Count; i++)
            {
                if (this.CollButtonLine[i].ButtonStatus == SmartX.SmartButton.BUTSTATUS.DOWN)
                {
                    this.UpdateLabelWeight(i, this.CollButtonLine[i], this.CollLabelWeight[i], this.CollLabelADC[i], weights[i], true);
                }
                else
                {
                    this.UpdateLabelWeight(i, this.CollButtonLine[i], this.CollLabelWeight[i], this.CollLabelADC[i], weights[i], false);
                }
            }
        }
        private void UpdateLabelWeight(int index, SmartButton buttonLane, SmartLabel labelWeight, SmartLabel labelADC, WeightData weightData, bool rbChecked)
        {
            string detail = "";

            switch (weightData.Status)
            {
                case DataStore.WeightStatus.CalNomal:
                    labelWeight.Text = Helper.DoubleToString(weightData.Weight, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    labelWeight.ForeColor = this.NormalColor;
                    labelADC.Text = weightData.ADCValue;
                    break;
                case DataStore.WeightStatus.CalStandby:
                    if (rbChecked == true)
                    {
                        // 분동중량 표시
                        labelWeight.Text = string.Format("-{0}", this.ParentForm.labelBalanceWeight.Text);
                        labelWeight.ForeColor = this.NormalColor;
                        labelADC.Text = weightData.ADCValue;
                        this.ParentForm.ClearListBox();
                        if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.Korean)
                        {
                            this.ParentForm.listBoxHelp.Items.Add("분동을 올려주세요.");
                        }
                        else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.English)
                        {
                            this.ParentForm.listBoxHelp.Items.Add("Put the balance weight.");
                        }
                        else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.Chinese)
                        {
                            this.ParentForm.listBoxHelp.Items.Add("放平衡重");
                        }
                        else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.Czech)
                        {
                            this.ParentForm.listBoxHelp.Items.Add("Položit vyvážení hmotnosti.");
                        }
                        else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.German)
                        {
                            this.ParentForm.listBoxHelp.Items.Add("Gewicht auf den Förderer legen");
                        }
                        else
                        {

                        }

                        this.ParentForm.CalibrationButtonEnable(false, true, true);
                    }
                    break;
                case DataStore.WeightStatus.CalBalans:
                    if (rbChecked == true)
                    {
                        // 분동중량표시
                        labelWeight.Text = string.Format("-{0}", this.ParentForm.labelBalanceWeight.Text);
                        labelWeight.ForeColor = this.NormalColor;
                        labelADC.Text = weightData.ADCValue;
                        this.ParentForm.CalibrationButtonEnable(false, false, true);
                    }
                    break;
                case DataStore.WeightStatus.CalFinish:
                    if (rbChecked == true)
                    {
                        this.ParentForm.ClearListBox();
                        if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.Korean)
                        {
                            this.ParentForm.listBoxHelp.Items.Add("중량조정이 완료 되었습니다.");
                        }
                        else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.English)
                        {
                            this.ParentForm.listBoxHelp.Items.Add("Finished.");
                        }
                        else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.Chinese)
                        {
                            this.ParentForm.listBoxHelp.Items.Add("完");
                        }
                        else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.Czech)
                        {
                            this.ParentForm.listBoxHelp.Items.Add("Ukončený.");
                        }
                        else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.German)
                        {
                            this.ParentForm.listBoxHelp.Items.Add("Die Kalibrierung ist abgeschlossen.");
                        }
                        else
                        {

                        }
                        this.ParentForm.CalibrationButtonEnable(true, false, false);
                        foreach (SmartX.SmartButton bt in this.CollButtonLine)
                            bt.Enabled = true;

                        // Part11
                        if (this.ParentForm.ParentForm.SystemConfig.IsPart11 == true)
                        {
                            if (this.CollCalStatus[index].IsFinish == false)
                            {
                                this.CollCalStatus[index].IsFinish = true;
                                detail = string.Format("{0}Lane Finish", index + 1);
                                this.ParentForm.ParentForm.SetTrackingHistoryData(DataStore.TrackingOperation.Calibration, detail);
                            }
                        }
                    }
                    labelWeight.Text = Helper.DoubleToString(weightData.Weight, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    labelWeight.ForeColor = this.FinishColor;
                    labelADC.Text = weightData.ADCValue;
                    break;
                case DataStore.WeightStatus.CalError:
                    if (rbChecked == true)
                    {
                        this.ParentForm.ClearListBox();
                        if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.Korean)
                        {
                            this.ParentForm.listBoxHelp.Items.Add("중량조정 에러");
                            this.ParentForm.listBoxHelp.Items.Add("다시 시도해 주세요");
                        }
                        else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.English)
                        {
                            this.ParentForm.listBoxHelp.Items.Add("Calibration error.");
                            this.ParentForm.listBoxHelp.Items.Add("Please try again.");
                        }
                        else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.Chinese)
                        {
                            this.ParentForm.listBoxHelp.Items.Add("错误发生，重试");
                        }
                        else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.Czech)
                        {
                            this.ParentForm.listBoxHelp.Items.Add("Kalibrace chyba.");
                            this.ParentForm.listBoxHelp.Items.Add("Zkuste znovu.");
                        }
                        else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.German)
                        {
                            this.ParentForm.listBoxHelp.Items.Add("Fehler!");
                            this.ParentForm.listBoxHelp.Items.Add("Bitte noch einmal versuchen");
                        }
                        else
                        {

                        }
                        this.ParentForm.CalibrationButtonEnable(true, false, false);
                        foreach (SmartX.SmartButton bt in this.CollButtonLine)
                            bt.Enabled = true;

                        // Part11
                        if (this.ParentForm.ParentForm.SystemConfig.IsPart11 == true)
                        {
                            if (this.CollCalStatus[index].IsError == false)
                            {
                                this.CollCalStatus[index].IsError = true;
                                detail = string.Format("{0}Lane Error", index + 1);
                                this.ParentForm.ParentForm.SetTrackingHistoryData(DataStore.TrackingOperation.Calibration, detail);
                            }
                        }
                    }
                    labelWeight.Text = Helper.DoubleToString(weightData.Weight, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    labelWeight.ForeColor = this.NormalColor;
                    labelADC.Text = weightData.ADCValue;
                    break;
                default:
                    break;
            }
        }
        #endregion

        #region Event Handler
        private void buttonLine_Click(object sender, EventArgs e)
        {
            SmartButton bt = sender as SmartButton;

            if (bt == null)
                return;

            this.ParentForm.ClearListBox();
            if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.Korean)
            {
                this.ParentForm.listBoxHelp.Items.Add("중량조정");
                this.ParentForm.listBoxHelp.Items.Add("LANE을 선택 후");
                this.ParentForm.listBoxHelp.Items.Add("시작을 누르세요");
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.English)
            {
                this.ParentForm.listBoxHelp.Items.Add("Calibration");
                this.ParentForm.listBoxHelp.Items.Add("After selecting line, ");
                this.ParentForm.listBoxHelp.Items.Add("push the [Start] button.");
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.Chinese)
            {
                this.ParentForm.listBoxHelp.Items.Add("校准");
                this.ParentForm.listBoxHelp.Items.Add("选择线后，按[开始]按钮。");
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.Czech)
            {
                this.ParentForm.listBoxHelp.Items.Add("Kalibrace");
                this.ParentForm.listBoxHelp.Items.Add("Po výběru řádku, ");
                this.ParentForm.listBoxHelp.Items.Add("stiskněte tlačítko [Start].");
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.LanguageID.German)
            {
                this.ParentForm.listBoxHelp.Items.Add("Kalibrierung");
                this.ParentForm.listBoxHelp.Items.Add("Drücken Sie nach der Auswahl der");
                this.ParentForm.listBoxHelp.Items.Add("Zeile die Taste [Start].");
            }
            else
            {

            }

            this.ParentForm.CalibrationButtonEnable(true, false, false);
        }
        #endregion
    }
}
