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

namespace FLY.Thick.RemoteHistory
{
    public class REMOTEHISTORY_OBJ_INTERFACE
    {
        #region Pack
        public class Pack_Params : IPack 
        {
            public int keeyday;
            public int saverows;
            public int minSaveInterval;

            public byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();
                buf.AddRange(BitConverter.GetBytes(keeyday));
                buf.AddRange(BitConverter.GetBytes(saverows));
                buf.AddRange(BitConverter.GetBytes(minSaveInterval));
                return buf.ToArray();
            }

            public bool TryParse(byte[] value)
            {
                if (value.Length < 12)
                    return false;
                int idx = 0;
                keeyday = BitConverter.ToInt32(value, idx);
                idx += 4;
                saverows = BitConverter.ToInt32(value, idx);
                idx += 4;
                minSaveInterval = BitConverter.ToInt32(value, idx);
                idx += 4;
                return true;

            }
        }
        public class Pack_State : IPack 
        {
            public int currrows;
            public string currpath;
            public int currdays;
            public byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();
                buf.AddRange(BitConverter.GetBytes(currdays));
                buf.AddRange(BitConverter.GetBytes(currrows));
                if (currpath != null)
                {
                    byte[] bs = Misc.Converter.StringToBytes(currpath);
                    int len = bs.Length;
                    buf.AddRange(BitConverter.GetBytes(len));
                    buf.AddRange(bs);
                }
                else 
                {
                    int len = 0;
                    buf.AddRange(BitConverter.GetBytes(len));
                }
                return buf.ToArray();
            }

            public bool TryParse(byte[] value)
            {
                int cnt = 12;
                if (value.Length < cnt)
                    return false;
                int idx = 0;
                currdays = BitConverter.ToInt32(value, idx);
                idx += 4;
                currrows = BitConverter.ToInt32(value, idx);
                idx += 4;
                int len = BitConverter.ToInt32(value, idx);
                idx += 4;
                cnt += len;
                if (value.Length < cnt)
                    return false;
                if (len > 0)
                {
                    currpath = Misc.Converter.BytesToString(value, idx, len);
                }
                else 
                {
                    currpath = null;
                }
                return true;
            }
        }
        public class Pack_GetPathsResponse : IPack
        {
            public List<string> paths;
            #region IPack 成员

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

                byte[] bs;

                buf.AddRange(BitConverter.GetBytes(paths.Count()));
                for (int i = 0; i < paths.Count(); i++)
                {
                    bs = Misc.Converter.StringToBytes(paths[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 (paths == null)
                    paths = new List<string>();
                paths.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;
                    paths.Add(s);
                }
                return true;
            }


            #endregion
        }
        public class Pack_GetPathsRequest : IPack 
        {
            public DateTime dt_begin;
            public DateTime dt_end;
            public string profilename;


            public byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();
                buf.AddRange(BitConverter.GetBytes(dt_begin.Ticks));
                buf.AddRange(BitConverter.GetBytes(dt_end.Ticks));
                if (!string.IsNullOrEmpty(profilename))
                {
                    byte[] bs = Misc.Converter.StringToBytes(profilename);
                    int len = bs.Count();
                    buf.AddRange(BitConverter.GetBytes(len));
                    buf.AddRange(bs);
                }
                else
                {
                    int len = 0;
                    buf.AddRange(BitConverter.GetBytes(len));
                }
                return buf.ToArray();
            }

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

                int idx = 0;
                dt_begin = new DateTime(BitConverter.ToInt64(value, idx));
                idx += 8;
                dt_end = new DateTime(BitConverter.ToInt64(value, idx));
                idx += 8;

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

                cnt += len;
                if (value.Length < cnt)
                    return false;
                if (len > 0)
                {
                    profilename = Misc.Converter.BytesToString(value, idx, len);
                    idx += len;
                }
                return true;
            }
        }
        public class Pack_GetRootPathResponse : IPack
        {
            public string rootpath;
            #region IPack 成员

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

                byte[] bs= Misc.Converter.StringToBytes(rootpath);
                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 l = BitConverter.ToInt32(value, idx);
                idx += 4;

                cnt += l;
                if (value.Length < cnt)
                    return false;

                rootpath = Misc.Converter.BytesToString(value, idx, l);
                idx += l;
                 
                return true;
            }


            #endregion
        }
        #endregion

        #region GetValue
        /// <summary>
        /// Pack_Params
        /// </summary>
        public const UInt16 GET_PARAMS = 0;
        /// <summary>
        /// Pack_State
        /// </summary>
        public const UInt16 GET_STATE = 1;
        #endregion
        #region SetValue
        /// <summary>
        /// Pack_Params
        /// </summary>
        public const UInt16 SET_PARAMS = 0;
        #endregion
        #region PushMsg
        /// <summary>
        /// Pack_Params
        /// </summary>
        public const UInt16 PUSH_PARAMS = 0;
        /// <summary>
        /// Pack_State
        /// </summary>
        public const UInt16 PUSH_STATE = 1;
        #endregion
        #region CALL
        /// <summary>
        /// null
        /// </summary>
        public const UInt16 CALL_FLUSH = 1;
        /// <summary>
        /// Pack_GetPathsRequest
        /// Pack_GetPathsResponse
        /// </summary>
        public const UInt16 CALL_GETPATHS = 2;

        /// <summary>
        /// null
        /// Pack_GetRootPathResponse
        /// </summary>
        public const UInt16 CALL_GETROOTPATH = 3;
        #endregion
    }
}
