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 INT89DB_26.Forms;
using INT89DB_26_ImageDll;

namespace INT89DB_26.Controls
{
    public partial class ControlCalibration5 : UserControl
    {
        #region Field
        private FormCalibration ParentForm;

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

        private Collection<SmartButton> CollectionButtonLine;
        private Collection<SmartLabel> CollectionLabelWeight;
        private Collection<SmartLabel> CollectionLabelADC;
        private Collection<SmartLabel> CollectionLabelConstant;
        #endregion

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

            this.ParentForm = parent;

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

        #region Property

        #endregion

        #region Method
        private void CreateCollection()
        {
            this.CollectionButtonLine = new Collection<SmartButton>();
            this.CollectionLabelWeight = new Collection<SmartLabel>();
            this.CollectionLabelADC = new Collection<SmartLabel>();
            this.CollectionLabelConstant = new Collection<SmartLabel>();

            this.CollectionButtonLine.Clear();
            this.CollectionLabelWeight.Clear();
            this.CollectionLabelADC.Clear();
            this.CollectionLabelConstant.Clear();

            this.CollectionButtonLine.Add(this.buttonLine1);
            this.CollectionButtonLine.Add(this.buttonLine2);
            this.CollectionButtonLine.Add(this.buttonLine3);
            this.CollectionButtonLine.Add(this.buttonLine4);
            this.CollectionButtonLine.Add(this.buttonLine5);

            this.CollectionLabelWeight.Add(this.labelWeight1);
            this.CollectionLabelWeight.Add(this.labelWeight2);
            this.CollectionLabelWeight.Add(this.labelWeight3);
            this.CollectionLabelWeight.Add(this.labelWeight4);
            this.CollectionLabelWeight.Add(this.labelWeight5);

            this.CollectionLabelADC.Add(this.labelADC1);
            this.CollectionLabelADC.Add(this.labelADC2);
            this.CollectionLabelADC.Add(this.labelADC3);
            this.CollectionLabelADC.Add(this.labelADC4);
            this.CollectionLabelADC.Add(this.labelADC5);

            this.CollectionLabelConstant.Add(this.labelConstant1);
            this.CollectionLabelConstant.Add(this.labelConstant2);
            this.CollectionLabelConstant.Add(this.labelConstant3);
            this.CollectionLabelConstant.Add(this.labelConstant4);
            this.CollectionLabelConstant.Add(this.labelConstant5);
        }
        private void InitilizeControls()
        {
            for (int i = 0; i < this.ParentForm.ParentForm.SystemConfig.EquipmentColumns; i++)
            {
                this.CollectionLabelWeight[i].Text = "0.0";
                this.CollectionLabelADC[i].Text = "0.0";
                this.CollectionLabelConstant[i].Text = "0.0";
            }
        }
        private void InitializeDesign()
        {
            Class1 images = new Class1();

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

            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.English)
            {
                this.labelStaticLineNumber.Text = "No.";
                this.labelStaticLineSelect.Text = "Line\nSelect";
                this.labelStaticWeight.Text = "Weight";
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.Chinese)
            {
                this.labelStaticLineNumber.Text = "编号";
                this.labelStaticLineSelect.Text = "选择路线";
                this.labelStaticWeight.Text = "重量";
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.Czech)
            {

            }
            else
            {

            }
        }

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

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

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

            }

            foreach (SmartButton bt in this.CollectionButtonLine)
            {
                if (bt.ButtonStatus == SmartX.SmartButton.BUTSTATUS.DOWN)
                    this.ParentForm.ParentForm.TransferData(CommunicationCommand.CalibrationBalance, bt.Tag.ToString());
            }

            this.ParentForm.CalibrationButtonEnable(false, false, true);
        }
        public void CalibrationStart()
        {
            if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.Korean)
            {
                this.ParentForm.listBoxHelp.Items.Add("중량조정을 시작합니다.");
                this.ParentForm.listBoxHelp.Items.Add("잠시만 기다리세요.");
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.E_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.E_LanguageID.Chinese)
            {
                this.ParentForm.listBoxHelp.Items.Add("校准开始按钮");
                this.ParentForm.listBoxHelp.Items.Add("请稍等");
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.Czech)
            {
                this.ParentForm.listBoxHelp.Items.Add("START kalibrace");
                this.ParentForm.listBoxHelp.Items.Add("Vyčkejte.");
            }
            else
            {

            }

            foreach (SmartButton bt in this.CollectionButtonLine)
            {
                if (bt.ButtonStatus == SmartX.SmartButton.BUTSTATUS.DOWN)
                    this.ParentForm.ParentForm.TransferData(CommunicationCommand.CalibrationStart, bt.Tag.ToString());

                bt.Enabled = false;
            }

            this.ParentForm.CalibrationButtonEnable(false, false, false);
        }
        public void CalibrationCancel()
        {
            if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.Korean)
            {
                this.ParentForm.listBoxHelp.Items.Add("중량조정을 취소합니다");
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.English)
            {
                this.ParentForm.listBoxHelp.Items.Add("Cancel the Calibration");
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.Chinese)
            {
                this.ParentForm.listBoxHelp.Items.Add("取消校准");
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.Czech)
            {
                this.ParentForm.listBoxHelp.Items.Add("Zrušení Kalibrace");
            }
            else
            {

            }

            foreach (SmartButton bt in this.CollectionButtonLine)
            {
                if (bt.ButtonStatus == SmartX.SmartButton.BUTSTATUS.DOWN)
                    this.ParentForm.ParentForm.TransferData(CommunicationCommand.CalibrationCancel, bt.Tag.ToString());

                bt.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;
        }
        public void UpdateWeightDisplay(DataStore.EquipmentStatus status, Collection<WeightData> weights)
        {
            if (this.CollectionLabelWeight == null || weights.Count < this.CollectionLabelWeight.Count || status == DataStore.EquipmentStatus.Start)
                return;

            for (int i = 0; i < weights.Count; i++)
            {
                if (this.CollectionButtonLine[i].ButtonStatus == SmartX.SmartButton.BUTSTATUS.DOWN)
                {
                    this.UpdateLabelWeight(this.CollectionLabelWeight[i], this.CollectionLabelADC[i], weights[i], true);
                }
                else
                {
                    this.UpdateLabelWeight(this.CollectionLabelWeight[i], this.CollectionLabelADC[i], weights[i], false);
                }
            }
        }
        private void UpdateLabelWeight(SmartLabel labelWeight, SmartLabel labelADC, WeightData weightData, bool rbChecked)
        {
            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.E_LanguageID.Korean)
                        {
                            this.ParentForm.listBoxHelp.Items.Add("분동을 올려주세요.");
                        }
                        else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.English)
                        {
                            this.ParentForm.listBoxHelp.Items.Add("Put the balance.");
                        }
                        else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.Chinese)
                        {
                            this.ParentForm.listBoxHelp.Items.Add("放平衡重");
                        }
                        else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.Czech)
                        {
                            this.ParentForm.listBoxHelp.Items.Add("Položit vyvážení hmotnosti.");
                        }
                        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.E_LanguageID.Korean)
                        {
                            this.ParentForm.listBoxHelp.Items.Add("중량조정이 완료 되었습니다.");
                        }
                        else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.English)
                        {
                            this.ParentForm.listBoxHelp.Items.Add("Finished.");
                        }
                        else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.Chinese)
                        {
                            this.ParentForm.listBoxHelp.Items.Add("完");
                        }
                        else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.Czech)
                        {
                            this.ParentForm.listBoxHelp.Items.Add("Ukončený.");
                        }
                        else
                        {

                        }
                        this.ParentForm.CalibrationButtonEnable(true, false, false);
                        foreach (SmartX.SmartButton bt in this.CollectionButtonLine)
                            bt.Enabled = true;
                    }
                    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.E_LanguageID.Korean)
                        {
                            this.ParentForm.listBoxHelp.Items.Add("중량조정 에러");
                            this.ParentForm.listBoxHelp.Items.Add("다시 시도해 주세요");
                        }
                        else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.E_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.E_LanguageID.Chinese)
                        {
                            this.ParentForm.listBoxHelp.Items.Add("错误发生，重试");
                        }
                        else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.Czech)
                        {
                            this.ParentForm.listBoxHelp.Items.Add("Kalibrace chyba.");
                            this.ParentForm.listBoxHelp.Items.Add("Zkuste znovu.");
                        }
                        else
                        {

                        }
                        this.ParentForm.CalibrationButtonEnable(true, false, false);
                        foreach (SmartX.SmartButton bt in this.CollectionButtonLine)
                            bt.Enabled = true;
                    }
                    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.E_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.E_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.E_LanguageID.Chinese)
            {
                this.ParentForm.listBoxHelp.Items.Add("校准");
                this.ParentForm.listBoxHelp.Items.Add("选择线后，按[开始]按钮。");
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == DataStore.E_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
            {

            }

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