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_2H_ImageDll;
using ITC81DB_2H.DialogForms;
using ITC81DB_2H.Forms;
using ITC81DB_2H.Datastore;

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

        #region Constructor
        public ControlConfiOptionBoard2(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.SystemConfig1.Language == Define.E_LanguageID.English)
            {
                this.labelTitleSetting.Text = "Settings";
                this.labelTitleDelayTime1.Font = this.labelTitleDelayTime2.Font = new Font("새굴림", 12, FontStyle.Bold);
                this.labelTitleDelayTime1.Text = "Delay 1 time";
                this.labelTitleDelayTime2.Text = "Delay 2 time";

                this.labelTitleHelp.Text = "Help";

                this.radioButtonPortNone.Text = "None";
                this.radioButtonPort56.Text = "Output port 5, 6";
                this.radioButtonPort78.Text = "Output port 7, 8";

                this.labelTitleOutputPort.Text = "Output Port";
                this.labelTitleZeroZoneCycle.Text = "Zero Zone Cycle";
                this.labelTitleZeroZoneCount.Text = "Zero Zone Count";

                this.labelHelp1.Text = "1. A : The object enters.";
                this.labelHelp2.Text = "2. B : The object enters into the range of dispenser sensor.";
                this.labelHelp3.Text = "3. C : After 'Delay 1 time', stick 'Dispenser 1' out.";
                this.labelHelp4.Text = "4. D : At the same time, 'Dispenser 2' descend.";
                this.labelHelp5.Text = "5. After 'Delay 2 time', come back to A.";
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Chinese)
            {
                this.labelTitleSetting.Text = "设置";
                this.labelTitleDelayTime1.Font = this.labelTitleDelayTime2.Font = new Font("새굴림", 12, FontStyle.Bold);
                this.labelTitleDelayTime1.Text = "延迟1时间";
                this.labelTitleDelayTime2.Text = "延迟2时间";

                this.labelTitleHelp.Text = "帮助";

                this.radioButtonPortNone.Text = "不使用";
                this.radioButtonPort56.Text = "输出 5, 6";
                this.radioButtonPort78.Text = "输出 7, 8";

                this.labelTitleOutputPort.Text = "输出端口";
                this.labelTitleZeroZoneCycle.Text = "零执行周期";
                this.labelTitleZeroZoneCount.Text = "零游程次数";

                this.labelHelp1.Text = "1. A : 被测量物体进入";
                this.labelHelp2.Text = "2. B : 被测量物体进入感应器检测范围";
                this.labelHelp3.Text = "3. C : 延迟1时间后，分配器1上升";
                this.labelHelp4.Text = "4. D : 分配器1上升的同时，分配器2下降";
                this.labelHelp5.Text = "5. 延迟2时间后，重新回到A状态";
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Czech)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Russian)
            {
                this.labelTitleSetting.Text = "Настройки";
                this.labelTitleDelayTime1.Font = this.labelTitleDelayTime2.Font = new Font("새굴림", 10, FontStyle.Bold);
                this.labelTitleDelayTime1.Text = "Время задержки 1";
                this.labelTitleDelayTime2.Text = "Время задержки 2";

                this.labelTitleHelp.Text = "Помощь";

                this.radioButtonPortNone.Text = "Нет";
                this.radioButtonPort56.Text = "Порт выхода 5, 6";
                this.radioButtonPort78.Text = "Порт выхода 7, 8";

                this.labelTitleOutputPort.Text = "Выходной порт";
                this.labelTitleZeroZoneCycle.Text = "Нулевой цикл";
                this.labelTitleZeroZoneCount.Text = "Нулевой счет";

                this.labelHelp1.Text = "1. A : Входит измеряемый объект";
                this.labelHelp2.Text = "2. B : Объект входит в зону действия датчика дозатора";
                this.labelHelp3.Text = "3. C : После «Задержки 1» отключите «Вход дозатора1»";
                this.labelHelp4.Text = "4. D : В то же время, «Вход дозатора 2» опустится";
                this.labelHelp5.Text = "5. Через 2 часа вернитесь в состояние A";
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.German)
            {
                this.labelTitleSetting.Text = "Einstellungen";
                this.labelTitleDelayTime1.Font = this.labelTitleDelayTime2.Font = new Font("새굴림", 12, FontStyle.Bold);
                this.labelTitleDelayTime1.Text = "Vezögerung 1";
                this.labelTitleDelayTime2.Text = "Vezögerung 2";

                this.labelTitleHelp.Text = "Helfen";

                this.radioButtonPortNone.Text = "Keiner";
                this.radioButtonPort56.Text = "Ausgangsport 5, 6";
                this.radioButtonPort78.Text = "Ausgangsport 7, 8";

                this.labelTitleOutputPort.Text = "Ausgangsport";
                this.labelTitleZeroZoneCycle.Text = "Nullzonenzyklus";
                this.labelTitleZeroZoneCount.Text = "Nullzonenanzahl";

                this.labelHelp1.Text = "1. A : Das Objekt tritt ein.";
                this.labelHelp2.Text = "2. B : Das Objekt gelangt in den Bereich des Spenders sensors.";
                this.labelHelp3.Text = "3. C : Nach 'Verzögerung 1' stecken Sie 'Dispenser 1' heraus.";
                this.labelHelp4.Text = "4. D : Gleichzeitig senkt 'Dispenser 2' ab.";
                this.labelHelp5.Text = "5. Nach 2 Stunden Verspätung kehren wir wieder in den A-Zustand zurück.";
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Spanish)
            {

            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == Define.E_LanguageID.Spanish)
            {
                this.labelTitleSetting.Text = "Ajustes";
                this.labelTitleDelayTime1.Font = this.labelTitleDelayTime2.Font = new Font("새굴림", 10, FontStyle.Bold);
                this.labelTitleDelayTime1.Text = "1 Tiempo de retardo";
                this.labelTitleDelayTime2.Text = "2 Tiempo de retardo";

                this.labelTitleHelp.Text = "Ayuda";

                this.radioButtonPortNone.Text = "Ninguna";
                this.radioButtonPort56.Text = "Puerto de salida 5, 6";
                this.radioButtonPort78.Text = "Puerto de salida 7, 8";

                this.labelTitleOutputPort.Text = "Puerto de salida";
                this.labelTitleZeroZoneCycle.Text = "Ciclo zona cero";
                this.labelTitleZeroZoneCount.Text = "Recuento de cero";

                this.labelHelp1.Text = "1. A : El objeto entra.";
                this.labelHelp2.Text = "2. B : El objeto entra en el rango de sensor de dispensador.";
                this.labelHelp3.Text = "3. C : Después de 'Tiempo de retraso 1', Stick 'Dispenser 1' Out";
                this.labelHelp4.Text = "4. D : Al mismo tiempo, 'Dispenser 2' desciende.";
                this.labelHelp5.Text = "5. Después de 'retraso 2' tiempo, vuelve a un.";
            }
            else
            {
                this.labelTitleSetting.Text = "품목 별 설정";
                this.labelTitleDelayTime1.Font = this.labelTitleDelayTime2.Font = new Font("새굴림", 12, FontStyle.Bold);
                this.labelTitleDelayTime1.Text = "지연시간 1";
                this.labelTitleDelayTime2.Text = "지연시간 2";

                this.labelTitleHelp.Text = "도움말";

                this.radioButtonPortNone.Text = "None";
                this.radioButtonPort56.Text = "Output port 5, 6";
                this.radioButtonPort78.Text = "Output port 7, 8";

                this.labelTitleOutputPort.Text = "출력 Port";
                this.labelTitleZeroZoneCycle.Text = "영점실행주기";
                this.labelTitleZeroZoneCount.Text = "영점실행개수";

                this.labelHelp1.Text = "1. A : 피계량물이 진입한다.";
                this.labelHelp2.Text = "2. B : 피계량물이 Dispenser sensor 범위 안에 진입한다.";
                this.labelHelp3.Text = "3. C : '지연시간1'만큼 지난 후, Dispenser1이 올라온다.";
                this.labelHelp4.Text = "4. D : Dispenser1이 올라옴과 동시에 Dispenser2가 내려간다.";
                this.labelHelp5.Text = "5. '지연시간2'만큼 지난 후, 다시 A 상태로 돌아온다.";
            }
        }
        private void DefaultSetting()
        {
            this.UpdateCurrentProductDisplay(this.ParentForm.ParentForm.CurrentProductItem, this.ParentForm.ParentForm.CurrentSystemParameter1, this.ParentForm.ParentForm.CurrentSystemParameter2);
        }

        public void UpdateCurrentProductDisplay(ProductItem product, SystemParameter1 parameter1, SystemParameter2 parameter2)
        {
            string value = "";

            value = product.Number.ToString();
            if (this.buttonItemNo.Text != value)
                this.buttonItemNo.Text = value;

            value = parameter2.OPT2ZeroZoneCycle;
            if (this.labelZeroZoneCycle.Text != value)
                this.labelZeroZoneCycle.Text = value;

            value = parameter2.OPT2ZeroZoneCount;
            if (this.labelZeroZoneCount.Text != value)
                this.labelZeroZoneCount.Text = value;

            this.UpdateDelayTime(product);
            this.UpdateDisplay(parameter1, parameter2);
        }
        private void UpdateDelayTime(ProductItem product)
        {
            string sValue = "";

            sValue = product.DispenserDelayTime1;
            if (this.labelDelayTime1.Text != sValue)
                this.labelDelayTime1.Text = sValue;

            sValue = product.DispenserDelayTime2;
            if (this.labelDelayTime2.Text != sValue)
                this.labelDelayTime2.Text = sValue;
        }
        private void UpdateDisplay(SystemParameter1 parameter1, SystemParameter2 parameter2)
        {
            int iValue = 0;
            string sValue = "";

            try
            {
                iValue = int.Parse(parameter2.OPT2Port);
            }
            catch
            {
                iValue = 0;
            }
            this.radioButtonPortNone.Click -= new EventHandler(this.radioButtonPort_Click);
            this.radioButtonPort56.Click -= new EventHandler(this.radioButtonPort_Click);
            this.radioButtonPort78.Click -= new EventHandler(this.radioButtonPort_Click);

            if (iValue == 1)
            {
                this.radioButtonPortNone.Checked = false;
                this.radioButtonPort78.Checked = false;
                this.radioButtonPort56.Checked = true;
            }
            else if (iValue == 2)
            {
                this.radioButtonPortNone.Checked = false;
                this.radioButtonPort56.Checked = false;
                this.radioButtonPort78.Checked = true;
            }
            else
            {
                this.radioButtonPortNone.Checked = true;
                this.radioButtonPort56.Checked = false;
                this.radioButtonPort78.Checked = false;
            }
            this.radioButtonPortNone.Click += new EventHandler(this.radioButtonPort_Click);
            this.radioButtonPort56.Click += new EventHandler(this.radioButtonPort_Click);
            this.radioButtonPort78.Click += new EventHandler(this.radioButtonPort_Click);
        }

        public void DisplayRefresh(SystemStatus status)
        {
            this.ParentForm.ParentForm.CurrentSystemStatus.CurrentDisplay = Define.E_DisplayStore.ConfiOptionBoard2;

            // 로그
            if (this.ParentForm.ParentForm.IsCommunicationLogOpen == true)
                this.ParentForm.ParentForm.smartFileCommunicationLog.WriteString(string.Format("Current Product ({0:yyyy-MM-dd HH:mm:ss}): {1}", DateTime.Now, this.ParentForm.ParentForm.SystemConfig1.ProductNumber.ToString()));

            this.UpdateCurrentProductDisplay(this.ParentForm.ParentForm.CurrentProductItem, this.ParentForm.ParentForm.CurrentSystemParameter1, this.ParentForm.ParentForm.CurrentSystemParameter2);
        }
        #endregion

        #region Event Handler
        private void buttonItemNo_Click(object sender, EventArgs e)
        {
            string message = "";
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.buttonItemNo.Text, 4, 0, false, this.ParentForm.ParentForm.SystemConfig1.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue < 1 || myKeyPad.doubleValue > this.ParentForm.ParentForm.ProductCount)
                {
                    // 입력범위를 확인하세요
                    message = string.Format("1 ~ {0}", this.ParentForm.ParentForm.ProductCount);
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig1.Language, message);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.buttonItemNo.Text = myKeyPad.StringValue;

                    this.buttonItemNo.Text = "**";
                    this.ParentForm.ParentForm.TransferProductParameter(myKeyPad.IntValue);
                }
            }
        }

        private void labelDelayTime1_Click(object sender, EventArgs e)
        {
            string value = "";
            string before = "", after = "";

            before = this.ParentForm.ParentForm.CurrentProductItem.DispenserDelayTime1;

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelDelayTime1.Text, 4, 0, false, this.ParentForm.ParentForm.SystemConfig1.Language);

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

                    value = Helper.StringZeroFillDigits4(this.labelDelayTime1.Text);
                    after = this.ParentForm.ParentForm.CurrentProductItem.DispenserDelayTime1 = myKeyPad.StringValue;

                    this.ParentForm.ParentForm.SaveProductFile(this.ParentForm.ParentForm.CurrentProductItem, this.ParentForm.ParentForm.SystemConfig1.ProductNumber - 1);

                    if (before != after)
                        this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.OPT2Delay1, "", before, after);
                }
            }
        }
        private void labelDelayTime2_Click(object sender, EventArgs e)
        {
            string value = "";
            string before = "", after = "";

            before = this.ParentForm.ParentForm.CurrentProductItem.DispenserDelayTime2;

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelDelayTime2.Text, 4, 0, false, this.ParentForm.ParentForm.SystemConfig1.Language);

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

                    value = Helper.StringZeroFillDigits4(this.labelDelayTime2.Text);
                    after = this.ParentForm.ParentForm.CurrentProductItem.DispenserDelayTime2 = myKeyPad.StringValue;

                    this.ParentForm.ParentForm.SaveProductFile(this.ParentForm.ParentForm.CurrentProductItem, this.ParentForm.ParentForm.SystemConfig1.ProductNumber - 1);

                    if (before != after)
                        this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.OPT2Delay2, "", before, after);
                }
            }
        }

        private void radioButtonPort_Click(object sender, EventArgs e)
        {
            string value = "";
            string before = "", after = "";

            before = this.ParentForm.ParentForm.CurrentSystemParameter2.OPT2Port;

            this.radioButtonPortNone.Click -= new EventHandler(this.radioButtonPort_Click);
            this.radioButtonPort56.Click -= new EventHandler(this.radioButtonPort_Click);
            this.radioButtonPort78.Click -= new EventHandler(this.radioButtonPort_Click);
            if (this.radioButtonPortNone.Checked == true)
            {
                value = "0000";
                this.radioButtonPort56.Checked = false;
                this.radioButtonPort78.Checked = false;
            }
            else if (this.radioButtonPort56.Checked == true)
            {
                value = "0001";
                this.radioButtonPortNone.Checked = false;
                this.radioButtonPort78.Checked = false;
            }
            else
            {
                value = "0002";
                this.radioButtonPortNone.Checked = false;
                this.radioButtonPort56.Checked = false;
            }
            this.radioButtonPortNone.Click += new EventHandler(this.radioButtonPort_Click);
            this.radioButtonPort56.Click += new EventHandler(this.radioButtonPort_Click);
            this.radioButtonPort78.Click += new EventHandler(this.radioButtonPort_Click);

            after = this.ParentForm.ParentForm.CurrentSystemParameter2.OPT2Port = value;
            this.ParentForm.ParentForm.SaveSystemParameter2File(this.ParentForm.ParentForm.CurrentSystemParameter2);
            this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.OPT2Port, value);

            if (before != after)
                this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.OPT2Port, "", before, after);
        }

        private void labelZeroZoneCycle_Click(object sender, EventArgs e)
        {
            string value = "";
            string before = "", after = "";

            before = this.ParentForm.ParentForm.CurrentSystemParameter2.OPT2ZeroZoneCycle;

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelZeroZoneCycle.Text, 4, 0, false, this.ParentForm.ParentForm.SystemConfig1.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.IntValue == 0 || (myKeyPad.IntValue >= 10 && myKeyPad.IntValue <= 9999))
                {
                    this.labelZeroZoneCycle.Text = myKeyPad.StringValue;

                    value = Helper.StringZeroFillDigits4(this.labelZeroZoneCycle.Text);
                    after = this.ParentForm.ParentForm.CurrentSystemParameter2.OPT2ZeroZoneCycle = myKeyPad.StringValue;
                    this.ParentForm.ParentForm.SaveSystemParameter2File(this.ParentForm.ParentForm.CurrentSystemParameter2);
                    this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.SubBoardAll, CommunicationAddress.OPT2ZeroZoneCycle, value);

                    if (before != after)
                        this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.OPT2ZeroCycle, "", before, after);
                }
                else
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig1.Language);
                    myMsg.ShowDialog();
                }
            }
        }
        private void labelZeroZoneCount_Click(object sender, EventArgs e)
        {
            string value = "";
            string before = "", after = "";

            before = this.ParentForm.ParentForm.CurrentSystemParameter2.OPT2ZeroZoneCount;

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelZeroZoneCount.Text, 4, 0, false, this.ParentForm.ParentForm.SystemConfig1.Language);

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

                    value = Helper.StringZeroFillDigits4(this.labelZeroZoneCount.Text);
                    after = this.ParentForm.ParentForm.CurrentSystemParameter2.OPT2ZeroZoneCount = myKeyPad.StringValue;
                    this.ParentForm.ParentForm.SaveSystemParameter2File(this.ParentForm.ParentForm.CurrentSystemParameter2);
                    this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.SubBoardAll, CommunicationAddress.OPT2ZeroZoneCount, value);

                    if (before != after)
                        this.ParentForm.ParentForm.SetTrackingHistoryData(Define.E_TrackingParameter.OPT2ZeroCount, "", before, after);
                }
            }
        }
        #endregion
    }
}
