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.Forms;
using ITC81DB_ImageDll;
using ITC81DB.DialogForms;

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

        #region Constructor
        public ControlBasicProductLotSetting(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.labelTitleReceiveData.Text = "Receive Data(COM3) : ";
                this.labelTitleExtractData.Text = "Extract Data : ";
                this.labelTitleDigit.Text = "(up to 14 digits)";

                this.buttonRemove.Text = "Remove";
                this.buttonCheck.Text = "Check";
                this.buttonSave.Text = "Save";
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Chinese)
            {
                this.labelTitleReceiveData.Text = "接收数据(COM3) : ";
                this.labelTitleExtractData.Text = "数据提取 : ";
                this.labelTitleDigit.Text = "(最多 14 位数字)";

                this.buttonRemove.Text = "消除";
                this.buttonCheck.Text = "查看";
                this.buttonSave.Text = "节省";
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Czech)
            {

            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Russian)
            {
                this.labelTitleReceiveData.Text = "Получить данные(COM3) : ";
                this.labelTitleExtractData.Text = "Извлечь данные : ";
                this.labelTitleDigit.Text = "(до 14 цифр)";

                this.buttonRemove.Text = "Удалять";
                this.buttonCheck.Text = "Проверять";
                this.buttonSave.Text = "Сохранять";
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.German)
            {
                this.labelTitleReceiveData.Text = "Empfange Daten(COM3) : ";
                this.labelTitleExtractData.Text = "Extrahieren Daten : ";
                this.labelTitleDigit.Text = "(bis zu 14 Stellen)";

                this.buttonRemove.Text = "Entfernen";
                this.buttonCheck.Text = "Prüfen";
                this.buttonSave.Text = "Speichern";
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Spanish)
            {
                this.labelTitleReceiveData.Text = "Recibir datos(COM3) : ";
                this.labelTitleExtractData.Text = "Extraer datos : ";
                this.labelTitleDigit.Text = "(hasta 14 dígitos)";

                this.buttonRemove.Text = "Remover";
                this.buttonCheck.Text = "Comprobar";
                this.buttonSave.Text = "Ahorrar";
            }
            else
            {
                this.labelTitleReceiveData.Text = "수신 데이터(COM3) : ";
                this.labelTitleExtractData.Text = "추출 데이터 : ";
                this.labelTitleDigit.Text = "(최대 14자리)";

                this.buttonRemove.Text = "삭제";
                this.buttonCheck.Text = "확인";
                this.buttonSave.Text = "저장";
            }
        }
        private void DefaultSetting()
        {
        }

        public void DisplayReceive(string value)
        {
            value = value.Trim();

            this.labelReceiveData.Text = value;

            if (value.Length <= 14 && value.Trim() != "NoRead")
            {
                this.labelExtractDataFrom.Text = "1";
                this.labelExtractDataTo.Text = value.Length.ToString();

                this.ParentForm.ParentForm.SystemConfig3.LOTExtractDataFromIndex = 0;
                this.ParentForm.ParentForm.SystemConfig3.LOTExtractDataToIndex = value.Length - 1;

                this.ParentForm.ParentForm.ChildFormMenu.CenterBasicProduct.SaveLOTToProductItem(value);

                this.ParentForm.ParentForm.SaveSystemConfigurationFile3(this.ParentForm.ParentForm.SystemConfig3);
            }
        }

        public void DisplayRefresh()
        {
            this.labelExtractDataFrom.Text = (this.ParentForm.ParentForm.SystemConfig3.LOTExtractDataFromIndex + 1).ToString();
            this.labelExtractDataTo.Text = (this.ParentForm.ParentForm.SystemConfig3.LOTExtractDataToIndex + 1).ToString();
            this.buttonSave.Enabled = false;
        }
        #endregion

        #region Event Handler
        private void buttonRemove_Click(object sender, EventArgs e)
        {
            this.labelReceiveData.Text = "";
        }

        private void labelExtractDataFrom_Click(object sender, EventArgs e)
        {
            int to = int.Parse(this.labelExtractDataTo.Text);
            int receiveDataLength = this.labelReceiveData.Text.Length;
            int gap = to - int.Parse(this.labelExtractDataFrom.Text);

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

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                this.buttonSave.Enabled = false;

                if (myKeyPad.IntValue > to)
                {
                    if (receiveDataLength >= myKeyPad.IntValue)
                    {
                        this.labelExtractDataFrom.Text = myKeyPad.StringValue;
                        if (myKeyPad.IntValue + gap > receiveDataLength)
                            this.labelExtractDataTo.Text = receiveDataLength.ToString();
                        else
                            this.labelExtractDataTo.Text = (myKeyPad.IntValue + gap).ToString();
                    }
                    else
                    {
                        this.labelExtractDataFrom.Text = receiveDataLength.ToString();
                        this.labelExtractDataTo.Text = receiveDataLength.ToString();
                    }
                }
                else
                {
                    this.labelExtractDataFrom.Text = myKeyPad.StringValue;
                    gap = to - myKeyPad.IntValue;
                    if (gap > 13)
                        this.labelExtractDataTo.Text = (myKeyPad.IntValue + 13).ToString();
                    else
                    {
                        if (receiveDataLength < myKeyPad.IntValue + 13)
                            this.labelExtractDataTo.Text = receiveDataLength.ToString();
                    }
                }

                this.ParentForm.ParentForm.SystemConfig3.LOTExtractDataFromIndex = int.Parse(this.labelExtractDataFrom.Text) - 1;
                this.ParentForm.ParentForm.SystemConfig3.LOTExtractDataToIndex = int.Parse(this.labelExtractDataTo.Text) - 1;
                this.ParentForm.ParentForm.SaveSystemConfigurationFile3(this.ParentForm.ParentForm.SystemConfig3);
            }
        }
        private void labelExtractDataTo_Click(object sender, EventArgs e)
        {
            int from = int.Parse(this.labelExtractDataFrom.Text);
            int receiveDataLength = this.labelReceiveData.Text.Length;

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

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                this.buttonSave.Enabled = false;

                if (myKeyPad.IntValue < from)
                    this.labelExtractDataTo.Text = this.labelExtractDataFrom.Text;
                else
                {
                    if (myKeyPad.IntValue > receiveDataLength)
                        this.labelExtractDataTo.Text = receiveDataLength.ToString();
                    else
                        this.labelExtractDataTo.Text = myKeyPad.StringValue;

                    if (receiveDataLength > 13)
                    {
                        if(myKeyPad.IntValue - from > 13)
                            this.labelExtractDataFrom.Text = (int.Parse(this.labelExtractDataTo.Text) - 13).ToString();
                    }
                    else
                        this.labelExtractDataFrom.Text = "1";
                }

                this.ParentForm.ParentForm.SystemConfig3.LOTExtractDataFromIndex = int.Parse(this.labelExtractDataFrom.Text) - 1;
                this.ParentForm.ParentForm.SystemConfig3.LOTExtractDataToIndex = int.Parse(this.labelExtractDataTo.Text) - 1;
                this.ParentForm.ParentForm.SaveSystemConfigurationFile3(this.ParentForm.ParentForm.SystemConfig3);
            }
        }

        private void buttonCheck_Click(object sender, EventArgs e)
        {
            int receiveDataLength = this.labelReceiveData.Text.Length;
            int from = int.Parse(this.labelExtractDataFrom.Text);
            int to = int.Parse(this.labelExtractDataTo.Text);

            try
            {
                this.labelExtractData.Text = this.labelReceiveData.Text.Substring(from - 1, to - from + 1);
                this.buttonSave.Enabled = true;
            }
            catch
            {
                // 입력범위를 확인하세요
                DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig1.Language);
                myMsg.ShowDialog();
            }
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            this.ParentForm.ParentForm.ChildFormMenu.CenterBasicProduct.SaveLOTToProductItem(this.labelExtractData.Text);
        }
        #endregion
    }
}
