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_0H_ImageDll;
using ITC81DB_0H.DialogForms;
using ITC81DB_0H.Forms;

namespace ITC81DB_0H.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 == DataStore.LanguageID.English)
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.engBackgroundDispenser));

                this.labelTitleDelayTime1.Font = this.labelTitleDelayTime2.Font = new Font("새굴림", 12, FontStyle.Bold);
                this.labelTitleDelayTime1.Text = "Delay 1 time";
                this.labelTitleDelayTime2.Text = "Delay 2 time";

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

                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 == DataStore.LanguageID.Chinese)
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.chnBackgroundDispenser));

                this.labelTitleDelayTime1.Font = this.labelTitleDelayTime2.Font = new Font("새굴림", 12, FontStyle.Bold);
                this.labelTitleDelayTime1.Text = "延迟1时间";
                this.labelTitleDelayTime2.Text = "延迟2时间";

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

                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 == DataStore.LanguageID.Czech)
            {
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Russian)
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.rusBackgroundDispenser));

                this.labelTitleDelayTime1.Font = this.labelTitleDelayTime2.Font = new Font("새굴림", 10, FontStyle.Bold);
                this.labelTitleDelayTime1.Text = "Время задержки 1";
                this.labelTitleDelayTime2.Text = "Время задержки 2";

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

                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 == DataStore.LanguageID.German)
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.gerBackgroundDispenser));

                this.labelTitleDelayTime1.Font = this.labelTitleDelayTime2.Font = new Font("새굴림", 12, FontStyle.Bold);
                this.labelTitleDelayTime1.Text = "Vezögerung 1";
                this.labelTitleDelayTime2.Text = "Vezögerung 2";

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

                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
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.korBackgroundDispenser));

                this.labelTitleDelayTime1.Font = this.labelTitleDelayTime2.Font = new Font("새굴림", 12, FontStyle.Bold);
                this.labelTitleDelayTime1.Text = "지연시간 1";
                this.labelTitleDelayTime2.Text = "지연시간 2";

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

                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.UpdateDisplay(this.ParentForm.ParentForm.CurrentSystemParameter2);
        }

        private void UpdateDisplay(SystemParameter2 parameter)
        {
            int iValue = 0;
            string sValue = "";

            try
            {
                iValue = int.Parse(parameter.OPT2Port);
            }
            catch 
            {
                iValue = 0;
            }
            if (iValue == 1)
            {
                this.radioButtonPortNone.Checked = false;
                this.radioButtonPort56.Checked = true;
                this.radioButtonPort78.Checked = false;
            }
            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;
            }

            sValue = parameter.OPT2DelayTime1;
            if (this.labelDelayTime1.Text != sValue)
                this.labelDelayTime1.Text = sValue;

            sValue = parameter.OPT2DelayTime2;
            if (this.labelDelayTime2.Text != sValue)
                this.labelDelayTime2.Text = sValue;
        }

        public void DisplayRefresh(SystemStatus status)
        {
            this.UpdateDisplay(this.ParentForm.ParentForm.CurrentSystemParameter2);
        }
        #endregion

        #region Event Handler
        private void labelDelayTime1_Click(object sender, EventArgs e)
        {
            string value = "";
            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);
                    this.ParentForm.ParentForm.CurrentSystemParameter2.OPT2DelayTime1 = myKeyPad.StringValue;

                    this.ParentForm.ParentForm.SaveSystemParameter2File(this.ParentForm.ParentForm.CurrentSystemParameter2);
                    this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.OPT2DelayTime1, value);
                }
            }
        }

        private void labelDelayTime2_Click(object sender, EventArgs e)
        {
            string value = "";
            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);
                    this.ParentForm.ParentForm.CurrentSystemParameter2.OPT2DelayTime2 = myKeyPad.StringValue;

                    this.ParentForm.ParentForm.SaveSystemParameter2File(this.ParentForm.ParentForm.CurrentSystemParameter2);
                    this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.OPT2DelayTime2, value);
                }
            }
        }

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

            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.ParentForm.ParentForm.CurrentSystemParameter2.OPT2Port = value;
            this.ParentForm.ParentForm.SaveSystemParameter2File(this.ParentForm.ParentForm.CurrentSystemParameter2);
            this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.OPT2Port, value);
        }
        #endregion
    }
}