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

using SmartX;
using INT_PT002.DataStore;
using INT_PT002.Forms;
using INT_PT002.DialogForms;

namespace INT_PT002.Controls
{
    public partial class ControlMenuManualIoTest : UserControl
    {
        #region Field
        private FormMenu m_ParentForm;

        private Color ColorEnable;
        private Color ColorDisable;

        private Collection<SmartLabel> CollLabelDiff;
        private Collection<SmartLabel> CollLabelDisp;
        private Collection<SmartLabel> CollLabelVacu;
        private Collection<SmartLabel> CollLabelInput;
        private Collection<SmartLabel> CollLabelSbAlarm;
        #endregion

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

            this.ParentForm = parent;
            this.Initialize();
        } 
        #endregion

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

        #region Method
        private void Initialize()
        {
            this.smartGroupBox1.Text = "Manual > IO Test";

            this.ColorEnable = Color.Lime;
            this.ColorDisable = Color.Gainsboro;

            this.CollLabelDiff = new Collection<SmartLabel>();
            this.CollLabelDiff.Add(this.labelDiff1);
            this.CollLabelDiff.Add(this.labelDiff2);
            this.CollLabelDiff.Add(this.labelDiff3);
            this.CollLabelDiff.Add(this.labelDiff4);
            this.CollLabelDiff.Add(this.labelDiff5);
            this.CollLabelDiff.Add(this.labelDiff6);
            this.CollLabelDiff.Add(this.labelDiff7);
            this.CollLabelDiff.Add(this.labelDiff8);
            this.CollLabelDiff.Add(this.labelDiff9);
            this.CollLabelDiff.Add(this.labelDiff10);

            this.CollLabelDisp = new Collection<SmartLabel>();
            this.CollLabelDisp.Add(this.labelDisp1);
            this.CollLabelDisp.Add(this.labelDisp2);
            this.CollLabelDisp.Add(this.labelDisp3);
            this.CollLabelDisp.Add(this.labelDisp4);
            this.CollLabelDisp.Add(this.labelDisp5);
            this.CollLabelDisp.Add(this.labelDisp6);
            this.CollLabelDisp.Add(this.labelDisp7);
            this.CollLabelDisp.Add(this.labelDisp8);
            this.CollLabelDisp.Add(this.labelDisp9);
            this.CollLabelDisp.Add(this.labelDisp10);

            this.CollLabelVacu = new Collection<SmartLabel>();
            this.CollLabelVacu.Add(this.labelVacuum1);
            this.CollLabelVacu.Add(this.labelVacuum2);
            this.CollLabelVacu.Add(this.labelVacuum3);
            this.CollLabelVacu.Add(this.labelVacuum4);
            this.CollLabelVacu.Add(this.labelVacuum5);
            this.CollLabelVacu.Add(this.labelVacuum6);
            this.CollLabelVacu.Add(this.labelVacuum7);
            this.CollLabelVacu.Add(this.labelVacuum8);
            this.CollLabelVacu.Add(this.labelVacuum9);
            this.CollLabelVacu.Add(this.labelVacuum10);

            this.CollLabelInput = new Collection<SmartLabel>();
            this.CollLabelInput.Add(this.labelInput1);
            this.CollLabelInput.Add(this.labelInput2);
            this.CollLabelInput.Add(this.labelInput3);
            this.CollLabelInput.Add(this.labelInput4);
            this.CollLabelInput.Add(this.labelInput5);

            this.CollLabelSbAlarm = new Collection<SmartLabel>();
            this.CollLabelSbAlarm.Clear();
            this.CollLabelSbAlarm.Add(this.labelSbAlarm1);
            this.CollLabelSbAlarm.Add(this.labelSbAlarm2);
            this.CollLabelSbAlarm.Add(this.labelSbAlarm3);
            this.CollLabelSbAlarm.Add(this.labelSbAlarm4);
            this.CollLabelSbAlarm.Add(this.labelSbAlarm5);
            this.CollLabelSbAlarm.Add(this.labelSbAlarm6);
            this.CollLabelSbAlarm.Add(this.labelSbAlarm7);
            this.CollLabelSbAlarm.Add(this.labelSbAlarm8);
            this.CollLabelSbAlarm.Add(this.labelSbAlarm9);
            this.CollLabelSbAlarm.Add(this.labelSbAlarm10);

            for (int i = 0; i < this.ParentForm.ParentForm.CurrentSystemStatus.EquipmentColumn; i++)
            {
                this.CollLabelDiff[i].Text = "0.00";
                this.CollLabelDisp[i].Text = "0.00";
                this.CollLabelVacu[i].Text = "0.0";
                this.CollLabelSbAlarm[i].Visible = false;
            }

            for (int i = 0; i < this.CollLabelInput.Count; i++)
                this.CollLabelInput[i].RoundRectFillColor = this.ColorDisable;
        }

        private void OutputCommand(string address, string data)
        {
            string command = "", id = "";
            
            command = CommunicationCommand.IOTest;
            id = CommunicationID.MainBoard;

            this.ParentForm.ParentForm.TransferDataStream(command, id, address, data);
        }
        private void ServoParameterAllRead()
        {
            SmartSplash.Start(SmartSplash.BuiltInLoadingImages.PROCESSING1, 200);
            
            this.ParentForm.Enabled = false;

            // ServoMotor 1
            this.labelMotor1Origin.Text = this.ParentForm.ParentForm.ServoMotorRead2(1, ServoMotorAddress.CMD21);
            this.labelMotor1Front1.Text = this.ParentForm.ParentForm.ServoMotorRead2(1, ServoMotorAddress.CMD16);
            this.labelMotor1Front2.Text = this.ParentForm.ParentForm.ServoMotorRead2(1, ServoMotorAddress.CMD17);

            // ServoMotor 2
            this.labelMotor2Origin.Text = this.ParentForm.ParentForm.ServoMotorRead2(2, ServoMotorAddress.CMD21);
            this.labelMotor2Turn.Text = this.ParentForm.ParentForm.ServoMotorRead2(2, ServoMotorAddress.CMD25);

            SmartSplash.Finish();

            this.ParentForm.Enabled = true;
        }

        public void UpdateDisplayDispData(Collection<DispData> datas)
        {
            for (int i = 0; i < this.ParentForm.ParentForm.CurrentSystemStatus.EquipmentColumn; i++)
                this.CollLabelDisp[i].Text = datas[i].RData;
        }
        public void UpdateDisplayDiffData(Collection<DiffData> datas)
        {
            for (int i = 0; i < this.ParentForm.ParentForm.CurrentSystemStatus.EquipmentColumn; i++)
                this.CollLabelDiff[i].Text = datas[i].MAdc;
        }
        public void UpdateDisplayPresData(Collection<PressureData> datas)
        {
            for (int i = 0; i < this.ParentForm.ParentForm.CurrentSystemStatus.EquipmentColumn; i++)
                this.CollLabelVacu[i].Text = datas[i].WorkingChamber;
        }
        public void UpdateDisplayInputData(Collection<string> datas)
        {
            Color value = this.ColorDisable;
            for (int i = 0; i < datas.Count; i++)
            {
                if (datas[i] == "0")
                    value = this.ColorDisable;
                else
                    value = this.ColorEnable;

                this.CollLabelInput[i].RoundRectFillColor = value;
            }
        }

        public void UpdateDisplayAlarmView(AlarmList alarm)
        {
            this.CollLabelSbAlarm[0].Visible = alarm.CollectionIsSensorBoardError[0];
            this.CollLabelSbAlarm[1].Visible = alarm.CollectionIsSensorBoardError[1];
            this.CollLabelSbAlarm[2].Visible = alarm.CollectionIsSensorBoardError[2];
            this.CollLabelSbAlarm[3].Visible = alarm.CollectionIsSensorBoardError[3];
            this.CollLabelSbAlarm[4].Visible = alarm.CollectionIsSensorBoardError[4];
            this.CollLabelSbAlarm[5].Visible = alarm.CollectionIsSensorBoardError[5];
            this.CollLabelSbAlarm[6].Visible = alarm.CollectionIsSensorBoardError[6];
            this.CollLabelSbAlarm[7].Visible = alarm.CollectionIsSensorBoardError[7];
            this.CollLabelSbAlarm[8].Visible = alarm.CollectionIsSensorBoardError[8];
            this.CollLabelSbAlarm[9].Visible = alarm.CollectionIsSensorBoardError[9];
        }

        public void DisplayRefresh()
        {
            this.ParentForm.ParentForm.CurrentSystemStatus.CurrentDisplayMode = Define.E_DisplayModeStore.IOTest;
            this.ParentForm.ParentForm.SetDisplayMode(Define.E_EquipmentMode.IOTest);

            this.ServoParameterAllRead();
        }
        #endregion

        #region Event Handler
        // Output 1~10
        private void buttonOut1_Click(object sender, EventArgs e)
        {
            string address = "", data = "";
            SmartButton button = sender as SmartButton;

            if (button == null)
                return;

            // adress
            address = Helper.StringBlankFillDigits4(CommunicationAddress._7701_Output1);

            // data
            if (button.ButtonStatus == SmartButton.BUTSTATUS.DOWN)
                data = Helper.StringBlankFillDigits4("1");
            else
                data = Helper.StringBlankFillDigits4("0");

            this.OutputCommand(address, data);
        }
        private void buttonOut2_Click(object sender, EventArgs e)
        {
            string address = "", data = "";
            SmartButton button = sender as SmartButton;

            if (button == null)
                return;

            // adress
            address = Helper.StringBlankFillDigits4(CommunicationAddress._7702_Output2);

            // data
            if (button.ButtonStatus == SmartButton.BUTSTATUS.DOWN)
                data = Helper.StringBlankFillDigits4("1");
            else
                data = Helper.StringBlankFillDigits4("0");

            this.OutputCommand(address, data);
        }
        private void buttonOut3_Click(object sender, EventArgs e)
        {
            string address = "", data = "";
            SmartButton button = sender as SmartButton;

            if (button == null)
                return;

            // adress
            address = Helper.StringBlankFillDigits4(CommunicationAddress._7703_Output3);

            // data
            if (button.ButtonStatus == SmartButton.BUTSTATUS.DOWN)
                data = Helper.StringBlankFillDigits4("1");
            else
                data = Helper.StringBlankFillDigits4("0");

            this.OutputCommand(address, data);
        }
        private void buttonOut4_Click(object sender, EventArgs e)
        {
            string address = "", data = "";
            SmartButton button = sender as SmartButton;

            if (button == null)
                return;

            // adress
            address = Helper.StringBlankFillDigits4(CommunicationAddress._7704_Output4);

            // data
            if (button.ButtonStatus == SmartButton.BUTSTATUS.DOWN)
                data = Helper.StringBlankFillDigits4("1");
            else
                data = Helper.StringBlankFillDigits4("0");

            this.OutputCommand(address, data);
        }
        private void buttonOut5_Click(object sender, EventArgs e)
        {
            string address = "", data = "";
            SmartButton button = sender as SmartButton;

            if (button == null)
                return;

            // adress
            address = Helper.StringBlankFillDigits4(CommunicationAddress._7705_Output5);

            // data
            if (button.ButtonStatus == SmartButton.BUTSTATUS.DOWN)
                data = Helper.StringBlankFillDigits4("1");
            else
                data = Helper.StringBlankFillDigits4("0");

            this.OutputCommand(address, data);
        }
        private void buttonOut6_Click(object sender, EventArgs e)
        {
            string address = "", data = "";
            SmartButton button = sender as SmartButton;

            if (button == null)
                return;

            // adress
            address = Helper.StringBlankFillDigits4(CommunicationAddress._7706_Output6);

            // data
            if (button.ButtonStatus == SmartButton.BUTSTATUS.DOWN)
                data = Helper.StringBlankFillDigits4("1");
            else
                data = Helper.StringBlankFillDigits4("0");

            this.OutputCommand(address, data);
        }
        private void buttonOut7_Click(object sender, EventArgs e)
        {
            string address = "", data = "";
            SmartButton button = sender as SmartButton;

            if (button == null)
                return;

            // adress
            address = Helper.StringBlankFillDigits4(CommunicationAddress._7707_Output7);

            // data
            if (button.ButtonStatus == SmartButton.BUTSTATUS.DOWN)
                data = Helper.StringBlankFillDigits4("1");
            else
                data = Helper.StringBlankFillDigits4("0");

            this.OutputCommand(address, data);
        }
        private void buttonOut8_Click(object sender, EventArgs e)
        {
            string address = "", data = "";
            SmartButton button = sender as SmartButton;

            if (button == null)
                return;

            // adress
            address = Helper.StringBlankFillDigits4(CommunicationAddress._7708_Output8);

            // data
            if (button.ButtonStatus == SmartButton.BUTSTATUS.DOWN)
                data = Helper.StringBlankFillDigits4("1");
            else
                data = Helper.StringBlankFillDigits4("0");

            this.OutputCommand(address, data);
        }
        private void buttonOut9_Click(object sender, EventArgs e)
        {
            string address = "", data = "";
            SmartButton button = sender as SmartButton;

            if (button == null)
                return;

            // adress
            address = Helper.StringBlankFillDigits4(CommunicationAddress._7709_Output9);

            // data
            if (button.ButtonStatus == SmartButton.BUTSTATUS.DOWN)
                data = Helper.StringBlankFillDigits4("1");
            else
                data = Helper.StringBlankFillDigits4("0");

            this.OutputCommand(address, data);
        }
        private void buttonOut10_Click(object sender, EventArgs e)
        {
            string address = "", data = "";
            SmartButton button = sender as SmartButton;

            if (button == null)
                return;

            // adress
            address = Helper.StringBlankFillDigits4(CommunicationAddress._7710_Output10);

            // data
            if (button.ButtonStatus == SmartButton.BUTSTATUS.DOWN)
                data = Helper.StringBlankFillDigits4("1");
            else
                data = Helper.StringBlankFillDigits4("0");

            this.OutputCommand(address, data);
        }

        // Motor 1
        private void buttonMotor1Origin_Click(object sender, EventArgs e)
        {
            this.ParentForm.ParentForm.TransferData(CommunicationCommand.MotorOrigin, CommunicationID.SubBoard1);
        }
        private void buttonMotor1AlarmClear_Click(object sender, EventArgs e)
        {
            this.ParentForm.ParentForm.TransferData(CommunicationCommand.MotorAlarmReset, CommunicationID.SubBoard1);
        }
        private void buttonMotor1Front_Click(object sender, EventArgs e)
        {
            this.ParentForm.ParentForm.TransferData(CommunicationCommand.MotorMove1, CommunicationID.SubBoard1);
        }
        private void buttonMotor1Back_Click(object sender, EventArgs e)
        {
            this.ParentForm.ParentForm.TransferData(CommunicationCommand.MotorMove2, CommunicationID.SubBoard1);
        }
        private void labelMotor1Origin_Click(object sender, EventArgs e)
        {
            string caption = "Motor1 Origin";
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(caption, this.labelMotor1Origin.Text, 7, 0, false);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue == 0)
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.LANGUAGE);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.ParentForm.ParentForm.ServoMotorWrite2(1, ServoMotorAddress.CMD21, myKeyPad.IntValue, this.labelMotor1Origin);
                    this.ParentForm.ParentForm.ServoMotorWrite2(1, ServoMotorAddress.CMD2, myKeyPad.IntValue);
                }
            }
        }
        private void labelMotor1Front1_Click(object sender, EventArgs e)
        {
            string caption = "Motor1 Front1";
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(caption, this.labelMotor1Front1.Text, 7, 0, false);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue == 0)
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.LANGUAGE);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.ParentForm.ParentForm.ServoMotorWrite2(1, ServoMotorAddress.CMD16, myKeyPad.IntValue, this.labelMotor1Front1);
                }
            }
        }
        private void labelMotor1Front2_Click(object sender, EventArgs e)
        {
            string caption = "Motor1 Front2";
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(caption, this.labelMotor1Front2.Text, 7, 0, false);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue == 0)
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.LANGUAGE);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.ParentForm.ParentForm.ServoMotorWrite2(1, ServoMotorAddress.CMD17, myKeyPad.IntValue, this.labelMotor1Front2);
                }
            }
        }

        // Motor 2
        private void buttonMotor2Origin_Click(object sender, EventArgs e)
        {
            this.ParentForm.ParentForm.TransferData(CommunicationCommand.MotorOrigin, CommunicationID.SubBoard2);
        }
        private void buttonMotor2AlarmClear_Click(object sender, EventArgs e)
        {
            this.ParentForm.ParentForm.TransferData(CommunicationCommand.MotorAlarmReset, CommunicationID.SubBoard2);
        }
        private void buttonMotor2Turn_Click(object sender, EventArgs e)
        {
            this.ParentForm.ParentForm.TransferData(CommunicationCommand.MotorMove1, CommunicationID.SubBoard2);
        }
        private void labelMotor2Origin_Click(object sender, EventArgs e)
        {
            string caption = "Motor2 Origin";
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(caption, this.labelMotor2Origin.Text, 7, 0, false);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue == 0)
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.LANGUAGE);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.ParentForm.ParentForm.ServoMotorWrite2(2, ServoMotorAddress.CMD21, myKeyPad.IntValue, this.labelMotor2Origin);
                    this.ParentForm.ParentForm.ServoMotorWrite2(2, ServoMotorAddress.CMD16, myKeyPad.IntValue);
                    this.ParentForm.ParentForm.ServoMotorWrite2(2, ServoMotorAddress.CMD27, myKeyPad.IntValue);
                }
            }
        }
        private void labelMotor2Turn_Click(object sender, EventArgs e)
        {
            string caption = "Motor2 Turn";
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(caption, this.labelMotor2Turn.Text, 7, 0, false);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue == 0)
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.ParentForm.SystemConfig.LANGUAGE);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.ParentForm.ParentForm.ServoMotorWrite2(1, ServoMotorAddress.CMD25, myKeyPad.IntValue, this.labelMotor2Turn);
                }
            }
        }
        #endregion
    }
}
