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

namespace FLY.Thick.Base.OBJ_INTERFACE
{
    public class Pack_StringList 
    {
        public List<string> list;
        #region IPack 成员

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

            byte[] bs;

            buf.AddRange(BitConverter.GetBytes(list.Count()));
            for (int i = 0; i < list.Count(); i++)
            {
                bs = Misc.Converter.StringToBytes(list[i]);
                buf.AddRange(BitConverter.GetBytes(bs.Count()));
                buf.AddRange(bs);
            }
            return buf.ToArray();
        }
        /// <summary>
        /// 返回由字节数组中指定位置的9个字节转换来的数据。
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool TryParse(byte[] value)
        {
            int cnt = 4;
            if (value.Length < cnt)
                return false;

            int idx = 0;
            int len = BitConverter.ToInt32(value, idx);
            idx += 4;
            cnt += len * 4;
            if (value.Length < cnt)
                return false;
            if (list == null)
                list = new List<string>();
            list.Clear();
            for (int i = 0; i < len; i++)
            {
                int l = BitConverter.ToInt32(value, idx);
                idx += 4;
                cnt += l;
                if (value.Length < cnt)
                    return false;

                string s = Misc.Converter.BytesToString(value, idx, l);
                idx += l;
                list.Add(s);
            }
            return true;
        }


        #endregion
    }
    public class Pack_String
    {
        public string data;
        #region IPack 成员

        public byte[] ToBytes()
        {
            List<byte> buf = new List<byte>();
            if (data != null)
            {
                byte[] bs = Misc.Converter.StringToBytes(data);
                buf.AddRange(BitConverter.GetBytes(bs.Length));
                buf.AddRange(bs);
            }
            else
            {
                buf.AddRange(BitConverter.GetBytes(0));
            }

            return buf.ToArray();
        }
        /// <summary>
        /// 返回由字节数组中指定位置的9个字节转换来的数据。
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool TryParse(byte[] value, int index, int count)
        {
            int cnt = 4;
            if (value.Length < cnt)
                return false;

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

            cnt += len;
            if (value.Length < cnt)
                return false;
            if (len <= 0)
                data = null;
            else
            {
                data = Misc.Converter.BytesToString(value, idx, len);
                idx += len;
            }
            return true;
        }

        public bool TryParse(byte[] value)
        {
            return TryParse(value, 0, value.Length);
        }
        #endregion
    }
}