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

namespace FLY.Thick.Base.Common
{
    public class TimeGridEventArgs : EventArgs
    {
        /// <summary>
        /// 数据, 厚度数据！！！！！
        /// </summary>
        public int[] data;
        /// <summary>
        /// 单数据时间间隔
        /// </summary>
        public TimeSpan ts;
        /// <summary>
        /// 开始时间点
        /// </summary>
        public DateTime time;

        #region IPack 成员

        public byte[] ToBytes()
        {
            List<byte> buf = new List<byte>();

            buf.AddRange(BitConverter.GetBytes(time.Ticks));
            buf.AddRange(BitConverter.GetBytes(ts.Ticks));
            buf.AddRange(BitConverter.GetBytes(data.Length));
            for (int i = 0; i < data.Length; i++)
                buf.AddRange(BitConverter.GetBytes(data[i]));

            return buf.ToArray();
        }

        public bool TryParse(byte[] value)
        {
            int cnt = 8 * 2 + 4;
            if (value.Length < cnt)
                return false;
            int idx = 0;
            time = new DateTime(BitConverter.ToInt64(value, idx));
            idx += 8;
            ts = TimeSpan.FromTicks(BitConverter.ToInt64(value, idx));
            idx += 8;
            int len = BitConverter.ToInt32(value, idx);
            idx += 4;
            cnt += (len * 4);
            if (value.Length < cnt)
                return false;

            data = new int[len];

            for (int i = 0; i < len; i++)
            {
                data[i] = BitConverter.ToInt32(value, idx);
                idx += 4;
            }
            return true;
        }

        #endregion
    }
}
