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

namespace WCS.Entity.Protocol.DataStructure
{
    /// <summary>
    ///  设备数据包
    /// </summary>
    [DataContract]
    public class DeviceDataPack
    {
        [DataMember(Order = 0)]
        public ProtocolData<WCS_BCR80>[] BCR80 { get; set; }

        [DataMember(Order = 1)]
        public ProtocolData<WCS_BCR81>[] BCR81 { get; set; }

        [DataMember(Order = 2)]
        public ProtocolData<WCS_BCR83>[] BCR83 { get; set; }

        [DataMember(Order = 3)]
        public ProtocolData<WCS_RGV520>[] RGV520 { get; set; }

        [DataMember(Order = 4)]
        public ProtocolData<WCS_RGV521>[] RGV521 { get; set; }

        [DataMember(Order = 5)]
        public ProtocolData<WCS_Robot520>[] Robot520 { get; set; }

        [DataMember(Order = 6)]
        public ProtocolData<WCS_Robot521>[] Robot521 { get; set; }

        [DataMember(Order = 7)]
        public ProtocolData<WCS_Robot522>[] Robot522 { get; set; }

        [DataMember(Order = 8)]
        public ProtocolData<WCS_Robot530>[] Robot530 { get; set; }

        [DataMember(Order = 9)]
        public ProtocolData<WCS_Robot531>[] Robot531 { get; set; }

        [DataMember(Order = 10)]
        public ProtocolData<WCS_SRM520>[] SRM520 { get; set; }

        [DataMember(Order = 11)]
        public ProtocolData<WCS_SRM521>[] SRM521 { get; set; }

        [DataMember(Order = 12)]
        public ProtocolData<WCS_SRM523>[] SRM523 { get; set; }

        [DataMember(Order = 13)]
        public ProtocolData<WCS_Station520>[] Station520 { get; set; }

        [DataMember(Order = 14)]
        public ProtocolData<WCS_Station521>[] Station521 { get; set; }

        [DataMember(Order = 15)]
        public ProtocolData<WCS_Station523>[] Station523 { get; set; }

        [DataMember(Order = 16)]
        public ProtocolData<WCS_Station524>[] Station524 { get; set; }

        [DataMember(Order = 17)]
        public ProtocolData<WCS_Station525>[] Station525 { get; set; }

        [DataMember(Order = 18)]
        public ProtocolData<WCS_Station90>[] Station90 { get; set; }

        [DataMember(Order = 19)]
        public ProtocolData<WCS_Station91>[] Station91 { get; set; }

        [DataMember(Order = 20)]
        public ProtocolData<WCS_Truss520>[] Truss520 { get; set; }

        [DataMember(Order = 21)]
        public ProtocolData<WCS_Truss521>[] Truss521 { get; set; }

        [DataMember(Order = 22)]
        public ProtocolData<WCS_Truss523>[] Truss523 { get; set; }

        [DataMember(Order = 23)]
        public ProtocolData<WCS_Truss530>[] Truss530 { get; set; }

        [DataMember(Order = 24)]
        public ProtocolData<WCS_Truss531>[] Truss531 { get; set; }

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

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

        [DataMember(Order = 27)]
        public ProtocolData<WCS_RGV523>[] RGV523 { 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;
        }
    }

    [DataContract]
    public class ProtocolData
    {
        [DataMember(Order = 0)]
        public string Code { get; set; }

        [DataMember(Order = 1)]
        public DateTime Frame { get; set; }
    }

    [DataContract]
    public class ProtocolData<T> : ProtocolData
    {
        [DataMember(Order = 2)]
        public T Data { get; set; }
    }
}
