using MesControl;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Xml.Linq;
//using SystemFramework;

namespace MisDataName
{
    public delegate void ControlConnectPLC();
    public partial class SecondForm
    {
        ControlConnectPLC PlcConnectTimer;

        void InitFormDynamicControls()
        {
            try
            { 
               // DataTable dt_OpName=(DataTable) FunctionForm_HashiTable.OpName_Hash_Read("OpNameList");

                List<MesBoolSignal> signalItems = InitWorkStationSignal();
                RecordWorkStationSignal(signalItems);

                 int RowsWidth = SignalPanel.Width-20;
                 int RowsNum = RowsWidth / 133;
                 int i = 0;
                var opNameList = DtWebApi.OpcDemo.Get_opNameList();
                foreach (var opName in opNameList)
                {
                    PlcWorkStationSignal plcWorkStationSignal = new PlcWorkStationSignal();

                    plcWorkStationSignal.BackColor = System.Drawing.Color.Transparent;
                    plcWorkStationSignal.Location = new System.Drawing.Point(3 + ((i % RowsNum) * 133), (i / RowsNum) * ((signalItems.Count * 25) + 60));
                    plcWorkStationSignal.MaximumSize = new System.Drawing.Size(0, 0);
                    plcWorkStationSignal.MinimumSize = new System.Drawing.Size(0, 0);
                    plcWorkStationSignal.Name = "plcWorkStationSignal_" + opName;
                    plcWorkStationSignal.Size = new System.Drawing.Size(136, 60);
                    plcWorkStationSignal.TabIndex = i;
                    plcWorkStationSignal.SignalItems = signalItems;
                    plcWorkStationSignal.WorkStationNameText = opName;
                    plcWorkStationSignal.WS_PlcLabelText = "PLC连接";
                    plcWorkStationSignal.TestPLC_Interval = 10000;

                    plcWorkStationSignal.TestPLC_Enable = true;
                    PlcConnectTimer += plcWorkStationSignal.ExternalTimerExcute;

                    this.SignalPanel.Controls.Add(plcWorkStationSignal);
                    i++;
                }

                
            }
            catch (Exception err)
            {
                //ApplicationLog.WriteLog(err, err.Message);
            }

        }

        public List<MesBoolSignal> InitWorkStationSignal()
        {
            try
            { 
                List<MesBoolSignal> signalItems = new List<MesBoolSignal>();
                MesBoolSignal MBS;
                signalItems.Clear();
                XDocument SystemConfig = XDocument.Load("SystemConfig.xml");
                IEnumerable<XElement> plc = SystemConfig.Descendants ("PLC");
                IEnumerable<XElement> plcSignal = plc.Elements();
                foreach (XElement PlcSignal in plcSignal)
                {
                    XElement Name = PlcSignal.Element("Name");
                    XElement TagType = PlcSignal.Element("TagType");
                    XElement Text = PlcSignal.Element("Text");
                    XElement Index = PlcSignal.Element("Index");
                    XElement Enabled = PlcSignal.Element("Enabled");

                    MBS = new MesBoolSignal();
                    MBS.Name = "TagType_" + TagType.Value ;
                    MBS.SignalText = Text.Value ;
                    MBS.SignalImage = MesBoolSignal.SignalImage_Enum.none;

                    if (Convert.ToBoolean(Enabled.Value))
                    {
                        signalItems.Add(MBS);
                    }
                }
                IEnumerable<XElement> mes = SystemConfig.Descendants("MES");
                IEnumerable<XElement> mesSignal = mes.Elements();
                foreach (XElement MesSignal in mesSignal)
                {
                    XElement Name = MesSignal.Element("Name");
                    XElement TagType = MesSignal.Element("TagType");
                    XElement Text = MesSignal.Element("Text");
                    XElement Index = MesSignal.Element("Index");
                    XElement Enabled = MesSignal.Element("Enabled");

                    MBS = new MesBoolSignal();
                    MBS.Name = "TagType_" + TagType.Value;
                    MBS.SignalText = Text.Value;
                    MBS.SignalImage = MesBoolSignal.SignalImage_Enum.none;

                    if (Convert .ToBoolean ( Enabled .Value))
                    {
                        signalItems.Add(MBS);
                    }
                
                }

                return signalItems;
            }
            catch (Exception err)
            {
                //ApplicationLog.WriteLog(err, err.Message);
                return null;
            }
        }


        string[] SignalNames;

        void RecordWorkStationSignal(List<MesBoolSignal> items)
        {
            SignalNames = new string[items.Count];
            for (int i=0;i< items.Count;i++)
            {
                SignalNames[i] = items[i].Name;
            }


        }



    }
}
