using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SIASUN.Autopilot.Device
{
    class Package_Data_Msg<T>
    {
        public Package_Data_Msg()
        {
        }
        /// <summary>
        /// 实际数据部分
        /// </summary>
        [Data(DataType = "LIST", Description = "", Sequence = 2)]
        public List<T> Message { get; set; }

        public byte[] Package()
        {

            System.IO.MemoryStream mspackage = new System.IO.MemoryStream();
            System.IO.MemoryStream msbody = new System.IO.MemoryStream();
            System.IO.MemoryStream msmessage = new System.IO.MemoryStream();

            byte[] bytedata;

            foreach (object o in this.Message as System.Collections.IEnumerable)
            {
                bytedata = MessageCoder.Serialize(o);
                msmessage.Write(bytedata, 0, bytedata.Length);
            }
            byte[] bytemessage = msmessage.ToArray();


            mspackage.Write(bytemessage, 0, bytemessage.Length);
            return mspackage.ToArray();
        }

        public object UnPackage(string msgId, byte[] data)
        {
            //#region 小端转成大端
            //byte[] bytefield = new byte[data.Length];

            //for (int i = data.Length - 1; i >= 0; i--)
            //{
            //    bytefield[data.Length - 1 - i] = data[i];
            //}
            //#endregion
            this.Message = new List<T>();
            object msg = MessageId.GetMessageByType(msgId);
            this.Message.Add((T)MessageCoder.DeSerialize(msg, data));

            return this;
        }
    }
}
