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

namespace Project.Dialog
{
    public partial class fIOMonitor : Form
    {

        public fIOMonitor()
        {
            InitializeComponent();
            this.FormClosed += fIOMonitor_FormClosed;

            this.Opacity = 1.0;

            this.tblDI.SuspendLayout();
            this.tblDO.SuspendLayout();
            this.tblFG.SuspendLayout();

            tblDI.setTitle(Pub.dio.DIName);
            tblDO.setTitle(Pub.dio.DOName);
            tblFG.setTitle(Pub.flag.Name);

            var pinNameI = new string[Pub.dio.DIName.Length];
            var pinNameO = new string[Pub.dio.DOName.Length];
            for (int i = 0; i < pinNameI.Length; i++)
                pinNameI[i] = Enum.GetName(typeof(eDIPin), i);
            for (int i = 0; i < pinNameO.Length; i++)
                pinNameO[i] = Enum.GetName(typeof(eDOPin), i);

            tblDI.setNames(pinNameI);
            tblDO.setNames(pinNameO);

            tblDI.setItemTextAlign(ContentAlignment.BottomLeft);
            tblDO.setItemTextAlign(ContentAlignment.BottomLeft);
            tblFG.setItemTextAlign(ContentAlignment.BottomLeft);

            var NamePKY = new string[16];
            var NamePKZ = new string[16];
            var NamePKT = new string[16];
            var NamePLM = new string[16];
            var NamePLZ = new string[16];
            var NamePRM = new string[16];
            var NamePRZ = new string[16];
            var NamePRL = new string[16];
            var NamePRR = new string[16];
            var NameVS0 = new string[16];
            var NameVS1 = new string[16];
            var NameVS2 = new string[16];

            for (int i = 0; i < NamePKY.Length; i++)
                NamePKY[i] = Enum.GetName(typeof(eILockPKX), i);
            for (int i = 0; i < NamePKZ.Length; i++)
                NamePKZ[i] = Enum.GetName(typeof(eILockPKZ), i);
            for (int i = 0; i < NamePKT.Length; i++)
                NamePKT[i] = Enum.GetName(typeof(eILockPKT), i);
            for (int i = 0; i < NamePLM.Length; i++)
                NamePLM[i] = Enum.GetName(typeof(eILockPRM), i);
            for (int i = 0; i < NamePLZ.Length; i++)
                NamePLZ[i] = Enum.GetName(typeof(eILockPRZ), i);
            for (int i = 0; i < NamePRM.Length; i++)
                NamePRM[i] = Enum.GetName(typeof(eILockPRM), i);
            for (int i = 0; i < NamePRZ.Length; i++)
                NamePRZ[i] = Enum.GetName(typeof(eILockPRZ), i);
            for (int i = 0; i < NamePRL.Length; i++)
                NamePRL[i] = Enum.GetName(typeof(eILockPRL), i);
            for (int i = 0; i < NamePRR.Length; i++)
                NamePRR[i] = Enum.GetName(typeof(eILockPRR), i);
            for (int i = 0; i < NameVS0.Length; i++)
                NameVS0[i] = Enum.GetName(typeof(eILockVS0), i);
            for (int i = 0; i < NameVS1.Length; i++)
                NameVS1[i] = Enum.GetName(typeof(eILockVS1), i);
            for (int i = 0; i < NameVS2.Length; i++)
                NameVS2[i] = Enum.GetName(typeof(eILockVS2), i);


            gviPKX.setTitle(NamePKY);
            gviPKZ.setTitle(NamePKZ);
            gviPKT.setTitle(NamePKT);
            gviPLM.setTitle(NamePLM);
            gviPLZ.setTitle(NamePLZ);
            gviPRM.setTitle(NamePRM);
            gviPRZ.setTitle(NamePRZ);
            gviPRL.setTitle(NamePRL);
            gviPRR.setTitle(NamePRR);
            gviVS0.setTitle(NameVS0);
            gviVS1.setTitle(NameVS1);
            gviVS2.setTitle(NameVS2);

            gviPKX.setItemTextAlign(ContentAlignment.MiddleCenter);
            gviPKZ.setItemTextAlign(ContentAlignment.MiddleCenter);
            gviPKT.setItemTextAlign(ContentAlignment.MiddleCenter);
            gviPLM.setItemTextAlign(ContentAlignment.MiddleCenter);
            gviPLZ.setItemTextAlign(ContentAlignment.MiddleCenter);
            gviPRM.setItemTextAlign(ContentAlignment.MiddleCenter);
            gviPRZ.setItemTextAlign(ContentAlignment.MiddleCenter);
            gviPRL.setItemTextAlign(ContentAlignment.MiddleCenter);
            gviPRR.setItemTextAlign(ContentAlignment.MiddleCenter);
            gviVS0.setItemTextAlign(ContentAlignment.MiddleCenter);
            gviVS1.setItemTextAlign(ContentAlignment.MiddleCenter);
            gviVS2.setItemTextAlign(ContentAlignment.MiddleCenter);

            gviPKX.ColorList[1].BackColor1 = Color.IndianRed; gviPKX.ColorList[1].BackColor2 = Color.LightCoral;
            gviPKZ.ColorList[1].BackColor1 = Color.IndianRed; gviPKZ.ColorList[1].BackColor2 = Color.LightCoral;
            gviPKT.ColorList[1].BackColor1 = Color.IndianRed; gviPKT.ColorList[1].BackColor2 = Color.LightCoral;
            gviPLM.ColorList[1].BackColor1 = Color.IndianRed; gviPLM.ColorList[1].BackColor2 = Color.LightCoral;
            gviPLZ.ColorList[1].BackColor1 = Color.IndianRed; gviPLZ.ColorList[1].BackColor2 = Color.LightCoral;
            gviPRM.ColorList[1].BackColor1 = Color.IndianRed; gviPRM.ColorList[1].BackColor2 = Color.LightCoral;
            gviPRZ.ColorList[1].BackColor1 = Color.IndianRed; gviPRZ.ColorList[1].BackColor2 = Color.LightCoral;
            gviPRL.ColorList[1].BackColor1 = Color.IndianRed; gviPRL.ColorList[1].BackColor2 = Color.LightCoral;
            gviPRR.ColorList[1].BackColor1 = Color.IndianRed; gviPRR.ColorList[1].BackColor2 = Color.LightCoral;
            gviVS0.ColorList[1].BackColor1 = Color.IndianRed; gviVS0.ColorList[1].BackColor2 = Color.LightCoral;
            gviVS1.ColorList[1].BackColor1 = Color.IndianRed; gviVS1.ColorList[1].BackColor2 = Color.LightCoral;
            gviVS2.ColorList[1].BackColor1 = Color.IndianRed; gviVS2.ColorList[1].BackColor2 = Color.LightCoral;

            //값확인
            List<Boolean> diValue = new List<bool>();
            for (int i = 0; i < Pub.dio.PIN_I; i++)
                diValue.Add(Pub.dio.getDIValue(i));
            List<Boolean> doValue = new List<bool>();
            for (int i = 0; i < Pub.dio.PIN_O; i++)
                doValue.Add(Pub.dio.getDOValue(i));
            List<Boolean> fgValue = new List<bool>();
            for (int i = 0; i < Pub.flag.PortCount; i++)
                fgValue.Add(Pub.flag.get(i));

            tblDI.setValue(diValue.ToArray());
            tblDO.setValue(doValue.ToArray());
            tblFG.setValue(fgValue.ToArray());


            List<Boolean> PKXValue = new List<bool>();
            List<Boolean> PKZValue = new List<bool>();
            List<Boolean> PKTValue = new List<bool>();
            List<Boolean> PLMValue = new List<bool>();
            List<Boolean> PLZValue = new List<bool>();
            List<Boolean> PRMValue = new List<bool>();
            List<Boolean> PRZValue = new List<bool>();
            List<Boolean> PRLValue = new List<bool>();
            List<Boolean> PRRValue = new List<bool>();
            List<Boolean> VS0Value = new List<bool>();
            List<Boolean> VS1Value = new List<bool>();
            List<Boolean> VS2Value = new List<bool>();
            for (int i = 0; i < 16; i++)
            {
                PKXValue.Add(Pub.LockPKX.get(i));
                PKZValue.Add(Pub.LockPKZ.get(i));
                PKTValue.Add(Pub.LockPKT.get(i));
                PLMValue.Add(Pub.LockPLM.get(i));
                PLZValue.Add(Pub.LockPLZ.get(i));
                PRMValue.Add(Pub.LockPRM.get(i));
                PRZValue.Add(Pub.LockPRZ.get(i));
                PRLValue.Add(Pub.LockPRL.get(i));
                PRRValue.Add(Pub.LockPRR.get(i));
                VS0Value.Add(Pub.LockVS0.get(i));
                VS1Value.Add(Pub.LockVS1.get(i));
                VS2Value.Add(Pub.LockVS2.get(i));
            }
            gviPKX.setValue(PKXValue.ToArray());
            gviPKZ.setValue(PKZValue.ToArray());
            gviPKT.setValue(PKTValue.ToArray());
            gviPLM.setValue(PLMValue.ToArray());
            gviPLZ.setValue(PLZValue.ToArray());
            gviPRM.setValue(PRMValue.ToArray());
            gviPRZ.setValue(PRZValue.ToArray());
            gviPRL.setValue(PRLValue.ToArray());
            gviPRR.setValue(PRRValue.ToArray());
            gviVS0.setValue(VS0Value.ToArray());
            gviVS1.setValue(VS1Value.ToArray());
            gviVS2.setValue(VS2Value.ToArray());

            Pub.dio.IOValueChanged += dio_IOValueChanged;
            Pub.flag.ValueChanged += flag_ValueChanged;

            Pub.LockPKX.ValueChanged += LockXF_ValueChanged;
            Pub.LockPKZ.ValueChanged += LockXF_ValueChanged;
            Pub.LockPKT.ValueChanged += LockXF_ValueChanged;
            Pub.LockPLM.ValueChanged += LockXF_ValueChanged;
            Pub.LockPLZ.ValueChanged += LockXF_ValueChanged;
            Pub.LockPRM.ValueChanged += LockXF_ValueChanged;
            Pub.LockPRZ.ValueChanged += LockXF_ValueChanged;
            Pub.LockPRL.ValueChanged += LockXF_ValueChanged;
            Pub.LockPRR.ValueChanged += LockXF_ValueChanged;
            Pub.LockVS0.ValueChanged += LockXF_ValueChanged;
            Pub.LockVS1.ValueChanged += LockXF_ValueChanged;
            Pub.LockVS2.ValueChanged += LockXF_ValueChanged;

            if (Pub.sm.Step != StateMachine.eSMStep.RUN)
            {
                this.tblDI.ItemClick += tblDI_ItemClick;
                this.tblDO.ItemClick += tblDO_ItemClick;
                this.tblFG.ItemClick += tblFG_ItemClick;

                this.gviPKX.ItemClick += gvILXF_ItemClick;
                this.gviPKZ.ItemClick += gvILXF_ItemClick;
                this.gviPKT.ItemClick += gvILXF_ItemClick;
                this.gviPLM.ItemClick += gvILXF_ItemClick;
                this.gviPLZ.ItemClick += gvILXF_ItemClick;
                this.gviPRM.ItemClick += gvILXF_ItemClick;
                this.gviPRZ.ItemClick += gvILXF_ItemClick;
                this.gviPRL.ItemClick += gvILXF_ItemClick;
                this.gviPRR.ItemClick += gvILXF_ItemClick;
                this.gviVS0.ItemClick += gvILXF_ItemClick;
                this.gviVS1.ItemClick += gvILXF_ItemClick;
                this.gviVS2.ItemClick += gvILXF_ItemClick;
            }


            this.tblDI.Invalidate();
            this.tblDO.Invalidate();
            this.tblFG.Invalidate();

            this.gviPKX.Invalidate();
            this.gviPKZ.Invalidate();
            this.gviPKT.Invalidate();
            this.gviPLM.Invalidate();
            this.gviPLZ.Invalidate();
            this.gviPRM.Invalidate();
            this.gviPRZ.Invalidate();
            this.gviPRL.Invalidate();
            this.gviPRR.Invalidate();
            this.gviVS0.Invalidate();
            this.gviVS1.Invalidate();
            this.gviVS2.Invalidate();

            this.lbTitle.MouseMove += LbTitle_MouseMove;
            this.lbTitle.MouseUp += LbTitle_MouseUp;
            this.lbTitle.MouseDown += LbTitle_MouseDown;
            this.lbTitle.DoubleClick += lbTitle_DoubleClick;

            this.KeyDown += fIOMonitor_KeyDown;
        }


        void fIOMonitor_FormClosed(object sender, FormClosedEventArgs e)
        {
            Pub.dio.IOValueChanged -= dio_IOValueChanged;
            Pub.flag.ValueChanged -= flag_ValueChanged;


            Pub.LockPKX.ValueChanged -= LockXF_ValueChanged;
            Pub.LockPKZ.ValueChanged -= LockXF_ValueChanged;
            Pub.LockPKT.ValueChanged -= LockXF_ValueChanged;
            Pub.LockPLM.ValueChanged -= LockXF_ValueChanged;
            Pub.LockPLZ.ValueChanged -= LockXF_ValueChanged;
            Pub.LockPRM.ValueChanged -= LockXF_ValueChanged;
            Pub.LockPRZ.ValueChanged -= LockXF_ValueChanged;
            Pub.LockPRL.ValueChanged -= LockXF_ValueChanged;
            Pub.LockPRR.ValueChanged -= LockXF_ValueChanged;
            Pub.LockVS0.ValueChanged -= LockXF_ValueChanged;
            Pub.LockVS1.ValueChanged -= LockXF_ValueChanged;
            Pub.LockVS2.ValueChanged -= LockXF_ValueChanged;

            if (Pub.isRunning == false)
            {
                this.tblDI.ItemClick -= tblDI_ItemClick;
                this.tblDO.ItemClick -= tblDO_ItemClick;
                this.tblFG.ItemClick -= tblFG_ItemClick;

                this.gviPKX.ItemClick -= gvILXF_ItemClick;
                this.gviPKZ.ItemClick -= gvILXF_ItemClick;
                this.gviPKT.ItemClick -= gvILXF_ItemClick;
                this.gviPLM.ItemClick -= gvILXF_ItemClick;
                this.gviPLZ.ItemClick -= gvILXF_ItemClick;
                this.gviPRM.ItemClick -= gvILXF_ItemClick;
                this.gviPRZ.ItemClick -= gvILXF_ItemClick;
                this.gviPRL.ItemClick -= gvILXF_ItemClick;
                this.gviPRR.ItemClick -= gvILXF_ItemClick;
                this.gviVS0.ItemClick -= gvILXF_ItemClick;
                this.gviVS1.ItemClick -= gvILXF_ItemClick;
                this.gviVS2.ItemClick -= gvILXF_ItemClick;
            }

            this.lbTitle.MouseMove -= LbTitle_MouseMove;
            this.lbTitle.MouseUp -= LbTitle_MouseUp;
            this.lbTitle.MouseDown -= LbTitle_MouseDown;
            this.lbTitle.DoubleClick -= lbTitle_DoubleClick;

            this.KeyDown -= fIOMonitor_KeyDown;
        }

        void LockXF_ValueChanged(object sender, CInterLock.ValueEventArgs e)
        {
            var item = sender as CInterLock;
            var tagStr = item.Tag.ToString();

            if (tagStr == "PKX")
            {
                if (gviPKX.setValue(e.ArrIDX, e.NewValue)) gviPKX.Invalidate();
            }
            else if (tagStr == "PKZ")
            {
                if (gviPKZ.setValue(e.ArrIDX, e.NewValue)) gviPKZ.Invalidate();
            }
            else if (tagStr == "PKT")
            {
                if (gviPKT.setValue(e.ArrIDX, e.NewValue)) gviPKT.Invalidate();
            }
            else if (tagStr == "PLM")
            {
                if (gviPLM.setValue(e.ArrIDX, e.NewValue)) gviPLM.Invalidate();
            }
            else if (tagStr == "PLZ")
            {
                if (gviPLZ.setValue(e.ArrIDX, e.NewValue)) gviPLZ.Invalidate();
            }
            else if (tagStr == "PRM")
            {
                if (gviPRM.setValue(e.ArrIDX, e.NewValue)) gviPRM.Invalidate();
            }
            else if (tagStr == "PRZ")
            {
                if (gviPRZ.setValue(e.ArrIDX, e.NewValue)) gviPRZ.Invalidate();
            }
            else if (tagStr == "PRL")
            {
                if (gviPRL.setValue(e.ArrIDX, e.NewValue)) gviPRL.Invalidate();
            }
            else if (tagStr == "PRR")
            {
                if (gviPRR.setValue(e.ArrIDX, e.NewValue)) gviPRR.Invalidate();
            }
            else if (tagStr == "VS0")
            {
                if (gviVS0.setValue(e.ArrIDX, e.NewValue)) gviVS0.Invalidate();
            }
            else if (tagStr == "VS1")
            {
                if (gviVS1.setValue(e.ArrIDX, e.NewValue)) gviVS1.Invalidate();
            }
            else if (tagStr == "VS2")
            {
                if (gviVS2.setValue(e.ArrIDX, e.NewValue)) gviVS2.Invalidate();
            }
        }

        void gvILXF_ItemClick(object sender, arFrame.Control.GridView.ItemClickEventArgs e)
        {
            var gv = sender as arFrame.Control.GridView;
            var tagStr = gv.Tag.ToString();
            if (tagStr == "PKX")
            {
                var curValue = Pub.LockPKX.get(e.idx);
                Pub.LockPKX.set(e.idx, !curValue, "IOMONITOR");
            }
            else if (tagStr == "PKZ")
            {
                var curValue = Pub.LockPKZ.get(e.idx);
                Pub.LockPKZ.set(e.idx, !curValue, "IOMONITOR");
            }
            else if (tagStr == "PKT")
            {
                var curValue = Pub.LockPKT.get(e.idx);
                Pub.LockPKT.set(e.idx, !curValue, "IOMONITOR");
            }
            else if (tagStr == "PLM")
            {
                var curValue = Pub.LockPKT.get(e.idx);
                Pub.LockPLM.set(e.idx, !curValue, "IOMONITOR");
            }
            else if (tagStr == "PLZ")
            {
                var curValue = Pub.LockPKT.get(e.idx);
                Pub.LockPLZ.set(e.idx, !curValue, "IOMONITOR");
            }
            else if (tagStr == "PRM")
            {
                var curValue = Pub.LockPKT.get(e.idx);
                Pub.LockPRM.set(e.idx, !curValue, "IOMONITOR");
            }
            else if (tagStr == "PRZ")
            {
                var curValue = Pub.LockPKT.get(e.idx);
                Pub.LockPRZ.set(e.idx, !curValue, "IOMONITOR");
            }


            else if (tagStr == "PRL")
            {
                var curValue = Pub.LockPRL.get(e.idx);
                Pub.LockPRL.set(e.idx, !curValue, "IOMONITOR");
            }
            else if (tagStr == "PRR")
            {
                var curValue = Pub.LockPRR.get(e.idx);
                Pub.LockPRR.set(e.idx, !curValue, "IOMONITOR");
            }
            else if (tagStr == "VS0")
            {
                var curValue = Pub.LockVS0.get(e.idx);
                Pub.LockVS0.set(e.idx, !curValue, "IOMONITOR");
            }
            else if (tagStr == "VS1")
            {
                var curValue = Pub.LockVS1.get(e.idx);
                Pub.LockVS1.set(e.idx, !curValue, "IOMONITOR");
            }
            else if (tagStr == "VS2")
            {
                var curValue = Pub.LockVS2.get(e.idx);
                Pub.LockVS2.set(e.idx, !curValue, "IOMONITOR");
            }

        }

        void lbTitle_DoubleClick(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Maximized) this.WindowState = FormWindowState.Normal;
            else this.WindowState = FormWindowState.Maximized;
        }

        void fIOMonitor_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape) this.Close();
            else if (e.KeyCode == Keys.D && e.Control)
            {
                this.tblDO.showDebugInfo = !this.tblDO.showDebugInfo;
                this.tblDI.showDebugInfo = this.tblDO.showDebugInfo;
                this.tblFG.showDebugInfo = this.tblDO.showDebugInfo;
            }
        }

        private void fIOMonitor_Load(object sender, EventArgs e)
        {
            this.Text = "I/O Monitor";
            this.Show();
            //'Application.DoEvents();

            Dialog.QuickControl fctl = new QuickControl();
            fctl.TopLevel = false;
            fctl.Visible = true;
            this.panel3.Controls.Add(fctl);
            fctl.Dock = DockStyle.Top;
            fctl.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;

            fctl.FormBorderStyle = FormBorderStyle.None;//.panTitleBar.Visible = false;
            //  fctl.panTopMenuDiv.Visible = false;
            fctl.panBG.BackColor = this.BackColor;
            fctl.panBG.BorderStyle = BorderStyle.None;
            fctl.Show();
            fctl.Dock = DockStyle.Fill;

            this.tmDisplay.Start();
        }

        #region "Mouse Form Move"

        private Boolean fMove = false;
        private Point MDownPos;
        private void LbTitle_MouseMove(object sender, MouseEventArgs e)
        {
            if (fMove)
            {
                Point offset = new Point(e.X - MDownPos.X, e.Y - MDownPos.Y);
                this.Left += offset.X;
                this.Top += offset.Y;
                offset = new Point(0, 0);
            }
        }
        private void LbTitle_MouseUp(object sender, MouseEventArgs e)
        {
            fMove = false;
        }
        private void LbTitle_MouseDown(object sender, MouseEventArgs e)
        {
            MDownPos = new Point(e.X, e.Y);
            fMove = true;
        }

        #endregion

        void tblFG_ItemClick(object sender, arFrame.Control.GridView.ItemClickEventArgs e)
        {
            var curValue = Pub.flag.get(e.idx);
            Pub.flag.set(e.idx, !curValue, "IOMONITOR");
        }

        void tblDO_ItemClick(object sender, arFrame.Control.GridView.ItemClickEventArgs e)
        {
            var newValue = !Pub.dio.getDOValue(e.idx);
            if (Pub.dio.IsInit == false)
            {
                //임시시그널
                var dlg = Util.MsgQ("가상 시그널을 생성하시겠습니까?");
                if (dlg == System.Windows.Forms.DialogResult.Yes)
                {
                    Pub.dio.RaiseEvent(eIOPINDIR.OUTPUT, e.idx, newValue);
                    Pub.log.Add("fake do : " + e.idx.ToString() + ",val=" + newValue.ToString());
                }
            }
            else
            {
                Pub.dio.SetOutput(e.idx, newValue);
                Pub.log.Add(string.Format("set output(iomonitor-userclick) idx={0},val={1}", e.idx, newValue));
            }
        }

        void tblDI_ItemClick(object sender, arFrame.Control.GridView.ItemClickEventArgs e)
        {
            var newValue = !Pub.dio.getDIValue(e.idx);
            if (Pub.dio.IsInit == false || Pub.flag.get(eFlag.DEBUG) == true)
            {
                //임시시그널
                var dlg = Util.MsgQ("가상 시그널을 생성하시겠습니까?");
                if (dlg == System.Windows.Forms.DialogResult.Yes)
                {
                    Pub.dio.RaiseEvent(eIOPINDIR.INPUT, e.idx, newValue);
                    Pub.log.Add("fake di : " + e.idx.ToString() + ",val=" + newValue.ToString());
                }
            }
        }

        void flag_ValueChanged(object sender, Flag.ValueEventArgs e)
        {
            //var butIndex = getControlIndex(e.ArrIDX);
            //if (butIndex >= this.tblFG.Controls.Count) return;

            //해당 아이템의 값을 변경하고 다시 그린다.
            if (tblFG.setValue(e.ArrIDX, e.NewValue))
                tblFG.Invalidate();//.drawItem(e.ArrIDX);
        }


        void dio_IOValueChanged(object sender, arDev.AzinAxt.DIO.IOValueEventArgs e)
        {
            //var butIndex = getControlIndex(e.ArrIDX);
            if (e.Direction == eIOPINDIR.INPUT)
            {
                //해당 아이템의 값을 변경하고 다시 그린다.
                if (tblDI.setValue(e.ArrIDX, e.NewValue))
                    tblDI.Invalidate();//.drawItem(e.ArrIDX);
            }
            else
            {
                //해당 아이템의 값을 변경하고 다시 그린다.
                if (tblDO.setValue(e.ArrIDX, e.NewValue))
                    tblDO.Invalidate();//.drawItem(e.ArrIDX);
            }
        }

        private void tbClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void tmDisplay_Tick(object sender, EventArgs e)
        {
            //flg update
            lbTitle3.Text = "FLAG(" + Pub.flag.Value.ToString() + ")";
            //var jobend = Pub.flag.get(32);
            for (int i = 0; i < 64; i++)
                tblFG.setValue(i, Pub.flag.get(i));
            tblFG.Invalidate();

            this.label1.Text = string.Format("PICKER-X({0})", Pub.LockPKX.Value);
            this.lbXR.Text = string.Format("PICKER-Z({0})", Pub.LockPKZ.Value);
            this.label5.Text = string.Format("THETA({0})", Pub.LockPKT.Value);
            this.label10.Text = string.Format("VISION-0({0})", Pub.LockVS0.Value);
            this.label2.Text = string.Format("VISION-1({0})", Pub.LockVS1.Value);
            this.label11.Text = string.Format("VISION-2({0})", Pub.LockVS2.Value);

            //8,9,6,7,4,3
            this.label8.Text = string.Format("PRINTER-LEFT({0})", Pub.LockPRL.Value);
            this.label9.Text = string.Format("PRINTER-RIGHT({0})", Pub.LockPRR.Value);
            this.label6.Text = string.Format("PRINTER-LEFT-MOVE({0})", Pub.LockPLM.Value);
            this.label7.Text = string.Format("PRINTER-RIGHT-MOVE({0})", Pub.LockPRM.Value);
            this.label4.Text = string.Format("PRINTER-LEFT-UP/DN({0})", Pub.LockPLZ.Value);
            this.label3.Text = string.Format("PRINTER-RIGHT-UP/DN({0})", Pub.LockPRZ.Value);
        }

        private void saveIOListToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var namelist = Enum.GetNames(typeof(eDIName));
            var vlist = Enum.GetValues(typeof(eDIName));
            var pinlist = Enum.GetNames(typeof(eDIPin));

            System.Text.StringBuilder sb = new StringBuilder();
            sb.AppendLine("#DI");
            sb.AppendLine("IDX,PIN,DESCRIPTION");
            for(int i = 0; i < 64;i++)
            {
                var ev = (eDIName)i;
                var val = (int)(ev);
                var ep = (eDIPin)val;
                var desc = ev.ToString() == val.ToString() ? string.Empty : ev.ToString();
                sb.AppendLine(string.Format("{0},{1},{2}", val, ep, desc));
            }
            sb.AppendLine();
            sb.AppendLine("#DO");
            sb.AppendLine("IDX,PIN,DESCRIPTION");
             vlist = Enum.GetValues(typeof(eDOName));

            for (int i = 0; i < 64; i++)
            {
                var ev = (eDOName)i;
                var val = (int)(ev);
                var ep = (eDOPin)val;
                var desc = ev.ToString() == val.ToString() ? string.Empty : ev.ToString();
                sb.AppendLine(string.Format("{0},{1},{2}", val, ep, desc));
            }

            SaveFileDialog sd = new SaveFileDialog();
            sd.Filter = "csv|*.csv";
            sd.FileName = "iolist.csv";
            if (sd.ShowDialog() == DialogResult.OK)
            {
                System.IO.File.WriteAllText(sd.FileName, sb.ToString(), System.Text.Encoding.Default);
                Util.RunExplorer(sd.FileName);
            }

        }
    }
}
