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 INT63DC_6CH.Controls;
using INT63DC_6CH.DialogForms;
using INT63DC_ImageDll;

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

        public bool IsBasicMode;

        private ControlCalibration6 Calibration6;
        private ControlCalibration7 Calibration7;
        private ControlCalibration8 Calibration8;
        private ControlCalibration10 Calibration10;
        private ControlCalibration12 Calibration12;
        #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()
        {
            ImageDll images = new ImageDll();

            switch (this.ParentForm.SystemConfig.Language)
            {
                case DataStore.E_LanguageID.Korean:
                    break;
                case DataStore.E_LanguageID.English:
                    #region English
                    this.labelTitle.Text = "Calibration";
                    this.labelTitleZero.Text = "Zero";
                    this.labelTitleStart.Text = "Start";
                    this.labelTitleCancel.Text = "Cancel";
                    this.groupBoxHelp.Text = "Help";

                    this.buttonExtanalCAL.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engCorrectionDisable));
                    this.buttonExtanalCAL.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engCorrectionDown));
                    this.buttonExtanalCAL.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engCorrectionUp));
                    this.buttonBack.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engBackDisable));
                    this.buttonBack.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engBackDown));
                    this.buttonBack.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engBackUp));
                    #endregion
                    break;
                case DataStore.E_LanguageID.Chinese:
                    #region Chinese
                    this.labelTitle.Text = "校准";
                    this.labelTitleZero.Text = "零";
                    this.labelTitleStart.Text = "开启";
                    this.labelTitleCancel.Text = "取消";
                    this.groupBoxHelp.Text = "帮助";
                    
                    this.buttonExtanalCAL.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engCorrectionDisable));
                    this.buttonExtanalCAL.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engCorrectionDown));
                    this.buttonExtanalCAL.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engCorrectionUp));
                    this.buttonBack.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnBackDisable));
                    this.buttonBack.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnBackDown));
                    this.buttonBack.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnBackUp));
                    #endregion
                    break;
                case DataStore.E_LanguageID.Czech:
                    #region Czech
                    this.labelTitle.Text = "Kalibrace";
                    this.labelTitleZero.Text = "Nula";
                    this.labelTitleStart.Text = "Start";
                    this.labelTitleCancel.Text = "Zrušit";
                    this.groupBoxHelp.Text = "Pomoc";

                    this.buttonExtanalCAL.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engCorrectionDisable));
                    this.buttonExtanalCAL.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engCorrectionDown));
                    this.buttonExtanalCAL.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engCorrectionUp));
                    this.buttonBack.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.czeBackDisable));
                    this.buttonBack.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.czeBackDown));
                    this.buttonBack.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.czeBackUp));
                    #endregion
                    break;
                case DataStore.E_LanguageID.German:
                    #region German
                    this.labelTitle.Text = "Kalibrierung";
                    this.labelTitleZero.Text = "Null";
                    this.labelTitleStart.Text = "Start";
                    this.labelTitleCancel.Text = "Stornieren";
                    this.groupBoxHelp.Text = "Hilfe";

                    this.buttonExtanalCAL.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.gerCorrectionDisable));
                    this.buttonExtanalCAL.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.gerCorrectionDown));
                    this.buttonExtanalCAL.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.gerCorrectionUp));
                    this.buttonBack.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.gerBackDisable));
                    this.buttonBack.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.gerBackDown));
                    this.buttonBack.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.gerBackUp));
                    #endregion
                    break;
                case DataStore.E_LanguageID.Japanese:
                    #region Japanese
                    this.labelTitle.Text = "校正";
                    this.labelTitleZero.Text = "ゼロ";
                    this.labelTitleStart.Text = "スタㅡト";
                    this.labelTitleCancel.Text = "キャンセル";
                    this.groupBoxHelp.Text = "ヘルプ";

                    this.buttonExtanalCAL.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engCorrectionDisable));
                    this.buttonExtanalCAL.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engCorrectionDown));
                    this.buttonExtanalCAL.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engCorrectionUp));
                    this.buttonBack.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.jpnBackDisable));
                    this.buttonBack.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.jpnBackDown));
                    this.buttonBack.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.jpnBackUp));
                    #endregion
                    break;
                default:
                    break;
            }
        }
        private void InitializeControl()
        {
            this.IsBasicMode = true;
            this.buttonExtanalCAL.ButtonUp();

            switch (this.ParentForm.SystemConfig.EquipmentColumns)
            {
                case 6:
                    this.Calibration6 = new ControlCalibration6(this);
                    this.Calibration6.Location = new Point(0, 80);
                    this.Controls.Add(this.Calibration6);
                    break;
                case 7:
                    this.Calibration7 = new ControlCalibration7(this);
                    this.Calibration7.Location = new Point(0, 80);
                    this.Controls.Add(this.Calibration7);
                    break;
                case 8:
                    this.Calibration8 = new ControlCalibration8(this);
                    this.Calibration8.Location = new Point(0, 80);
                    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;
                case 12:
                    this.Calibration12 = new ControlCalibration12(this);
                    this.Calibration12.Location = new Point(0, 70);
                    this.Controls.Add(this.Calibration12);
                    break;
                default:
                    break;
            }

            this.UpdateDisplayHelpInit();
        }

        public void UpdateDisplayHelpInit()
        {
            this.listBoxHelp.ClearAll();

            switch (this.ParentForm.SystemConfig.Language)
            {
                case DataStore.E_LanguageID.Korean:
                    this.listBoxHelp.AddItem("중량조정");
                    this.listBoxHelp.AddItem("LANE을 선택 후");
                    this.listBoxHelp.AddItem("시작을 누르세요");
                    break;
                case DataStore.E_LanguageID.English:
                    this.listBoxHelp.AddItem("Calibration");
                    this.listBoxHelp.AddItem("After selecting lane, ");
                    this.listBoxHelp.AddItem("push the [Start] button.");
                    break;
                case DataStore.E_LanguageID.Chinese:
                    this.listBoxHelp.AddItem("校准");
                    this.listBoxHelp.AddItem("选择线后，按[开始]按钮。");
                    break;
                case DataStore.E_LanguageID.Czech:
                    this.listBoxHelp.AddItem("Kalibrace");
                    this.listBoxHelp.AddItem("Po výběru řádku, ");
                    this.listBoxHelp.AddItem("stiskněte tlačítko [Start].");
                    break;
                case DataStore.E_LanguageID.German:
                    this.listBoxHelp.AddItem("Kalibrierung");
                    this.listBoxHelp.AddItem("Drücken Sie nach der Auswahl der");
                    this.listBoxHelp.AddItem("Zeile die Taste [Start].");
                    break;
                case DataStore.E_LanguageID.Japanese:
                    this.listBoxHelp.AddItem("較正");
                    this.listBoxHelp.AddItem("レㅡンを選択したら、");
                    this.listBoxHelp.AddItem("［スタㅡト］ボタンを押します。");
                    break;
                default:
                    break;
            }
        }
        public void UpdateDisplayHelpBalance()
        {
            this.listBoxHelp.ClearAll();

            switch (this.ParentForm.SystemConfig.Language)
            {
                case DataStore.E_LanguageID.Korean:
                    this.listBoxHelp.AddItem("분동 계산중 입니다");
                    this.listBoxHelp.AddItem("잠시만 기다리세요");
                    break;
                case DataStore.E_LanguageID.English:
                    this.listBoxHelp.AddItem("Caculating...");
                    this.listBoxHelp.AddItem("Wait a minute.");
                    break;
                case DataStore.E_LanguageID.Chinese:
                    this.listBoxHelp.AddItem("计算...");
                    this.listBoxHelp.AddItem("请稍等");
                    break;
                case DataStore.E_LanguageID.Czech:
                    this.listBoxHelp.AddItem("Kontroluji hmotnost...");
                    this.listBoxHelp.AddItem("čekejte prosím.");
                    break;
                case DataStore.E_LanguageID.German:
                    this.listBoxHelp.AddItem("Gewichtsüberprüfung,");
                    this.listBoxHelp.AddItem("bitte warten");
                    break;
                case DataStore.E_LanguageID.Japanese:
                    this.listBoxHelp.AddItem("重量をチェック。");
                    this.listBoxHelp.AddItem("待機する");
                    break;
                default:
                    break;
            }
        }
        public void UpdateDisplayHelpStart()
        {
            this.listBoxHelp.ClearAll();

            switch (this.ParentForm.SystemConfig.Language)
            {
                case DataStore.E_LanguageID.Korean:
                    this.listBoxHelp.AddItem("중량조정을 시작합니다.");
                    this.listBoxHelp.AddItem("잠시만 기다리세요.");
                    break;
                case DataStore.E_LanguageID.English:
                    this.listBoxHelp.AddItem("Calibration start.");
                    this.listBoxHelp.AddItem("Wait a minute.");
                    break;
                case DataStore.E_LanguageID.Chinese:
                    this.listBoxHelp.AddItem("校准开始按钮");
                    this.listBoxHelp.AddItem("请稍等");
                    break;
                case DataStore.E_LanguageID.Czech:
                    this.listBoxHelp.AddItem("START kalibrace");
                    this.listBoxHelp.AddItem("čekejte prosím.");
                    break;
                case DataStore.E_LanguageID.German:
                    this.listBoxHelp.AddItem("Kalibrierungsstart");
                    this.listBoxHelp.AddItem("bitte warten");
                    break;
                case DataStore.E_LanguageID.Japanese:
                    this.listBoxHelp.AddItem("校正開始");
                    this.listBoxHelp.AddItem("待機する");
                    break;
                default:
                    break;
            }
        }
        public void UpdateDisplayHelpCancel()
        {
            this.listBoxHelp.ClearAll();

            switch (this.ParentForm.SystemConfig.Language)
            {
                case DataStore.E_LanguageID.Korean:
                    this.listBoxHelp.AddItem("중량조정을 취소합니다");
                    break;
                case DataStore.E_LanguageID.English:
                    this.listBoxHelp.AddItem("Cancel the Calibration");
                    break;
                case DataStore.E_LanguageID.Chinese:
                    this.listBoxHelp.AddItem("取消校准");
                    break;
                case DataStore.E_LanguageID.Czech:
                    this.listBoxHelp.AddItem("Zrušení Kalibrace");
                    break;
                case DataStore.E_LanguageID.German:
                    this.listBoxHelp.AddItem("Brechen Sie die ");
                    this.listBoxHelp.AddItem("Kalibrierung ab");
                    break;
                case DataStore.E_LanguageID.Japanese:
                    this.listBoxHelp.AddItem("校正をキャンセルする");
                    break;
                default:
                    break;
            }
        }
        public void UpdateDisplayHelpCalStandby()
        {
            this.listBoxHelp.ClearAll();

            switch (this.ParentForm.SystemConfig.Language)
            {
                case DataStore.E_LanguageID.Korean:
                    this.listBoxHelp.AddItem("분동을 올려주세요.");
                    break;
                case DataStore.E_LanguageID.English:
                    this.listBoxHelp.AddItem("Put the balance weight.");
                    break;
                case DataStore.E_LanguageID.Chinese:
                    this.listBoxHelp.AddItem("放平衡重");
                    break;
                case DataStore.E_LanguageID.Czech:
                    this.listBoxHelp.AddItem("Položit vyvážení hmotnosti.");
                    break;
                case DataStore.E_LanguageID.German:
                    this.listBoxHelp.AddItem("Gewicht auf den Förderer legen");
                    break;
                case DataStore.E_LanguageID.Japanese:
                    this.listBoxHelp.AddItem("バランスウェイトを置く。");
                    break;
                default:
                    break;
            }
        }
        public void UpdateDisplayHelpCalFinish()
        {
            this.listBoxHelp.ClearAll();

            switch (this.ParentForm.SystemConfig.Language)
            {
                case DataStore.E_LanguageID.Korean:
                    this.listBoxHelp.AddItem("중량조정이 완료 되었습니다.");
                    break;
                case DataStore.E_LanguageID.English:
                    this.listBoxHelp.AddItem("Finished.");
                    break;
                case DataStore.E_LanguageID.Chinese:
                    this.listBoxHelp.AddItem("完");
                    break;
                case DataStore.E_LanguageID.Czech:
                    this.listBoxHelp.AddItem("Ukončený.");
                    break;
                case DataStore.E_LanguageID.German:
                    this.listBoxHelp.AddItem("Die Kalibrierung ist abgeschlossen.");
                    break;
                case DataStore.E_LanguageID.Japanese:
                    this.listBoxHelp.AddItem("校正終了");
                    break;
                default:
                    break;
            }
        }
        public void UpdateDisplayHelpCalError()
        {
            this.listBoxHelp.ClearAll();

            switch (this.ParentForm.SystemConfig.Language)
            {
                case DataStore.E_LanguageID.Korean:
                    this.listBoxHelp.AddItem("중량조정 에러");
                    this.listBoxHelp.AddItem("다시 시도해 주세요");
                    break;
                case DataStore.E_LanguageID.English:
                    this.listBoxHelp.AddItem("Calibration error.");
                    this.listBoxHelp.AddItem("Please try again.");
                    break;
                case DataStore.E_LanguageID.Chinese:
                    this.listBoxHelp.AddItem("错误发生，重试");
                    break;
                case DataStore.E_LanguageID.Czech:
                    this.listBoxHelp.AddItem("Kalibrace chyba.");
                    this.listBoxHelp.AddItem("Zkuste znovu.");
                    break;
                case DataStore.E_LanguageID.German:
                    this.listBoxHelp.AddItem("Fehler!");
                    this.listBoxHelp.AddItem("Bitte noch einmal versuchen");
                    break;
                case DataStore.E_LanguageID.Japanese:
                    this.listBoxHelp.AddItem("校正エラㅡ");
                    this.listBoxHelp.AddItem("もうㅡ度試してください");
                    break;
                default:
                    break;
            }
        }
        public void UpdateDisplayHelpRemoveBalance()
        {
            this.listBoxHelp.ClearAll();

            switch (this.ParentForm.SystemConfig.Language)
            {
                case DataStore.E_LanguageID.Korean:
                    this.listBoxHelp.AddItem("분동을 제거해 주세요");
                    break;
                case DataStore.E_LanguageID.English:
                    this.listBoxHelp.AddItem("Remove the balance");
                    break;
                case DataStore.E_LanguageID.Chinese:
                    this.listBoxHelp.AddItem("移除平衡");
                    break;
                case DataStore.E_LanguageID.Czech:
                    this.listBoxHelp.AddItem("Odstranění vyvážení");
                    break;
                case DataStore.E_LanguageID.German:
                    this.listBoxHelp.AddItem("Balance entfernen");
                    break;
                case DataStore.E_LanguageID.Japanese:
                    this.listBoxHelp.AddItem("バランスを外す");
                    break;
                default:
                    break;
            }
        }

        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 UpdateWeightDisplay(DataStore.EquipmentStatus status, Collection<WeightData> weightDatas)
        {
            switch (this.ParentForm.SystemConfig.EquipmentColumns)
            {
                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;
                case 12:
                    this.Calibration12.UpdateWeightDisplay(status, weightDatas);
                    break;
                default:
                    break;
            }
        }

        public void DisplayRefresh()
        {
            this.ParentForm.SystemConfig.CurrentForm = DataStore.FormStore.FormCalibration;
            this.ParentForm.TransferData(CommunicationCommand.ModeCalibration, CommunicationID.MainBoard);

            this.buttonExtanalCAL.ButtonUp();
            this.IsBasicMode = true;

            switch (this.ParentForm.SystemConfig.EquipmentColumns)
            {
                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;
                case 12:
                    if (this.Calibration12 != null)
                        this.Calibration12.DisplayRefresh();
                    break;
                default:
                    break;
            }

            this.CalibrationButtonEnable(false, false, false);
            this.UpdateDisplayHelpInit();
        }
        #endregion

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

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

        #region Event Handler
        private void buttonStart_Click(object sender, EventArgs e)
        {
            switch (this.ParentForm.SystemConfig.EquipmentColumns)
            {
                case 6:
                    this.Calibration6.CalibrationStart(this.IsBasicMode);
                    break;
                case 7:
                    this.Calibration7.CalibrationStart(this.IsBasicMode);
                    break;
                case 8:
                    this.Calibration8.CalibrationStart(this.IsBasicMode);
                    break;
                case 10:
                    this.Calibration10.CalibrationStart(this.IsBasicMode);
                    break;
                case 12:
                    this.Calibration12.CalibrationStart(this.IsBasicMode);
                    break;
                default:
                    break;
            }
        }
        private void buttonBalance_Click(object sender, EventArgs e)
        {
            switch (this.ParentForm.SystemConfig.EquipmentColumns)
            {
                case 6:
                    this.Calibration6.CalibrationBalance();
                    break;
                case 7:
                    this.Calibration7.CalibrationBalance();
                    break;
                case 8:
                    this.Calibration8.CalibrationBalance();
                    break;
                case 10:
                    this.Calibration10.CalibrationBalance();
                    break;
                case 12:
                    this.Calibration12.CalibrationBalance();
                    break;
                default:
                    break;
            }
        }
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            switch (this.ParentForm.SystemConfig.EquipmentColumns)
            {
                case 6:
                    this.Calibration6.CalibrationCancel();
                    break;
                case 7:
                    this.Calibration7.CalibrationCancel();
                    break;
                case 8:
                    this.Calibration8.CalibrationCancel();
                    break;
                case 10:
                    this.Calibration10.CalibrationCancel();
                    break;
                case 12:
                    this.Calibration12.CalibrationCancel();
                    break;
                default:
                    break;
            }
        }
        private void buttonBack_Click(object sender, EventArgs e)
        {
            switch (this.ParentForm.SystemConfig.EquipmentColumns)
            {
                case 6:
                    this.Calibration6.CalibrationCancel();
                    break;
                case 7:
                    this.Calibration7.CalibrationCancel();
                    break;
                case 8:
                    this.Calibration8.CalibrationCancel();
                    break;
                case 10:
                    this.Calibration10.CalibrationCancel();
                    break;
                case 12:
                    this.Calibration12.CalibrationCancel();
                    break;
                default:
                    break;
            }

            this.ParentForm.ChildFormMenu.DisplayRefresh();
            ((FormMain)(Owner)).smartForm.Show((int)DataStore.FormStore.FormMenu);
        }
        private void buttonExtanalCAL_Click(object sender, EventArgs e)
        {
            if (this.buttonExtanalCAL.ButtonStatus == SmartX.SmartButton.BUTSTATUS.DOWN)
            {
                this.IsBasicMode = false;

                switch (this.ParentForm.SystemConfig.Language)
                {
                    case DataStore.E_LanguageID.Korean:
                        this.groupBoxButton.Text = "교정모드";
                        break;
                    case DataStore.E_LanguageID.English:
                        this.groupBoxButton.Text = "Correction Mode";
                        break;
                    case DataStore.E_LanguageID.German:
                        this.groupBoxButton.Text = "Korrekturmodus";
                        break;
                    default:
                        break;
                }
            }
            else
            {
                this.IsBasicMode = true;
                this.groupBoxButton.Text = "";
            }
        }
        private void buttonZero_Click(object sender, EventArgs e)
        {
            this.ParentForm.TransferData(CommunicationCommand.Zero, CommunicationID.SubBoardAll);

            // SetTrackingHistoryData
            this.ParentForm.SetTrackingHistoryData(DataStore.E_TrackingOperation.Zero, "");
        }
        #endregion
    }
}