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

using ITC81DB_2.Forms;
using ITC81DB_2_ImageDll;

namespace ITC81DB_2.Controls
{
    public partial class ControlCenterConfiCommunication : UserControl
    {
        #region Field
        private FormMenu m_ParentForm;
        #endregion

        #region Constructor
        public ControlCenterConfiCommunication(FormMenu parent)
        {
            InitializeComponent();

            this.ParentForm = parent;

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

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

        #region Method
        public void InitializeDesign()
        {
            Class1 images = new Class1();

            if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.English)
            {
                this.labelTitleSerialCom1.Text = "Serial";
                this.labelTitleSerialCom2.Text = "Comm";
                this.labelTitleChannel.Text = "Channel";
                this.labelTitleMode.Text = "Mode";

                this.labelDiscription2.Text = "- U : Under range, R : Reference, O : Over range";
                this.labelDiscription3.Text = "- G, Grd : Grade, W : Weight, CNT : Count";
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Chinese)
            {
                this.labelTitleSerialCom1.Text = "串行";
                this.labelTitleSerialCom2.Text = "端口";
                this.labelTitleChannel.Text = "渠道";
                this.labelTitleMode.Text = "渠道";

                this.labelDiscription2.Text = "- U : 下限, R : 标准, O : 上限";
                this.labelDiscription3.Text = "- G, Grd : 等级, W : 重量, CNT : 计数";
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Czech)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig.Language == Define.E_LanguageID.Spanish)
            {
                this.labelTitleSerialCom1.Text = "Serial";
                this.labelTitleSerialCom2.Text = "";
                this.labelTitleChannel.Text = "Canal";
                this.labelTitleMode.Text = "Modo";

                this.labelDiscription2.Text = "- U : Inferior, R : Referencia, O : Superior";
                this.labelDiscription3.Text = "- G, Grd : Calificación, W : Peso, CNT : Conteo";
            }
            else
            {
                this.labelTitleSerialCom1.Text = "시리얼";
                this.labelTitleSerialCom2.Text = "통신";
                this.labelTitleChannel.Text = "채널";
                this.labelTitleMode.Text = "모드";

                this.labelDiscription2.Text = "- U : Under range, R : Reference, O : Over range";
                this.labelDiscription3.Text = "- G, Grd : Grade, W : Weight, CNT : Count";
            }
        }
        private void DefaultSetting()
        {
            this.comboBoxCOM1BaudRate.Items.Clear();
            this.comboBoxCOM1BaudRate.SelectedIndexChanged -= new EventHandler(this.comboBoxSerial1BaudRate_SelectedIndexChanged);
            this.comboBoxCOM1BaudRate.Items.Add(9600);
            this.comboBoxCOM1BaudRate.Items.Add(19200);
            this.comboBoxCOM1BaudRate.Items.Add(38400);
            this.comboBoxCOM1BaudRate.Items.Add(115200);
            this.comboBoxCOM1BaudRate.SelectedIndex = 0;
            this.comboBoxCOM1BaudRate.SelectedIndexChanged += new EventHandler(this.comboBoxSerial1BaudRate_SelectedIndexChanged);

            this.comboBoxCOM1Mode.Items.Clear();
            this.comboBoxCOM1Mode.SelectedIndexChanged -= new EventHandler(this.comboBoxSerial1Mode_SelectedIndexChanged);
            this.comboBoxCOM1Mode.Items.Add("None");
            this.comboBoxCOM1Mode.Items.Add("STD1");
            this.comboBoxCOM1Mode.Items.Add("Remote");
            this.comboBoxCOM1Mode.Items.Add("Printer");
            this.comboBoxCOM1Mode.Items.Add("STD2");
            this.comboBoxCOM1Mode.SelectedIndex = 0;
            this.comboBoxCOM1Mode.SelectedIndexChanged += new EventHandler(this.comboBoxSerial1Mode_SelectedIndexChanged);

            this.comboBoxCOM3BaudRate.Items.Clear();
            this.comboBoxCOM3BaudRate.SelectedIndexChanged -= new EventHandler(this.comboBoxSerial3BaudRate_SelectedIndexChanged);
            this.comboBoxCOM3BaudRate.Items.Add(9600);
            this.comboBoxCOM3BaudRate.Items.Add(19200);
            this.comboBoxCOM3BaudRate.Items.Add(38400);
            this.comboBoxCOM3BaudRate.Items.Add(115200);
            this.comboBoxCOM3BaudRate.SelectedIndex = 0;
            this.comboBoxCOM3BaudRate.SelectedIndexChanged += new EventHandler(this.comboBoxSerial3BaudRate_SelectedIndexChanged);

            this.comboBoxCOM3Mode.Items.Clear();
            this.comboBoxCOM3Mode.SelectedIndexChanged -= new EventHandler(this.comboBoxSerial3Mode_SelectedIndexChanged);
            this.comboBoxCOM3Mode.Items.Add("None");
            this.comboBoxCOM3Mode.Items.Add("STD1");
            this.comboBoxCOM3Mode.Items.Add("Remote");
            this.comboBoxCOM3Mode.Items.Add("Printer");
            this.comboBoxCOM3Mode.Items.Add("STD2");
            this.comboBoxCOM3Mode.SelectedIndex = 0;
            this.comboBoxCOM3Mode.SelectedIndexChanged += new EventHandler(this.comboBoxSerial3Mode_SelectedIndexChanged);

            this.labelCh1Format.Text = "";
            this.labelCh2Format.Text = "";
        }

        private void UpdateLabelFormatDisplay()
        {
            this.UpdateLabelFormat1Display();
            this.UpdateLabelFormat2Display();
        }
        private void UpdateLabelFormat1Display()
        {
            switch(this.comboBoxCOM1Mode.SelectedIndex)
            {
                case 1: // STD1
                    this.labelCh1Format.Text = "STX[1],ID[5],No[4],Lane[1],U[6],R[6],O[6],G[1],W[6],ETX[1]";
                    break;
                case 2: // Remote
                    this.labelCh1Format.Text = "STX[1],No[2],Grd[1],Weight[6],ETX[1]";
                    break;
                case 3: // Printer
                    this.labelCh1Format.Text = "No[4],Grd[8],Weight[10],CNT[10]";
                    break;
                case 4:  // STD2
                    this.labelCh1Format.Text = "STX[1],ID[5],No[4],Lane[1],U[6],P[6],O[6],UC[7],PC[7],OC[7],ExC[7],TC[9],Grd[1],Sign[1],Weight[6],ETX[1]";
                    break;
                default:
                    break;
            }
        }
        private void UpdateLabelFormat2Display()
        {
            switch (this.comboBoxCOM3Mode.SelectedIndex)
            {
                case 1: // STD1
                    this.labelCh2Format.Text = "STX[1],ID[5],No[4],Lane[1],U[6],R[6],O[6],G[1],W[6],ETX[1]";
                    break;
                case 2: // Remote
                    this.labelCh2Format.Text = "STX[1],No[2],Grd[1],Weight[6],ETX[1]";
                    break;
                case 3: // Printer
                    this.labelCh2Format.Text = "No[4],Grd[8],Weight[10],CNT[10]";
                    break;
                case 4:  // STD2
                    this.labelCh2Format.Text = "STX[1],ID[5],No[4],Lane[1],U[6],P[6],O[6],UC[7],PC[7],OC[7],ExC[7],TC[9],Grd[1],Sign[1],Weight[6],ETX[1]";
                    break;
                default:
                    break;
            }
        }
        private void UpdateSerialParameterDisplay(SystemConfigurationItem item)
        {
            // CH1. BaudRate 
            if (this.comboBoxCOM1BaudRate.SelectedIndex != item.SerialCOM1BaudRate)
            {
                this.comboBoxCOM1BaudRate.SelectedIndexChanged -= new EventHandler(this.comboBoxSerial1BaudRate_SelectedIndexChanged);
                this.comboBoxCOM1BaudRate.SelectedIndex = item.SerialCOM1BaudRate;
                this.comboBoxCOM1BaudRate.SelectedIndexChanged += new EventHandler(this.comboBoxSerial1BaudRate_SelectedIndexChanged);
            }
            // CH1. Mode
            if (this.comboBoxCOM1Mode.SelectedIndex != item.SerialCOM1Mode)
            {
                this.comboBoxCOM1Mode.SelectedIndexChanged -= new EventHandler(this.comboBoxSerial1Mode_SelectedIndexChanged);
                this.comboBoxCOM1Mode.SelectedIndex = item.SerialCOM1Mode;
                this.comboBoxCOM1Mode.SelectedIndexChanged += new EventHandler(this.comboBoxSerial1Mode_SelectedIndexChanged);
            }

            // CH2. BaudRate 
            if (this.comboBoxCOM3BaudRate.SelectedIndex != item.SerialCOM3BaudRate)
            {
                this.comboBoxCOM3BaudRate.SelectedIndexChanged -= new EventHandler(this.comboBoxSerial3BaudRate_SelectedIndexChanged);
                this.comboBoxCOM3BaudRate.SelectedIndex = item.SerialCOM3BaudRate;
                this.comboBoxCOM3BaudRate.SelectedIndexChanged += new EventHandler(this.comboBoxSerial3BaudRate_SelectedIndexChanged);
            }
            // CH2. Mode
            if (this.comboBoxCOM3Mode.SelectedIndex != item.SerialCOM3Mode)
            {
                this.comboBoxCOM3Mode.SelectedIndexChanged -= new EventHandler(this.comboBoxSerial3Mode_SelectedIndexChanged);
                this.comboBoxCOM3Mode.SelectedIndex = item.SerialCOM3Mode;
                this.comboBoxCOM3Mode.SelectedIndexChanged += new EventHandler(this.comboBoxSerial3Mode_SelectedIndexChanged);
            }

            this.UpdateLabelFormatDisplay();
        }

        public void DisplayRefresh(SystemStatus status)
        {
            this.ParentForm.ParentForm.CurrentSystemStatus.CurrentDisplay = Define.E_DisplayStore.ConfiCommunication;
            this.ParentForm.ParentForm.SetDisplayMode(Define.E_DisplayMode.Menu);
            this.ParentForm.DisplayTitleRoot(this.ParentForm.ParentForm.CurrentSystemStatus);

            this.UpdateSerialParameterDisplay(this.ParentForm.ParentForm.SystemConfig);
            this.buttonSave1.Enabled = false;
            this.buttonSave2.Enabled = false;
            this.Focus();
        }
        #endregion

        #region Event Handler
        private void comboBoxSerial1BaudRate_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.buttonSave1.Enabled = true;
        }
        private void comboBoxSerial1Mode_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.buttonSave1.Enabled = true;

            this.UpdateLabelFormat1Display();
        }

        private void comboBoxSerial3BaudRate_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.buttonSave2.Enabled = true;
        }
        private void comboBoxSerial3Mode_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.buttonSave2.Enabled = true;

            this.UpdateLabelFormat2Display();
        }

        private void buttonSave1_Click(object sender, EventArgs e)
        {
            if (this.ParentForm.ParentForm.SystemConfig.SerialCOM3Mode == 3 && this.comboBoxCOM1Mode.SelectedIndex == 3)
            {
                this.comboBoxCOM3Mode.SelectedIndexChanged -= new EventHandler(this.comboBoxSerial3Mode_SelectedIndexChanged);
                this.comboBoxCOM3Mode.SelectedIndex = 0;
                this.comboBoxCOM3Mode.SelectedIndexChanged += new EventHandler(this.comboBoxSerial3Mode_SelectedIndexChanged);

                this.ParentForm.ParentForm.SystemConfig.SerialCOM3Mode = this.comboBoxCOM3Mode.SelectedIndex;
                this.ParentForm.ParentForm.CloseSerialCOM3();
            }
            this.ParentForm.ParentForm.SystemConfig.SerialCOM1BaudRate = this.comboBoxCOM1BaudRate.SelectedIndex;
            this.ParentForm.ParentForm.SystemConfig.SerialCOM1Mode = this.comboBoxCOM1Mode.SelectedIndex;

            this.ParentForm.CenterBasicDataStatistics.DisplayPrintButton(this.ParentForm.ParentForm.SystemConfig);
            this.ParentForm.ParentForm.ChildFormMainDisplay.DisplayPrintButton(this.ParentForm.ParentForm.SystemConfig);

            this.ParentForm.ParentForm.SaveSystemConfigurationFile1(this.ParentForm.ParentForm.SystemConfig);

            this.ParentForm.ParentForm.OpenSerialCOM1();

            this.buttonSave1.Enabled = false;
        }
        private void buttonSave2_Click(object sender, EventArgs e)
        {
            if (this.ParentForm.ParentForm.SystemConfig.SerialCOM1Mode == 3 && this.comboBoxCOM3Mode.SelectedIndex == 3)
            {
                this.comboBoxCOM1Mode.SelectedIndexChanged -= new EventHandler(this.comboBoxSerial1Mode_SelectedIndexChanged);
                this.comboBoxCOM1Mode.SelectedIndex = 0;
                this.comboBoxCOM1Mode.SelectedIndexChanged += new EventHandler(this.comboBoxSerial1Mode_SelectedIndexChanged);

                this.ParentForm.ParentForm.SystemConfig.SerialCOM1Mode = this.comboBoxCOM1Mode.SelectedIndex;
                this.ParentForm.ParentForm.CloseSerialCOM1();
            }

            this.ParentForm.ParentForm.SystemConfig.SerialCOM3BaudRate = this.comboBoxCOM3BaudRate.SelectedIndex;
            this.ParentForm.ParentForm.SystemConfig.SerialCOM3Mode = this.comboBoxCOM3Mode.SelectedIndex;

            this.ParentForm.CenterBasicDataStatistics.DisplayPrintButton(this.ParentForm.ParentForm.SystemConfig);
            this.ParentForm.ParentForm.ChildFormMainDisplay.DisplayPrintButton(this.ParentForm.ParentForm.SystemConfig);

            this.ParentForm.ParentForm.SaveSystemConfigurationFile1(this.ParentForm.ParentForm.SystemConfig);

            this.ParentForm.ParentForm.OpenSerialCOM3();

            this.buttonSave2.Enabled = false;
        }

        private void buttonTestSend1_Click(object sender, EventArgs e)
        {
            string value = "";
            StringBuilder sb = new StringBuilder();

            if (this.ParentForm.ParentForm.SystemConfig.SerialCOM1Mode == 0)
                return;

            switch (this.comboBoxCOM1Mode.SelectedIndex)
            {
                case 1:
                    #region STD1
                    sb.Append((char)0x02);
                    sb.Append("IN001");
                    sb.Append("0001");
                    sb.Append("0100.0");
                    sb.Append("0200.0");
                    sb.Append("0300.0");
                    sb.Append("P");
                    sb.Append("0123.4");
                    sb.Append((char)0x03);
                    #endregion
                    break;
                case 2:
                    #region Remote
                    sb.Append((char)0x02);
                    sb.Append("01");
                    sb.Append("P");
                    sb.Append(Helper.StringToDecimalPlaces("1234", this.ParentForm.ParentForm.SystemConfig.DecimalPlaces));
                    sb.Append((char)0x03);
                    #endregion
                    break;
                case 3:
                    #region Printer
                    sb.Append(string.Format("{0, 4}", this.ParentForm.ParentForm.SystemConfig.ProductNumber));
                    sb.Append(string.Format("{0, 3}", 1));
                    sb.Append("   P");

                    value = Helper.DoubleToString(123.4, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    sb.Append(value.PadLeft(9, ' '));
                    value = 9999999999.ToString();
                    sb.Append(value.PadLeft(11, ' '));
                    sb.Append("\n");
                    #endregion
                    break;
                case 4:
                    #region STD2
                    sb.Append((char)0x02);
                    sb.Append("IN001");
                    sb.Append("0001");
                    sb.Append("1");
                    sb.Append(Helper.StringZeroFillDigits6(Helper.StringToDecimalPlaces("1000", this.ParentForm.ParentForm.SystemConfig.DecimalPlaces)));
                    sb.Append(Helper.StringZeroFillDigits6(Helper.StringToDecimalPlaces("2000", this.ParentForm.ParentForm.SystemConfig.DecimalPlaces)));
                    sb.Append(Helper.StringZeroFillDigits6(Helper.StringToDecimalPlaces("3000", this.ParentForm.ParentForm.SystemConfig.DecimalPlaces)));
                    sb.Append(string.Format("{0:0000000}", 11111));
                    sb.Append(string.Format("{0:0000000}", 22222));
                    sb.Append(string.Format("{0:0000000}", 33333));
                    sb.Append(string.Format("{0:0000000}", 11111));
                    sb.Append(string.Format("{0:000000000}", 77777));
                    sb.Append("P");
                    sb.Append(" ");
                    sb.Append(Helper.StringZeroFillDigits6(Helper.DoubleToString(123.4, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces)));
                    sb.Append((char)0x03);
                    #endregion
                    break;
                default:
                    break;
            }

            this.ParentForm.ParentForm.TransferSerialCOM1(sb.ToString());
        }
        private void buttonTestSend2_Click(object sender, EventArgs e)
        {
            string value = "";
            StringBuilder sb = new StringBuilder();

            if (this.ParentForm.ParentForm.SystemConfig.SerialCOM3Mode == 0)
                return;

            switch (this.comboBoxCOM3Mode.SelectedIndex)
            {
                case 1:
                    #region STD1
                    sb.Append((char)0x02);
                    sb.Append("IN001");
                    sb.Append("0001");
                    sb.Append("0100.0");
                    sb.Append("0200.0");
                    sb.Append("0300.0");
                    sb.Append("P");
                    sb.Append("0123.4");
                    sb.Append((char)0x03);
                    #endregion
                    break;
                case 2:
                    #region Remote
                    sb.Append((char)0x02);
                    sb.Append("01");
                    sb.Append("P");
                    sb.Append(Helper.StringToDecimalPlaces("1234", this.ParentForm.ParentForm.SystemConfig.DecimalPlaces));
                    sb.Append((char)0x03);
                    #endregion
                    break;
                case 3:
                     #region Printer
                    sb.Append(string.Format("{0, 4}", this.ParentForm.ParentForm.SystemConfig.ProductNumber));
                    sb.Append(string.Format("{0, 3}", 1));
                    sb.Append("    P");

                    value = Helper.DoubleToString(123.4, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces);
                    sb.Append(value.PadLeft(8, ' '));
                    value = 9999999999.ToString();
                    sb.Append(value.PadLeft(11, ' '));
                    #endregion
                    break;
                case 4:
                    #region STD2
                    sb.Append((char)0x02);
                    sb.Append("IN001");
                    sb.Append("0001");
                    sb.Append("1");
                    sb.Append(Helper.StringZeroFillDigits6(Helper.StringToDecimalPlaces("1000", this.ParentForm.ParentForm.SystemConfig.DecimalPlaces)));
                    sb.Append(Helper.StringZeroFillDigits6(Helper.StringToDecimalPlaces("2000", this.ParentForm.ParentForm.SystemConfig.DecimalPlaces)));
                    sb.Append(Helper.StringZeroFillDigits6(Helper.StringToDecimalPlaces("3000", this.ParentForm.ParentForm.SystemConfig.DecimalPlaces)));
                    sb.Append(string.Format("{0:0000000}", 11111));
                    sb.Append(string.Format("{0:0000000}", 22222));
                    sb.Append(string.Format("{0:0000000}", 33333));
                    sb.Append(string.Format("{0:0000000}", 11111));
                    sb.Append(string.Format("{0:000000000}", 77777));
                    sb.Append("P");
                    sb.Append(" ");
                    sb.Append(Helper.StringZeroFillDigits6(Helper.DoubleToString(123.4, this.ParentForm.ParentForm.SystemConfig.DecimalPlaces)));
                    sb.Append((char)0x03);
                    #endregion
                    break;
                default:
                    break;
            }
            this.ParentForm.ParentForm.TransferSerialCOM3(sb.ToString());
        }
        #endregion
    }
}
