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

namespace FLY.Thick.Base.OBJ_INTERFACE
{
    public class PROFILE_OBJ_INTERFACE
    {
        #region Pack
        public class Pack_StringList : IPack
        {
            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
        }
        #endregion

        #region GetValue
        /// <summary>
        /// ProfileParam
        /// </summary>
        public const UInt16 GET_PARAMS = 0;
        #endregion
        #region SetValue
        /// <summary>
        /// ProfileParam
        /// </summary>
        public const UInt16 SET_PARAMS = 0;
        #endregion
        #region PushMsg
        /// <summary>
        /// ProfileParam
        /// </summary>
        public const UInt16 PUSH_PARAMS = 0;
        #endregion
        #region CallFunction
        /// <summary>
        /// request:null ;
        /// reponse:Pack_StringList
        /// </summary>
        public const UInt16 CALL_GETLIST = 0;
        /// <summary>
        /// request:Pack_String ;
        /// reponse:null
        /// </summary>
        public const UInt16 CALL_DEL = 1;
        /// <summary>
        /// request:Pack_String ;
        /// reponse:ProfileParam
        /// </summary>
        public const UInt16 CALL_READ = 2;
        #endregion
    }
}
