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 INT69DC_7C.Controls;
using INT69DC_7C.DialogForms;
using INT69DC_ImageDll;

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

        private ControlCalibration7 Calibration7;
        private ControlCalibration8 Calibration8;
        private ControlCalibration10 Calibration10;
        private ControlCalibration11 Calibration11;
        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();

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

            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.English)
            {
                this.labelTitle.Text = "Calibration";

                this.buttonStart.Text = "Start";
                this.buttonBalance.Text = "Balance";
                this.buttonCancel.Text = "Cancel";

                this.groupBoxSetting.Text = "Setting";
                this.groupBoxHelp.Text = "Help";
                this.labelStaticBalanceWeight.Text = "Balance weight";
                this.labelStaticMaxWeight.Text = "Max weight";
                this.labelStaticDigit.Text = "Digit";

                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));
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Chinese)
            {
                this.labelTitle.Text = "校准";

                this.buttonStart.Text = "开启";
                this.buttonBalance.Text = "砝码";
                this.buttonCancel.Text = "取消";

                this.groupBoxSetting.Text = "设定";
                this.groupBoxHelp.Text = "帮助";
                this.labelStaticBalanceWeight.Text = "砝码重量";
                this.labelStaticMaxWeight.Text = "最大重量";
                this.labelStaticDigit.Text = "数字";

                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));
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Czech)
            {
                this.labelTitle.Text = "Kalibrace";

                this.buttonStart.Text = "Start";
                this.buttonBalance.Text = "Váha";
                this.buttonCancel.Text = "Zrušit";

                this.groupBoxSetting.Text = "Seřízení";
                this.groupBoxHelp.Text = "Pomoc";
                this.labelStaticBalanceWeight.Text = "Vyvažovací závaží";
                this.labelStaticMaxWeight.Text = "Max. hmotnost";
                this.labelStaticDigit.Text = "Číslice";

                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));
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.German)
            {
                this.labelTitle.Text = "Kalibrierung";

                this.buttonStart.Text = "Start";
                this.buttonBalance.Text = "Gegengewicht";
                this.buttonCancel.Text = "Stornieren";

                this.groupBoxSetting.Text = "Einstellung";
                this.groupBoxHelp.Text = "Hilfe";
                this.labelStaticBalanceWeight.Text = "Gewicht der Waage";
                this.labelStaticMaxWeight.Text = "Maximalgewicht";
                this.labelStaticDigit.Text = "Nummer";

                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));
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Japanese)
            {
                this.labelTitle.Text = "キャリブレㅡション";

                this.buttonStart.Text = "スタㅡト";
                this.buttonBalance.Text = "分銅";
                this.buttonCancel.Text = "キャンセル";

                this.groupBoxSetting.Text = "設定";
                this.groupBoxHelp.Text = "ヘルプ";
                this.labelStaticBalanceWeight.Text = "バランス重量";
                this.labelStaticMaxWeight.Text = "最大重量";
                this.labelStaticDigit.Text = "目量";

                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));
            }
            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 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 11:
                    this.Calibration11 = new ControlCalibration11(this);
                    this.Calibration11.Location = new Point(0, 70);
                    this.Controls.Add(this.Calibration11);
                    break;
                case 12:
                    this.Calibration12 = new ControlCalibration12(this);
                    this.Calibration12.Location = new Point(0, 70);
                    this.Controls.Add(this.Calibration12);
                    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 selecting line, ");
                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.Czech)
            {
                this.listBoxHelp.Items.Add("Kalibrace");
                this.listBoxHelp.Items.Add("Po výběru řádku, ");
                this.listBoxHelp.Items.Add("stiskněte tlačítko [Start].");
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.German)
            {
                this.listBoxHelp.Items.Add("Kalibrierung");
                this.listBoxHelp.Items.Add("Drücken Sie nach der Auswahl der");
                this.listBoxHelp.Items.Add("Zeile die Taste [Start].");
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Japanese)
            {
                this.listBoxHelp.Items.Add("キャリブレㅡション");
                this.listBoxHelp.Items.Add("レㅡンを選択したら、");
                this.listBoxHelp.Items.Add("［スタㅡト］ボタンを押します。");
            }
            else
            {

            }
        }

        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 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 11:
                    if (this.Calibration11 != null)
                        this.Calibration11.DisplayRefresh();
                    break;
                case 12:
                    if (this.Calibration12 != null)
                        this.Calibration12.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 selecting line, ");
                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.Czech)
            {
                this.listBoxHelp.Items.Add("Kalibrace");
                this.listBoxHelp.Items.Add("Po výběru řádku, ");
                this.listBoxHelp.Items.Add("stiskněte tlačítko [Start].");
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.German)
            {
                this.listBoxHelp.Items.Add("Kalibrierung");
                this.listBoxHelp.Items.Add("Drücken Sie nach der Auswahl der");
                this.listBoxHelp.Items.Add("Zeile die Taste [Start].");
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Japanese)
            {
                this.listBoxHelp.Items.Add("キャリブレㅡション");
                this.listBoxHelp.Items.Add("レㅡンを選択したら、");
                this.listBoxHelp.Items.Add("［スタㅡト］ボタンを押します。");
            }
            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 7:
                    this.Calibration7.UpdateConfiguration(status, config);
                    break;
                case 8:
                    this.Calibration8.UpdateConfiguration(status, config);
                    break;
                case 10:
                    this.Calibration10.UpdateConfiguration(status, config);
                    break;
                case 11:
                    this.Calibration11.UpdateConfiguration(status, config);
                    break;
                case 12:
                    this.Calibration12.UpdateConfiguration(status, config);
                    break;
                default:
                    break;
            }
        }
        public void UpdateWeightDisplay(DataStore.EquipmentStatus status, Collection<WeightData> weightDatas)
        {
            switch (this.ParentForm.SystemConfig.EquipmentColumns)
            {
                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 11:
                    this.Calibration11.UpdateWeightDisplay(status, weightDatas);
                    break;
                case 12:
                    this.Calibration12.UpdateWeightDisplay(status, weightDatas);
                    break;
                default:
                    break;
            }
        }
        #endregion

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

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

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

            before = this.ParentForm.CurrentCalibrationItem.Digit;

            value = Helper.StringZeroFillDigits4(this.comboBoxDigit.SelectedItem.ToString());
            this.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.SubBoardAll, CommunicationAddress._3003_Digit, value);

            after = int.Parse(value).ToString();

            // Part 11
            if (this.ParentForm.SystemConfig.IsOptPart11 == true)
                this.ParentForm.SetTrackingHistoryData(DataStore.TrackingParameter.CalDigit, "", before, after);
        }
        private void labelBalanceWeight_Click(object sender, EventArgs e)
        {
            string value = "", before = "", after = "";

            before = this.labelBalanceWeight.Text;

            DialogFormNumKeyPad myKeyPad;

            if (this.ParentForm.SystemConfig.DecimalPlaces == 2)
                myKeyPad = new DialogFormNumKeyPad(this.labelBalanceWeight.Text, 5, this.ParentForm.SystemConfig.DecimalPlaces, false, this.ParentForm.SystemConfig.Language);
            else
                myKeyPad = new DialogFormNumKeyPad(this.labelBalanceWeight.Text, 4, this.ParentForm.SystemConfig.DecimalPlaces, false, this.ParentForm.SystemConfig.Language);

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

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

                    // Part 11
                    if (this.ParentForm.SystemConfig.IsOptPart11 == true)
                        this.ParentForm.SetTrackingHistoryData(DataStore.TrackingParameter.CalBalWeight, "", before, after);
                }
            }
        }
        private void labelMaxWeight_Click(object sender, EventArgs e)
        {
            string value = "", before = "", after = "";

            before = this.labelMaxWeight.Text;

            DialogFormNumKeyPad myKeyPad;

            if (this.ParentForm.SystemConfig.DecimalPlaces == 2)
                myKeyPad = new DialogFormNumKeyPad(this.labelMaxWeight.Text, 5, this.ParentForm.SystemConfig.DecimalPlaces, false, this.ParentForm.SystemConfig.Language);
            else
                myKeyPad = new DialogFormNumKeyPad(this.labelMaxWeight.Text, 4, this.ParentForm.SystemConfig.DecimalPlaces, false, this.ParentForm.SystemConfig.Language);

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

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

                    // Part 11
                    if (this.ParentForm.SystemConfig.IsOptPart11 == 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 7:
                    this.Calibration7.CalibrationStart();
                    break;
                case 8:
                    this.Calibration8.CalibrationStart();
                    break;
                case 10:
                    this.Calibration10.CalibrationStart();
                    break;
                case 11:
                    this.Calibration11.CalibrationStart();
                    break;
                case 12:
                    this.Calibration12.CalibrationStart();
                    break;
                default:
                    break;
            }
        }
        private void buttonBalance_Click(object sender, EventArgs e)
        {
            this.ClearListBox();

            switch (this.ParentForm.SystemConfig.EquipmentColumns)
            {
                case 7:
                    this.Calibration7.CalibrationBalance();
                    break;
                case 8:
                    this.Calibration8.CalibrationBalance();
                    break;
                case 10:
                    this.Calibration10.CalibrationBalance();
                    break;
                case 11:
                    this.Calibration11.CalibrationBalance();
                    break;
                case 12:
                    this.Calibration12.CalibrationBalance();
                    break;
                default:
                    break;
            }
        }
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.ClearListBox();

            switch (this.ParentForm.SystemConfig.EquipmentColumns)
            {
                case 7:
                    this.Calibration7.CalibrationCancel();
                    break;
                case 8:
                    this.Calibration8.CalibrationCancel();
                    break;
                case 10:
                    this.Calibration10.CalibrationCancel();
                    break;
                case 11:
                    this.Calibration11.CalibrationCancel();
                    break;
                case 12:
                    this.Calibration12.CalibrationCancel();
                    break;
                default:
                    break;
            }
        }
        private void buttonBack_Click(object sender, EventArgs e)
        {
            this.ClearListBox();

            switch (this.ParentForm.SystemConfig.EquipmentColumns)
            {
                case 7:
                    this.Calibration7.CalibrationCancel();
                    break;
                case 8:
                    this.Calibration8.CalibrationCancel();
                    break;
                case 10:
                    this.Calibration10.CalibrationCancel();
                    break;
                case 11:
                    this.Calibration11.CalibrationCancel();
                    break;
                case 12:
                    this.Calibration12.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_ParameterRead, "");
        }
        #endregion
    }
}