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

namespace FLY.Thick.Base.OBJ_INTERFACE
{
    public class BORDERSEARCH_OBJ_INTERFACE
    {
        #region Pack
        public class Pack_Params : IPack
        {
            public bool enable;
            public Range valid;
            public bool tempADBySet;
            public int tempad;
            public int temprange;
            public int n;
            public int sensorwidth;
            public int n2;
            public int n3;
            public bool isBreakDetect;
            #region IPack 成员

            public byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();
                buf.AddRange(BitConverter.GetBytes(enable));
                buf.AddRange(valid.ToBytes());
                buf.AddRange(BitConverter.GetBytes(tempADBySet));
                buf.AddRange(BitConverter.GetBytes(tempad));
                buf.AddRange(BitConverter.GetBytes(temprange));
                buf.AddRange(BitConverter.GetBytes(n));
                buf.AddRange(BitConverter.GetBytes(sensorwidth));
                buf.AddRange(BitConverter.GetBytes(n2));
                buf.AddRange(BitConverter.GetBytes(n3));
                buf.AddRange(BitConverter.GetBytes(isBreakDetect));
                return buf.ToArray();
            }

            public bool TryParse(byte[] value)
            {
                if (value.Length < (1+8+1+4*6+1))
                    return false;
                int idx = 0;
                enable = BitConverter.ToBoolean(value, idx);
                idx += 1;
                valid = new Range();
                valid.TryParse(value, idx);
                idx += 8;
                tempADBySet = BitConverter.ToBoolean(value, idx);
                idx += 1;
                tempad = BitConverter.ToInt32(value, idx);
                idx += 4;
                temprange = BitConverter.ToInt32(value, idx);
                idx += 4;
                n = BitConverter.ToInt32(value, idx);
                idx += 4;
                sensorwidth = BitConverter.ToInt32(value, idx);
                idx += 4;
                n2 = BitConverter.ToInt32(value, idx);
                idx += 4;
                n3 = BitConverter.ToInt32(value, idx);
                idx += 4;
                isBreakDetect = BitConverter.ToBoolean(value, idx);
                idx += 1;
                return true;
            }

            #endregion
        }
        public class Pack_Result : IPack
        {
            public Range borderForw;
            public Range borderBackw;
            public int width;
            public int mid;
            #region IPack 成员

            public byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();
                buf.AddRange(borderForw.ToBytes());
                buf.AddRange(borderBackw.ToBytes());
                buf.AddRange(BitConverter.GetBytes(width));
                buf.AddRange(BitConverter.GetBytes(mid));
                return buf.ToArray();
            }

            public bool TryParse(byte[] value)
            {
                if (value.Length < 8+8+4+4)
                    return false;
                int idx = 0;
                borderForw = new Range();
                borderForw.TryParse(value, 0);
                idx += 8;
                borderBackw = new Range();
                borderBackw.TryParse(value, idx);
                idx += 8;
                width = BitConverter.ToInt32(value, idx);
                idx += 4;
                mid = BitConverter.ToInt32(value, idx);
                idx += 4;
                return true;
            }

            #endregion
        }

        public class Pack_Data : IPack
        {
            public int[] data;

            public byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();
                if (data == null)
                {
                    buf.AddRange(BitConverter.GetBytes(0));
                }
                else
                {
                    buf.AddRange(BitConverter.GetBytes(data.Length));
                    for (int i = 0; i < data.Length; i++)
                    {
                        buf.AddRange(BitConverter.GetBytes(data[i]));
                    }
                }
                return buf.ToArray();
            }

            public bool TryParse(byte[] value)
            {
                int cnt = 4;
                int index = 0;
                if (value.Length < (index + cnt))
                    return false;
                int idx = index;
                int len = BitConverter.ToInt32(value, idx);
                idx += 4;
                cnt += len * 4;
                if (value.Length < (index + cnt))
                    return false;
                if (len <= 0)
                    data = null;
                else
                {
                    data = new int[len];
                    for (int i = 0; i < len; i++)
                    {
                        data[i] = BitConverter.ToInt32(value, idx);
                        idx += 4;
                    }
                }

                return true;
            }
        }
        #endregion
        #region GetValue
        /// <summary>
        /// Pack_Params
        /// </summary>
        public const UInt16 GET_PARAMS = 0;
        /// <summary>
        /// Pack_Result
        /// </summary>
        public const UInt16 GET_RESULT = 1;
        /// <summary>
        /// Pack_Data
        /// </summary>
        public const UInt16 GET_DATAS = 2;
        #endregion
        #region SetValue
        /// <summary>
        /// Pack_GetParam
        /// </summary>
        public const UInt16 SET_PARAMS = 0;
        #endregion
        #region PushMsg
        /// <summary>
        /// Pack_GetParam
        /// </summary>
        public const UInt16 PUSH_PARAMS = 0;
        /// <summary>
        /// Pack_GetResult
        /// </summary>
        public const UInt16 PUSH_RESULT = 1;
        /// <summary>
        /// Pack_Data
        /// </summary>
        public const UInt16 PUSH_DATAS = 2;
        #endregion
    }
}
