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

namespace FLY.Thick.Base.Common
{
    public enum MOTORTYPE
    {
        NULL = 0,
        VF0 = 1,
        SERVO = 2
    }
    public enum AREA_STATUS
    {
        /// <summary>
        /// 没有读过密码区
        /// </summary>
        NOTLOAD = 0,
        /// <summary>
        /// 读出错
        /// </summary>
        RDERR = 1,
        /// <summary>
        /// 非有效密码区 没有0xAA55 或 0x5A5A 标志
        /// </summary>
        NOTVALID = 2,
        /// <summary>
        /// 写出错
        /// </summary>
        WRERR = 3,
        /// <summary>
        /// 有效密码区
        /// </summary>
        VALID = 4,
        /// <summary>
        /// 有效密码区，应回写EEPROM
        /// </summary>
        VALID_DIRTY = 5,
        /// <summary>
        /// 有效密码区，但回写EEPROM失败
        /// </summary>
        VALID_WRERR = 6
    }

    public enum AREA_ERR
    {
        NO_ERR = 0, // 没有错误
        DUP_ACCESS = 1,//  重复使用码 
        ERR_ACCESS = 2//无效使用码
    }

    public class AccessInfo 
    {
        public AREA_STATUS status;
        public AREA_ERR ret;
        public byte[] code = new byte[7];
        public UInt16 surplus;
        public byte[] access = new byte[8];


        #region IPack 成员

        public byte[] ToBytes()
        {
            List<byte> buf = new List<byte>();
            buf.Add((byte)status);
            buf.Add((byte)ret);
            buf.AddRange(code);
            buf.AddRange(BitConverter.GetBytes(surplus));
            buf.AddRange(access);
            return buf.ToArray();
        }

        public bool TryParse(byte[] value)
        {

            if (value.Count() < 18)
                return false;
            int idx = 0;
            status = (AREA_STATUS)value[idx];
            idx++;
            ret = (AREA_ERR)value[idx];
            idx++;
            Array.Copy(value, idx, code, 0, code.Length);
            idx += code.Length;

            surplus = BitConverter.ToUInt16(value, idx);
            idx += 2;
            Array.Copy(value, idx, access, 0,access.Length);
            idx += access.Length;
            return true;
        }

        #endregion
    }

    public class GridInfo
    {
        public Misc.DIRECTION direction;
        public bool isall;//这就是全部
        public int grid_start;
        public int[] data;

        #region IPack 成员

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

            buf.AddRange(BitConverter.GetBytes((Int32)direction));
            buf.AddRange(BitConverter.GetBytes(isall));
            buf.AddRange(BitConverter.GetBytes(grid_start));
            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)
        {
            if (value.Length < (4 + 1 + 4 + 4))
                return false;
            int idx = 0;
            direction = (Misc.DIRECTION)BitConverter.ToInt32(value, idx);
            idx += 4;
            isall = BitConverter.ToBoolean(value, idx);
            idx++;
            grid_start = BitConverter.ToInt32(value, idx);
            idx += 4;
            int len = BitConverter.ToInt32(value, idx);
            idx += 4;
            if (value.Length < (4 + 1 + 4 + 4 + len * 4))
                return false;

            data = new int[len];

            for (int i = 0; i < len; i++)
            {
                data[i] = BitConverter.ToInt32(value, idx);
                idx += 4;
            }
            return true;
        }

        #endregion
    }



}
