using SourceGen;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using WCS.Entity.Protocol.BCR;
using WCS.Entity.Protocol.Protocol.DataStructure;
using WCS.Entity.Protocol.Robot;
using WCS.Entity.Protocol.SRM;
using WCS.Entity.Protocol.Station;

namespace WCS.Entity.Protocol.DataStructure
{
    /// <summary>
    ///  设备数据包
    /// </summary>
    [DataContract]
    public class DeviceDataPack
    {
        /// <summary>
        ///  堆垛机数据集
        /// </summary>
        [DataMember(Order = 0)]
        public DeviceDataCollection<SRMData> SRMDatas { get; set; }

        /// <summary>
        ///  穿梭车数据集
        /// </summary>
        [DataMember(Order = 1)]
        public DeviceDataCollection<RGVData> RGVDatas { get; set; }

        /// <summary>
        ///  站台数据集
        /// </summary>
        [DataMember(Order = 2)]
        public DeviceDataCollection<StationData> StationDatas { get; set; }

        ///// <summary>
        /////  扫码器数据
        ///// </summary>
        //[DataMember(Order = 3)]
        //public DeviceDataCollection<BCRData> BcrDatas { get; set; }

        /// <summary>
        ///  机器人数据集
        /// </summary>
        [DataMember(Order = 3)]
        public DeviceDataCollection<RobotData> RobotData { get; set; }

        /// <summary>
        ///  机器人数据集
        /// </summary>
        [DataMember(Order = 4)]
        public DeviceDataCollection<TrussData> TrussData { get; set; }

        ///// <summary>
        /////  产线数据集
        ///// </summary>
        //[DataMember(Order = 5)]
        //public DeviceDataCollection<ProdLineData> ProdLineData { get; set; }

        /// <summary>
        ///  时间
        /// </summary>
        [DataMember(Order = 5)]
        public DateTime Frame { get; set; }

        /// <summary>
        /// 回放最早时间
        /// </summary>
        [DataMember(Order = 6)]
        public int PlaybackSeconds { get; set; }
    }


    [DataContract]
    public class DataPack
    {
        [DataMember(Order = 0)]
        public DateTime Frame { get; set; }
        [DataMember(Order = 1)]
        public ProtocolData<IStation520Data>[] Station520 { get; set; }
        [DataMember(Order = 2)]
        public ProtocolData<IStation521Data>[] Station521 { get; set; }
        [DataMember(Order = 3)]
        public ProtocolData<IStation23Data>[] Station23 { get; set; }
        [DataMember(Order = 4)]
        public ProtocolData<IStation523Data>[] Station523 { get; set; }
        [DataMember(Order = 5)]
        public ProtocolData<IBCR81Data>[] BCR81 { get; set; }
        [DataMember(Order = 6)]
        public ProtocolData<ISRM520Data>[] SRM520 { get; set; }
        [DataMember(Order = 7)]
        public ProtocolData<ISRM521Data>[] SRM521 { get; set; }
        [DataMember(Order = 8)]
        public ProtocolData<ISRM537Data>[] SRM537 { get; set; }
        [DataMember(Order = 9)]
        public ProtocolData<IRobot520Data>[] Robot520 { get; set; }
        [DataMember(Order = 10)]
        public ProtocolData<IRobot521Data>[] Robot521 { get; set; }
        [DataMember(Order = 11)]
        public ProtocolData<IRobot522Data>[] Robot522 { get; set; }
        [DataMember(Order = 12)]
        public ProtocolData<IRobot530Data>[] Robot530 { get; set; }
        [DataMember(Order = 13)]
        public ProtocolData<IRobot531Data>[] Robot531 { get; set; }
        [DataMember(Order = 14)]
        public ProtocolData<IQT51Data>[] QT51 { get; set; }
        [DataMember(Order = 15)]
        public ProtocolData<LogContent>[] LogContent { get; set; }
        [DataMember(Order = 16)]
        public ProtocolData<ISRM542Data>[] SRM542 { get; set; }
        public Dictionary<string, ProtocolData[]> GetDeviceData()
        {
            List<Tuple<string, ProtocolData>> list = new List<Tuple<string, ProtocolData>>();
            foreach (var p in this.GetType().GetProperties())
            {
                if (p.PropertyType.IsArray)
                {
                    var arr = p.GetValue(this) as Array;
                    foreach (var a in arr)
                    {
                        var code = a.GetType().GetProperty("Code").GetValue(a).ToString();
                        //var data = a.GetType().GetProperty("Data").GetValue(a);
                        list.Add(new Tuple<string, ProtocolData>(code, a as ProtocolData));
                    }
                }
            }
            var res = list.GroupBy(v => v.Item1).ToDictionary(v => v.Key, v => v.Select(d => d.Item2).ToArray());
            return res;
        }





    }
}
