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

namespace FLY.Thick.Base.Common
{
    public class PasswordCell:Misc.ISaveToXml
    {
        static PasswordCell()
        {
            Misc.SaveToXmlHepler.Regist(typeof(PasswordCell));
        }
        /// <summary>
        /// 密码
        /// </summary>
        string password;
        public string Password 
        {
            get { return password; }
            set {
                if (password != value) 
                {
                    password = value;
                }
            }
        }
        /// <summary>
        /// 权限， 数值越大，权限越大。
        /// </summary>
         int level;
        public int Level
        {
            get { return level; }
            set
            {
                if (level != value)
                {
                    level = value;
                }
            }
        }
        public byte[] ToBytes()
        {
            List<byte> buf = new List<byte>();
            byte[] bs = Misc.Converter.StringToBytes(password);

            buf.AddRange(BitConverter.GetBytes(bs.Length));
            buf.AddRange(bs);
            buf.AddRange(BitConverter.GetBytes(level));
            return buf.ToArray();
        }

        public bool TryParse(byte[] value, int index, out int count)
        {
            count = 8;
            if (value.Length-index < count)
                return false;
            int idx = index;
            int len = BitConverter.ToInt32(value, idx);
            idx += 4;
            count += len;
            if (value.Length - index < count)
                return false;
            password = Misc.Converter.BytesToString(value, idx, len);
            idx += len;
            level = BitConverter.ToInt32(value, idx);
            idx += 4;
            return true;
        }

        public string[] GetSavePropertyNames()
        {
            return new string[]{
                "Password",
                "Level"};
            
        }
    }
}
