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 SmartX;

using ITC81DB.DialogForms;
using ITC81DB.Forms;
using ITC81DB_ImageDll;

namespace ITC81DB.Controls
{
    public partial class ControlCenterSystemJudgmentAuto : UserControl
    {
        #region Field
        private FormMenu m_ParentForm;        
        private DataStore.Step3 WizardStep;
        #endregion

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

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

            this.ParentForm = parent;
            this.InitializeDesign();
        }
        #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.engBackgroundJudgment_Auto));
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Chinese)
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.chnBackgroundJudgment_Auto));
            }
            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.rusBackgroundJudgment_Auto));
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.German)
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.gerBackgroundJudgment_Auto));
            }
            else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Spanish)
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.engBackgroundJudgment_Auto));
            }
            else
            {
                this.smartGroupBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.korBackgroundJudgment_Auto));
            }
        }
        private void ChangeStep(DataStore.Step3 step)
        {
            Class1 images = new Class1();

            this.WizardStep = step;

            switch (step)
            {
                case DataStore.Step3.Step1:
                    this.pictureBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.AutoJudgmentImage1));
                    if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.English)
                        this.labelHelp.Text = "After entering the speed (Hz), press the [→] button.";
                    else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Chinese)
                        this.labelHelp.Text = "输入速度(Hz)后，按[→]按钮。";
                    else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Czech)
                        this.labelHelp.Text = "Po zadání rychlosti(Hz) stiskněte tlačítko[→].";
                    else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Russian)
                        this.labelHelp.Text = "После ввода скорости(Hz) нажмите кнопку [→].";
                    else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.German)
                        this.labelHelp.Text = "Drücken Sie nach Eingabe der Geschwindigkeit(Hz) die Taste [→].";
                    else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Spanish)
                        this.labelHelp.Text = "Después de ingresar la velocidad (Hz), presione el botón [→].";
                    else
                        this.labelHelp.Text = "속도(Hz)를 입력한 후 [→] 버튼을 눌러주십시오.";

                    this.buttonNext.Enabled = true;
                    this.labelFeedSpeed.Enabled = true;
                    this.labelFeedSpeed.ForeColor = Color.White;
                    this.labelProductLength.Enabled = false;
                    this.labelProductLength.ForeColor = Color.Gray;
                    break;
                case DataStore.Step3.Step2:
                    this.pictureBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.AutoJudgmentImage2));
                    if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.English)
                        this.labelHelp.Text = "After entering the product length (mm), press the [→] button.";
                    else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Chinese)
                        this.labelHelp.Text = "输入产品长度(mm)后，按[→]按钮。";
                    else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Czech)
                        this.labelHelp.Text = "Po zadání délky produktu(mm) stiskněte tlačítko [→].";
                    else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Russian)
                        this.labelHelp.Text = "После ввода длины продукта (мм) нажмите кнопку [→].";
                    else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.German)
                        this.labelHelp.Text = "Drücken Sie nach Eingabe der Produktlänge(mm) die Taste [→].";
                    else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Spanish)
                        this.labelHelp.Text = "Después de ingresar la longitud del producto (mm), presione el botón [→].";
                    else
                        this.labelHelp.Text = "제품 길이(mm)를 입력한 후  [→] 버튼을 눌러주십시오.";

                    this.labelFeedSpeed.Enabled = false;
                    this.labelFeedSpeed.ForeColor = Color.Gray;
                    this.labelProductLength.Enabled = true;
                    this.labelProductLength.ForeColor = Color.White;
                    break;
                case DataStore.Step3.Step3:
                    this.pictureBox1.Image = new Bitmap(images.GetImage(Class1.ButtonImages.AutoJudgmentImage3));
                    if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.English)
                        this.labelHelp.Text = "After pushing the operation button, pass the product.";
                    else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Chinese)
                        this.labelHelp.Text = "按下操作按钮后，通过产品。";
                    else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Czech)
                        this.labelHelp.Text = "Po stisknutí ovládacího tlačítka produkt předejte.";
                    else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Russian)
                        this.labelHelp.Text = "После нажатия кнопки операции, передайте продукт.";
                    else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.German)
                        this.labelHelp.Text = "Übergeben Sie das Produkt, nachdem Sie die Bedientaste gedrückt haben.";
                    else if (this.ParentForm.ParentForm.SystemConfig1.Language == DataStore.LanguageID.Spanish)
                        this.labelHelp.Text = "Después de presionar el botón de Operación, pase el producto.";
                    else
                        this.labelHelp.Text = "운전 버튼을 누른 후, 제품을 통과시켜 주십시오.";

                    this.buttonNext.Enabled = false;
                    this.labelFeedSpeed.Enabled = false;
                    this.labelFeedSpeed.ForeColor = Color.Gray;
                    this.labelProductLength.Enabled = false;
                    this.labelProductLength.ForeColor = Color.Gray;
                    break;
                default:
                    break;
            }
        }
        public void CaculateJudgmentData(int sensorDetectingTime)
        {
            double time2; // 시간2 : (컨베이어 길이 - 제품길이) / 속도
            double time3;
            double speed; // 이송속도2 : 제품길이 / 시간1

            if (this.WizardStep == DataStore.Step3.Step3)
            {
                this.labelSensingTime.Text = sensorDetectingTime.ToString();

                SmartSplash splash = new SmartSplash();
                splash.CenterPosition = true;
                splash.AnimationInterval = 200;
                splash.LoadingImagePathname = "SmartLoading4";
                splash.Start();

                // 이송속도2 = 제품길이 / 시간1
                speed = Convert.ToDouble(this.ParentForm.ParentForm.CurrentJudgmentSetItem.ProductLength) / Convert.ToDouble(sensorDetectingTime);
                //speed2 = Math.Round(speed + 0.0005, 3) * 1000;
                //this.labelFeedSpeed2.Text = speed2.ToString();
                this.ParentForm.CenterSystemJudgmentManual.DisplaySpeedmmin(Math.Round(speed * 60, 2).ToString());
                //this.labelFeedSpeedUnitmmin.Text = Math.Round(speed * 60, 2).ToString();

                // 판정지연 = 컨베어 길이/속도 = 시간1 + 시간2(=(컨베어 길이-제품길이) / 속도)
                time2 = Convert.ToDouble(this.ParentForm.ParentForm.SystemConfig2.ConveyorLength -
                            this.ParentForm.ParentForm.CurrentJudgmentSetItem.ProductLength) / speed;
                time2 = Math.Floor(time2);
                this.ParentForm.ParentForm.CurrentJudgmentSetItem.JudgmentDelayTime = sensorDetectingTime + Convert.ToInt32(time2);

                // 이중지연 = time1 + 150
                if (this.ParentForm.ParentForm.CurrentJudgmentSetItem.JudgmentDelayTime < sensorDetectingTime + 150)
                    this.ParentForm.ParentForm.CurrentJudgmentSetItem.DoubleDelayTime = this.ParentForm.ParentForm.CurrentJudgmentSetItem.JudgmentDelayTime - 50;
                else
                    this.ParentForm.ParentForm.CurrentJudgmentSetItem.DoubleDelayTime = sensorDetectingTime + 150;

                // 선별기 지연시간 = (선별점거리 - 제품길이) + 제품길이 X 1/3 / 속도
                //time3 = Convert.ToDouble(this.ParentForm.ParentForm.SystemConfig2.SortingPointLength
                //    - (this.ParentForm.ParentForm.CurrentJudgmentSetItem.ProductLength * 2 / 3)) / speed;
                //if (time3 < 0)
                //    time3 = 0.0;
                //else
                //    time3 = Math.Floor(time3);
                //this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter1DelayTime = Convert.ToInt32(time3);
                //this.ParentForm.ParentForm.CurrentJudgmentSetItem.Sorter2DelayTime = Convert.ToInt32(time3);
                //this.labelSorterDelayTime.Text = Convert.ToInt32(time3).ToString();

                this.ParentForm.ParentForm.SaveJudgmentSetFile(this.ParentForm.ParentForm.CurrentJudgmentSetItem, this.ParentForm.ParentForm.SystemConfig1.ProductNumber - 1);
                this.ParentForm.ParentForm.TransferProductParameter(this.ParentForm.ParentForm.SystemConfig1.ProductNumber);

                splash.Finish();
            }
        }

        public void DisplayRefresh(SystemStatus status)
        {
            this.ParentForm.ParentForm.CurrentSystemStatus.CurrentDisplay = DataStore.DisplayStore.SystemJudgmentAuto;

            this.labelFeedSpeed.Text = this.ParentForm.ParentForm.CurrentJudgmentSetItem.FeedSpeed1.ToString();
            this.labelProductLength.Text = this.ParentForm.ParentForm.CurrentJudgmentSetItem.ProductLength.ToString();
            this.labelSensingTime.Text = "0";

            if (this.ParentForm != null)
                this.ParentForm.UpdateEquipmentStatusDisplay(this.ParentForm.ParentForm.CurrentSystemStatus.Equipment);

            this.ChangeStep(DataStore.Step3.Step1);
        }
        #endregion

        #region Event Handler
        private void labelFeedSpeed_Click(object sender, EventArgs e)
        {
            string value = "", message = "";

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

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

                    value = Helper.StringZeroFillDigits4(myKeyPad.StringValue);
                    this.ParentForm.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.MainBoard, CommunicationAddress.FeedSpeed, value);

                    this.ParentForm.ParentForm.SaveJudgmentSetFile(this.ParentForm.ParentForm.CurrentJudgmentSetItem, this.ParentForm.ParentForm.SystemConfig1.ProductNumber - 1);
                }
            }
        }

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

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

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.IntValue < 1) // || myKeyPad.IntValue > int.Parse(this.labelConveyorLength.Text))
                {
                    // 입력범위를 확인하세요
                    message = "1~"; // + this.labelConveyorLength.Text;
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig1.Language, message);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelProductLength.Text = myKeyPad.StringValue;
                    this.ParentForm.ParentForm.CurrentJudgmentSetItem.ProductLength = myKeyPad.IntValue;

                    this.ParentForm.ParentForm.SaveJudgmentSetFile(this.ParentForm.ParentForm.CurrentJudgmentSetItem, this.ParentForm.ParentForm.SystemConfig1.ProductNumber - 1);
                }
            }
        }

        private void buttonNext_Click(object sender, EventArgs e)
        {
            switch (this.WizardStep)
            {
                case DataStore.Step3.Step1:
                    this.ChangeStep(DataStore.Step3.Step2);
                    break;
                case DataStore.Step3.Step2:
                    this.ChangeStep(DataStore.Step3.Step3);
                    break;
                default:
                    break;
            }
        }
        private void buttonExit_Click(object sender, EventArgs e)
        {
            this.ParentForm.CenterSystemJudgmentManual.DisplayRefresh(this.ParentForm.ParentForm.CurrentSystemStatus);
            this.ParentForm.CenterSystemJudgmentManual.BringToFront();
        }
        #endregion        
    }
}
