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

namespace FLY.Thick.Base.OBJ_INTERFACE
{
    public class BOLTCREATEINFO_OBJ_INTERFACE
    {
        public const UInt32 ID = OBJ_INTERFACE_ID.BOLTCREATEINFO_ID;
        #region Pack
        public class Pack_CustomBolt:IPack 
        {
            public int no;
            public int position;

            /// <summary>
            /// 转换为8个bytes
            /// </summary>
            /// <returns></returns>
            public byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();

                buf.AddRange(BitConverter.GetBytes(no));
                buf.AddRange(BitConverter.GetBytes(position));
                
                return buf.ToArray();
            }
            /// <summary>
            /// 由8个bytes 转换为 CustomBolt
            /// </summary>
            /// <param name="value"></param>
            /// <returns></returns>
            public bool TryParse(byte[] value)
            {
                return TryParse(value, 0);
            }
            public bool TryParse(byte[] value, int index) 
            {
                if ((value.Length-index) < 8)
                    return false;
                int idx = index;
                no = BitConverter.ToInt32(value, idx);
                idx += 4;
                position = BitConverter.ToInt32(value, idx);
                idx += 4;
                return true;
            }
        }
        public class Pack_Params : IPack
        {
            public int poslength;
            public int boltwidth;
            public int boltNo1st;
            public int boltCnt;
            public Pack_CustomBolt[] custom;

            #region IPack 成员

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

                buf.AddRange(BitConverter.GetBytes(poslength));
                buf.AddRange(BitConverter.GetBytes(boltwidth));
                buf.AddRange(BitConverter.GetBytes(boltNo1st));
                buf.AddRange(BitConverter.GetBytes(boltCnt));
                if (custom != null)
                {
                    buf.AddRange(BitConverter.GetBytes(custom.Length));
                    for (int i = 0; i < custom.Length; i++)
                        buf.AddRange(custom[i].ToBytes());
                }
                else 
                {
                    int len = 0;
                    buf.AddRange(BitConverter.GetBytes(len));
                }
                return buf.ToArray();
            }

            public bool TryParse(byte[] value)
            {
                if (value.Length < 20)
                    return false;
                int idx = 0;
                poslength = BitConverter.ToInt32(value, idx);
                idx += 4;
                boltwidth = BitConverter.ToInt32(value, idx);
                idx += 4;
                boltNo1st = BitConverter.ToInt32(value, idx);
                idx += 4;

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

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

                if (value.Length < (20 + len * 8))
                    return false;

                custom = new Pack_CustomBolt[len];
                for (int i = 0; i < len; i++)
                { 
                    custom[i]=new Pack_CustomBolt();
                    custom[i].TryParse(value, idx);
                    idx+=8;
                }
                return true;
            }
            
            #endregion
        }
        #endregion

        #region GetValue
        /// <summary>
        /// Pack_Params
        /// </summary>
        public const UInt16 GET_PARAMS = 0;
        #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;
        #endregion
    }
}
