using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using FObjBase;
using FLY.Thick.Base.Common;

namespace FLY.Thick.Base.OBJ_INTERFACE
{
    public class FLYAD_OBJ_INTERFACE
    {
        public const UInt32 ID = OBJ_INTERFACE_ID.FLYAD_ID;
        #region Pack
        public class Pack_Params : IPack
        {
            public IPEndPoint ep;
            
            public int poslen;
            public MOTORTYPE motortype;
            public UInt16 posofgrid;
            public UInt16 ratio01;
            public UInt16 ratio02;
            public Int16 posoffset;
            public UInt32 jogvelocity;
            public bool hasCRC;
            public int gridsmooth;
            #region IPack 成员

            public byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();
                byte[] bs = Misc.Converter.IPEndPoint2Bytes(ep);
                buf.AddRange(BitConverter.GetBytes(bs.Length));
                buf.AddRange(bs);

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

                buf.AddRange(BitConverter.GetBytes((UInt16)motortype));
                buf.AddRange(BitConverter.GetBytes(posofgrid));
                buf.AddRange(BitConverter.GetBytes(ratio01));
                buf.AddRange(BitConverter.GetBytes(ratio02));
                buf.AddRange(BitConverter.GetBytes(posoffset));

                buf.AddRange(BitConverter.GetBytes(jogvelocity));
                buf.AddRange(BitConverter.GetBytes(hasCRC));
                buf.AddRange(BitConverter.GetBytes(gridsmooth));
                return buf.ToArray();
            }

            public bool TryParse(byte[] value)
            {
                int cnt = 4 + 4 + 2 * 5 + 4 + 1 + 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;
                ep = Misc.Converter.Bytes2IPEndPoint(value, idx, len);
                idx += len;

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

                motortype = (MOTORTYPE)BitConverter.ToUInt16(value, idx);
                idx += 2;
                posofgrid = BitConverter.ToUInt16(value, idx);
                idx += 2;
                ratio01 = BitConverter.ToUInt16(value, idx);
                idx += 2;
                ratio02 = BitConverter.ToUInt16(value, idx);
                idx += 2;
                posoffset = BitConverter.ToInt16(value, idx);
                idx += 2;
                jogvelocity = BitConverter.ToUInt32(value, idx);
                idx += 4;
                hasCRC = BitConverter.ToBoolean(value, idx);
                idx++;
                gridsmooth = BitConverter.ToInt32(value, idx);
                idx++;
                return true;
            }

            #endregion
        }

        public class Pack_SetAccessRequest : IPack
        {
            public byte[] data;

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

                buf.AddRange(BitConverter.GetBytes(data.Length));
                buf.AddRange(data);

                return buf.ToArray();
            }

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

                int idx = 0;
                int len = BitConverter.ToInt32(value, idx);
                idx += 4;
                if (value.Length < 4 + len)
                    return false;
                data = new byte[len];
                Array.Copy(value, idx, data, 0, len);
                idx += len;
                return true;
            }
        }
        public class Pack_GetGridRequest : IPack
        {
            public Misc.DIRECTION direction;
            public byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();

                buf.AddRange(BitConverter.GetBytes((int)direction));

                return buf.ToArray();
            }

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

                int idx = 0;
                direction = (Misc.DIRECTION)BitConverter.ToInt32(value, idx);
                idx += 4;
                return true;
            }
        }
        #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
        #region CallFunction
        /// <summary>
        /// request:null
        /// reponse:AccessInfo
        /// </summary>
        public const UInt16 CALL_GETACCESSINFO = 8;
        /// <summary>
        /// request:Pack_SetAccessRequest
        /// reponse:AccessInfo
        /// </summary>
        public const UInt16 CALL_SETACCESS = 9;
        /// <summary>
        /// request:Pack_GetGridReqest
        /// reponse:GridInfo
        /// </summary>
        public const UInt16 CALL_GETGRID = 10;

        #endregion
    }
}
