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

using SmartX;
using INT89DB_26.DialogForms;
using INT89DB_26_ImageDll;

namespace INT89DB_26.Forms
{
    public partial class FormEquipmentTest : Form
    {
        #region Field
        private FormMain m_ParentForm;

        private Collection<SmartLabel> CollectionLabelWeightValue;
        private Collection<SmartLabel> CollectionLabelADC;
        private Collection<Label> CollectionLabelZero;
        private Collection<Label> CollectionLabelSensor;
        private Collection<SmartButton> CollectionButtonConveyor;
        #endregion

        #region Constructor
        public FormEquipmentTest(FormMain parent)
        {
            InitializeComponent();

            this.ParentForm = parent;
        } 
        #endregion

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

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

            if (this.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.Korean)
            {

            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.English)
            {
                this.labelTitle.Text = "Equipment test";

                this.buttonBack.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.engBackDown));
                this.buttonBack.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.engBackUp));
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.Chinese)
            {
                this.labelTitle.Text = "设备测试";

                this.smartGroupBox1.Text = this.smartLabel1.Text = "重量";
                this.groupBoxConveyor.Text = this.smartLabel10.Text = "输送机";
                this.smartLabel7.Text = "选别机";
                this.labelStaticSorterDelayTime.Text = "延迟时间";
                this.labelStaticSorterOperatingTime.Text = "运行时间";
                this.smartLabel12.Text = "通信节目播送日记";
                this.smartLabel8.Text = "ADC节目播送日记";

                this.buttonLogOpen.Text = this.buttonAdcLogOpen.Text = "开";
                this.buttonAdcLogClose.Text = this.buttonLogClose.Text = "关闭";

                this.buttonBack.DownImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnBackDown));
                this.buttonBack.UpImage = new Bitmap(images.GetImage(Class1.ButtonImages.chnBackUp));
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.E_LanguageID.Czech)
            {

            }
            else
            {

            }
        }

        private void InitializeControl()
        {
          
        }
        private void DefaultSetting()
        {
            this.CollectionLabelWeightValue = new Collection<SmartLabel>();
            this.CollectionLabelWeightValue.Clear();
            this.CollectionLabelWeightValue.Add(this.labelWeight1);
            this.CollectionLabelWeightValue.Add(this.labelWeight2);
            this.CollectionLabelWeightValue.Add(this.labelWeight3);
            this.CollectionLabelWeightValue.Add(this.labelWeight4);
            this.CollectionLabelWeightValue.Add(this.labelWeight5);
            this.CollectionLabelWeightValue.Add(this.labelWeight6);

            this.CollectionLabelADC = new Collection<SmartLabel>();
            this.CollectionLabelADC.Clear();
            this.CollectionLabelADC.Add(this.labelADC1);
            this.CollectionLabelADC.Add(this.labelADC2);
            this.CollectionLabelADC.Add(this.labelADC3);
            this.CollectionLabelADC.Add(this.labelADC4);
            this.CollectionLabelADC.Add(this.labelADC5);
            this.CollectionLabelADC.Add(this.labelADC6);

            this.CollectionLabelZero = new Collection<Label>();
            this.CollectionLabelZero.Clear();
            this.CollectionLabelZero.Add(this.labelZero1);
            this.CollectionLabelZero.Add(this.labelZero2);
            this.CollectionLabelZero.Add(this.labelZero3);
            this.CollectionLabelZero.Add(this.labelZero4);
            this.CollectionLabelZero.Add(this.labelZero5);
            this.CollectionLabelZero.Add(this.labelZero6);

            this.CollectionLabelSensor = new Collection<Label>();
            this.CollectionLabelSensor.Clear();
            this.CollectionLabelSensor.Add(this.labelSensor1);
            this.CollectionLabelSensor.Add(this.labelSensor2);
            this.CollectionLabelSensor.Add(this.labelSensor3);
            this.CollectionLabelSensor.Add(this.labelSensor4);
            this.CollectionLabelSensor.Add(this.labelSensor5);
            this.CollectionLabelSensor.Add(this.labelSensor6);

            this.CollectionButtonConveyor = new Collection<SmartButton>();
            this.CollectionButtonConveyor.Clear();
            this.CollectionButtonConveyor.Add(this.buttonConveyor1);
            this.CollectionButtonConveyor.Add(this.buttonConveyor2);
            this.CollectionButtonConveyor.Add(this.buttonConveyor3);
            this.CollectionButtonConveyor.Add(this.buttonConveyor4);
            this.CollectionButtonConveyor.Add(this.buttonConveyor5);
            this.CollectionButtonConveyor.Add(this.buttonConveyor6);

            foreach (SmartLabel label in this.CollectionLabelWeightValue)
                label.Text = Helper.StringToDecimalPlaces("0", this.ParentForm.SystemConfig.DecimalPlaces);

            foreach (SmartLabel label in this.CollectionLabelADC)
                label.Text = "0";

            foreach (SmartButton button in this.CollectionButtonConveyor)
                button.ButtonUp();

            for (int i = 0; i < 6; i++)
            {
                this.CollectionLabelZero[i].Visible = false;
                this.CollectionLabelSensor[i].Visible = false;
            }
        }

        private void TransferDataStreamASetting(string value)
        {
            string length = "";

            length = Helper.StringZeroFillDigits4(value.Length.ToString());

            this.ParentForm.TransferDataStream(CommunicationCommand.BypassToMotor, CommunicationID.MainBoard, CommunicationAddress.None, length + "A" + value);
        }
        private void SetlabelOnOff(Label control, bool isOn)
        {
            if (isOn == true)
            {
                if (control.Visible != true)
                    control.Visible = true;
            }
            else
            {
                if (control.Visible != false)
                    control.Visible = false;
            }
        }

        public void UpdateEquipmentStatusDisplay(DataStore.EquipmentStatus status)
        {
            if (status == DataStore.EquipmentStatus.Start)
            {
                this.pictureBoxStart.Visible = true;
                this.pictureBoxStop.Visible = false;
                this.buttonBack.Enabled = false;
            }
            else
            {
                this.pictureBoxStart.Visible = false;
                this.pictureBoxStop.Visible = true;
                this.buttonBack.Enabled = true;
            }
        }
        public void UpdateStopWeightDisplay(DataStore.EquipmentStatus status, Collection<WeightData> weightDatas)
        {
            string value = "";

            if (status == DataStore.EquipmentStatus.Stop)
            {
                for (int i = 0; i < this.ParentForm.SystemConfig.EquipmentColumns; i++)
                {
                    // 중량
                    value = Helper.DoubleToString(weightDatas[i].Weight, this.ParentForm.SystemConfig.DecimalPlaces);
                    if (this.CollectionLabelWeightValue[i].Text != value)
                        this.CollectionLabelWeightValue[i].Text = value;

                    // ADC
                    value = weightDatas[i].ADCValue;
                    if (this.CollectionLabelADC[i].Text != value)
                        this.CollectionLabelADC[i].Text = value;

                    // 영점표시
                    if (weightDatas[i].Status == DataStore.WeightStatus.WeightZero)
                        this.CollectionLabelZero[i].Visible = true;
                    else
                        this.CollectionLabelZero[i].Visible = false;
                }
            }
            else
            {
                for (int i = 0; i < this.ParentForm.SystemConfig.EquipmentColumns; i++)
                {
                    // 영점표시
                    if (weightDatas[i].Status == DataStore.WeightStatus.WeightZero)
                        this.CollectionLabelZero[i].Visible = true;
                    else
                        this.CollectionLabelZero[i].Visible = false;
                }
            }
        }
        public void UpdateUpdateStartWeightDisplay(DataStore.EquipmentStatus status, Collection<WeightData> weightDatas)
        {
            this.UpdateUpdateStartWeightDisplay1(status, weightDatas[0]);
            this.UpdateUpdateStartWeightDisplay1(status, weightDatas[1]);
            this.UpdateUpdateStartWeightDisplay1(status, weightDatas[2]);
            this.UpdateUpdateStartWeightDisplay1(status, weightDatas[3]);
            this.UpdateUpdateStartWeightDisplay1(status, weightDatas[4]);
            this.UpdateUpdateStartWeightDisplay1(status, weightDatas[5]);   
        }
        public void UpdateUpdateStartWeightDisplay1(DataStore.EquipmentStatus status, WeightData weightData)
        {
            string value = "";

            value = Helper.DoubleToString(weightData.Weight, this.ParentForm.SystemConfig.DecimalPlaces);
            if (this.labelWeight1.Text != value)
                this.labelWeight1.Text = value;

            value = weightData.ADCValue;
            if (this.labelADC1.Text != value)
                this.labelADC1.Text = value;
        }
        public void UpdateUpdateStartWeightDisplay2(DataStore.EquipmentStatus status, WeightData weightData)
        {
            string value = "";

            value = Helper.DoubleToString(weightData.Weight, this.ParentForm.SystemConfig.DecimalPlaces);
            if (this.labelWeight2.Text != value)
                this.labelWeight2.Text = value;

            value = weightData.ADCValue;
            if (this.labelADC2.Text != value)
                this.labelADC2.Text = value;
        }
        public void UpdateUpdateStartWeightDisplay3(DataStore.EquipmentStatus status, WeightData weightData)
        {
            string value = "";

            value = Helper.DoubleToString(weightData.Weight, this.ParentForm.SystemConfig.DecimalPlaces);
            if (this.labelWeight3.Text != value)
                this.labelWeight3.Text = value;

            value = weightData.ADCValue;
            if (this.labelADC3.Text != value)
                this.labelADC3.Text = value;
        }
        public void UpdateUpdateStartWeightDisplay4(DataStore.EquipmentStatus status, WeightData weightData)
        {
            string value = "";

            value = Helper.DoubleToString(weightData.Weight, this.ParentForm.SystemConfig.DecimalPlaces);
            if (this.labelWeight4.Text != value)
                this.labelWeight4.Text = value;

            value = weightData.ADCValue;
            if (this.labelADC4.Text != value)
                this.labelADC4.Text = value;
        }
        public void UpdateUpdateStartWeightDisplay5(DataStore.EquipmentStatus status, WeightData weightData)
        {
            string value = "";

            value = Helper.DoubleToString(weightData.Weight, this.ParentForm.SystemConfig.DecimalPlaces);
            if (this.labelWeight5.Text != value)
                this.labelWeight5.Text = value;

            value = weightData.ADCValue;
            if (this.labelADC5.Text != value)
                this.labelADC5.Text = value;
        }
        public void UpdateUpdateStartWeightDisplay6(DataStore.EquipmentStatus status, WeightData weightData)
        {
            string value = "";

            value = Helper.DoubleToString(weightData.Weight, this.ParentForm.SystemConfig.DecimalPlaces);
            if (this.labelWeight6.Text != value)
                this.labelWeight6.Text = value;

            value = weightData.ADCValue;
            if (this.labelADC6.Text != value)
                this.labelADC6.Text = value;
        }
        public void UpdateInputRead(Collection<string> values)
        {
            if (values.Count == 0 || values == null)
                return;

            for (int i = 0; i < this.CollectionLabelSensor.Count; i++)
                this.SetlabelOnOff(this.CollectionLabelSensor[i], values[i + 8] == "1" ? true : false);
        }

        public void DisplayRefresh()
        {
            string value = "";
            this.ParentForm.SystemConfig.CurrentForm = DataStore.E_FormStore.FormEquipmentTest;

            this.ParentForm.TransferData(CommunicationCommand.ModeEquipmentTest, CommunicationID.MainBoard);

            value = this.ParentForm.CurrentJudgmentSetItem.Sorter1DelayTime.ToString();
            if (this.labelSorterDelayTime.Text != value)
                this.labelSorterDelayTime.Text = value;

            value = this.ParentForm.CurrentJudgmentSetItem.Sorter1RunTime.ToString();
            if (this.labelSorterOperatingTime.Text != value)
                this.labelSorterOperatingTime.Text = value;

            if (this.CollectionButtonConveyor != null && this.CollectionButtonConveyor.Count != 0)
            {
                foreach (SmartButton button in this.CollectionButtonConveyor)
                    button.ButtonUp();
            }
        }
        #endregion

        #region Override Member
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            this.InitializeDesign();
            this.InitializeControl();
            this.DefaultSetting();
        }
        #endregion

        #region Event Handler
        private void buttonBack_Click(object sender, EventArgs e)
        {
            this.ParentForm.ChildFormMenu.DisplayRefresh();
            ((FormMain)(Owner)).smartForm.Show((int)DataStore.E_FormStore.FormMenu);
        }

        private void buttonZero_Click(object sender, EventArgs e)
        {
            SmartButton button = sender as SmartButton;

            if (button == null)
                return;

            if (button == this.buttonZero1)
                this.ParentForm.TransferData(CommunicationCommand.Zero, CommunicationID.SubBoard1);
            else if (button == this.buttonZero2)
                this.ParentForm.TransferData(CommunicationCommand.Zero, CommunicationID.SubBoard2);
            else if (button == this.buttonZero3)
                this.ParentForm.TransferData(CommunicationCommand.Zero, CommunicationID.SubBoard3);
            else if (button == this.buttonZero4)
                this.ParentForm.TransferData(CommunicationCommand.Zero, CommunicationID.SubBoard4);
            else if (button == this.buttonZero5)
                this.ParentForm.TransferData(CommunicationCommand.Zero, CommunicationID.SubBoard5);
            else
                this.ParentForm.TransferData(CommunicationCommand.Zero, CommunicationID.SubBoard6);
        }
       
        private void pictureBoxStart_Click(object sender, EventArgs e)
        {
            this.ParentForm.TransferData(CommunicationCommand.Stop, CommunicationID.MainBoard);

            foreach (SmartButton button in this.CollectionButtonConveyor)
                button.ButtonUp();
        }
        private void pictureBoxStop_Click(object sender, EventArgs e)
        {
            this.ParentForm.TransferData(CommunicationCommand.Start, CommunicationID.MainBoard);

            foreach (SmartButton button in this.CollectionButtonConveyor)
                button.ButtonDown();
        }

        private void buttonAdcLogOpen_Click(object sender, EventArgs e)
        {
            this.ParentForm.smartFileAdcLog.Close();
            this.ParentForm.smartFileAdcLog.Open(this.ParentForm.BufferSmartUart);

            this.ParentForm.IsAdcLogOpen = true;
            this.ParentForm.timerAdcLog.Enabled = true;

            this.labelAdcLogOpen.Visible = true;
        }
        private void buttonAdcLogClose_Click(object sender, EventArgs e)
        {
            this.ParentForm.IsAdcLogOpen = false;
            this.ParentForm.timerAdcLog.Enabled = false;

            this.ParentForm.smartFileAdcLog.Close();

            this.labelAdcLogOpen.Visible = false;
        }

        private void buttonLogOpen_Click(object sender, EventArgs e)
        {
            if (this.ParentForm.IsCommunicationLogOpen == false)
                this.ParentForm.IsCommunicationLogOpen = true;

            this.ParentForm.smartFileCommunicationLog.Close();
            this.ParentForm.smartFileCommunicationLog.Open(this.ParentForm.BufferSmartUart);
        }
        private void buttonLogClose_Click(object sender, EventArgs e)
        {
            if (this.ParentForm.IsCommunicationLogOpen == true)
                this.ParentForm.smartFileCommunicationLog.Close();

            this.ParentForm.IsCommunicationLogOpen = false;
        }

        private void buttonCom3LogOpen_Click(object sender, EventArgs e)
        {
            if (this.ParentForm.IsCom3LogOpen == false)
                this.ParentForm.IsCom3LogOpen = true;

            this.ParentForm.smartFileSerialPort3Log.Close();
            this.ParentForm.smartFileSerialPort3Log.Open(this.ParentForm.BufferSmartUart);
        }

        private void buttonCom3LogClose_Click(object sender, EventArgs e)
        {
            if (this.ParentForm.IsCom3LogOpen == true)
                this.ParentForm.smartFileSerialPort3Log.Close();

            this.ParentForm.IsCom3LogOpen = false;
        }

        #region Conveyor
        private void buttonConveyor_Click(object sender, EventArgs e)
        {
            SmartButton button = sender as SmartButton;

            if (button == null)
                return;

            if (button.ButtonStatus == SmartButton.BUTSTATUS.DOWN)
            {
                this.ParentForm.TransferDataStream(CommunicationCommand.IOTest, CommunicationID.MainBoard, Helper.StringZeroFillDigits4(button.Tag.ToString()),
                    Helper.StringZeroFillDigits4("1"));
            }
            else
            {
                this.ParentForm.TransferDataStream(CommunicationCommand.IOTest, CommunicationID.MainBoard, Helper.StringZeroFillDigits4(button.Tag.ToString()),
                    Helper.StringZeroFillDigits4("0"));
            }
        }

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

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelSorterDelayTime.Text, 4, 0, false, this.ParentForm.SystemConfig.Language);
            myKeyPad.Location = new Point(300, 90);

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

                    value = Helper.StringZeroFillDigits4(this.labelSorterDelayTime.Text);
                    this.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.SubBoardAll, CommunicationAddress.Sorter1DelayTime, value);

                    this.ParentForm.SaveJudgmentSetFile(this.ParentForm.CurrentJudgmentSetItem, this.ParentForm.SystemConfig.ProductNumber - 1);
                }
            }
        }
        private void labelSorterOperatingTime_Click(object sender, EventArgs e)
        {
            string value = "";

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelSorterOperatingTime.Text, 4, 0, false, this.ParentForm.SystemConfig.Language);
            myKeyPad.Location = new Point(300, 90);

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

                    value = Helper.StringZeroFillDigits4(this.labelSorterOperatingTime.Text);
                    this.ParentForm.TransferDataStream(CommunicationCommand.Write, CommunicationID.SubBoardAll, CommunicationAddress.Sorter1RunTime, value);

                    this.ParentForm.SaveJudgmentSetFile(this.ParentForm.CurrentJudgmentSetItem, this.ParentForm.SystemConfig.ProductNumber - 1);
                }
            }
        }
        #endregion

        private void smartForm1_Click(object sender, EventArgs e)
        {

        }
        #endregion
    }
}