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

namespace FLY.Thick.RemoteHistory
{
    public class BUFFER_OBJ_INTERFACE
    {
        #region Pack
        public class Pack_List<T> : IPack2
            where T : IPack2, new()
        {
            public bool isAdd;//不是add，就是changed
            public T[] list;

            #region IPack2 成员

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

                buf.AddRange(BitConverter.GetBytes(isAdd));
                if (list == null)
                {
                    buf.AddRange(BitConverter.GetBytes(0));
                }
                else
                {
                    buf.AddRange(BitConverter.GetBytes(list.Count()));
                    for (int i = 0; i < list.Count(); i++)
                    {
                        buf.AddRange(list[i].ToBytes());
                    }
                }
                return buf.ToArray();
            }

            public bool TryParse(byte[] value)
            {
                int cnt;
                return TryParse(value, 0, out cnt);
            }

            public bool TryParse(byte[] value, int index, out int cnt)
            {
                cnt = 1 + 4;
                if (value.Length < (index + cnt))
                    return false;
                int idx = index;

                isAdd = BitConverter.ToBoolean(value, idx);
                idx += 1;

                int len = BitConverter.ToInt32(value, idx);
                idx += 4;
                if (len <= 0)
                {
                    list = null;
                    return true;
                }

                list = new T[len];
                for (int i = 0; i < len; i++)
                {
                    int c;
                    list[i] = new T();
                    if (!list[i].TryParse(value, idx, out c))
                        return false;
                    idx += c;
                    cnt += c;
                }
                return true;
            }
            #endregion

        }
        public class Pack_Params
        {
            public int size;

            #region IPack实现
            public byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();

                buf.AddRange(BitConverter.GetBytes(size));

                return buf.ToArray();
            }

            public bool TryParse(byte[] value)
            {
                int cnt = 4;
                if (value.Length < cnt)
                    return false;

                int idx = 0;
                size = BitConverter.ToInt32(value, idx);
                idx += 4;

                return true;
            }
            #endregion
        }
        #endregion

        #region GetValue
        public const UInt16 GET_PARAMS = 0;
        /// <summary>
        /// Pack_List
        /// </summary>
        public const UInt16 GET_LIST = 1;

        #endregion

        #region SetValue
        public const UInt16 SET_PARAMS = 0;
        #endregion
        #region PushMsg

        public const UInt16 PUSH_PARAMS = 0;
        /// <summary>
        /// Pack_List
        /// </summary>
        public const UInt16 PUSH_LIST = 1;
        #endregion
    }
}
