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

namespace FLY.Thick.Base.OBJ_INTERFACE
{
    public class GAGEINFO_OBJ_INTERFACE
    {
        #region Pack
        public class Pack_State : IPack
        {
            public int progress;
            public bool ing;
            public bool dataOk;
            #region IPack 成员

            public byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();
                buf.AddRange(BitConverter.GetBytes(progress));
                buf.AddRange(BitConverter.GetBytes(ing));
                buf.AddRange(BitConverter.GetBytes(dataOk));
                return buf.ToArray();
            }

            public bool TryParse(byte[] value)
            {
                if (value.Length < 6)
                    return false;
                int idx = 0;
                progress = BitConverter.ToInt32(value, idx);
                idx += 4;
                ing = BitConverter.ToBoolean(value, idx);
                idx++;
                dataOk = BitConverter.ToBoolean(value, idx);
                idx++;
                return true;
            }

            #endregion
        }
        public class Pack_Data : IPack
        {
            public int posLen;
            public int posOfGrid;
            public int[] forwData;
            public int[] backwData;


            public byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();
                buf.AddRange(BitConverter.GetBytes(posLen));
                buf.AddRange(BitConverter.GetBytes(posOfGrid));
                
                buf.AddRange(BitConverter.GetBytes(forwData.Length));
                for (int i = 0; i < forwData.Length; i++)
                {
                    buf.AddRange(BitConverter.GetBytes(forwData[i]));
                }
                buf.AddRange(BitConverter.GetBytes(backwData.Length));
                for (int i = 0; i < backwData.Length; i++)
                {
                    buf.AddRange(BitConverter.GetBytes(backwData[i]));
                }
                return buf.ToArray();
            }

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

                len = BitConverter.ToInt32(value, idx);
                idx += 4;
                cnt += len * 4;
                if (value.Length < cnt)
                    return false;
                backwData = null;
                if (len > 0)
                {
                    backwData = new int[len];
                    for (int i = 0; i < len; i++)
                    {
                        backwData[i] = BitConverter.ToInt32(value, idx);
                        idx += 4;
                    }
                }

                return true;
            }
        }
        #endregion
        #region GetValue
        /// <summary>
        /// Pack_State
        /// </summary>
        public const UInt16 GET_STATE = 1;
        /// <summary>
        /// Pack_Data
        /// </summary>
        public const UInt16 GET_DATA = 2;

        #endregion

        #region SetValue

        #endregion

        #region CallFunction
        /// <summary>
        /// NULL
        /// </summary>
        public const UInt16 CALL_START = 1;

        /// <summary>
        /// NULL
        /// </summary>
        public const UInt16 CALL_STOP = 2;

        #endregion
        #region PushInfo

        /// <summary>
        /// Pack_State
        /// </summary>
        public const UInt16 PUSH_STATE = 1;

        /// <summary>
        /// Pack_Data
        /// </summary>
        public const UInt16 PUSH_DATA = 2;

        #endregion

    }
}
