using System;
using System.Collections.Generic;
using System.Text;
using Misc;


namespace FLY.Thick.Base.Common
{
    public class ProfileParam 
    {
        public string pname;
        public int target;
        public int alarm;
        public double comp;
        public int shift;
        public int beginNo;
        public int endNo;
        public int dataBeginNo;
        public int dataEndNo;

        #region IPack 成员

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

            buf.AddRange(BitConverter.GetBytes(target));
            buf.AddRange(BitConverter.GetBytes(alarm));

            buf.AddRange(BitConverter.GetBytes(comp));
            buf.AddRange(BitConverter.GetBytes(shift));
            buf.AddRange(BitConverter.GetBytes(beginNo));
            buf.AddRange(BitConverter.GetBytes(endNo));

            buf.AddRange(BitConverter.GetBytes(dataBeginNo));
            buf.AddRange(BitConverter.GetBytes(dataEndNo));
            return buf.ToArray();
        }

        public bool TryParse(byte[] value)
        {
            int cnt = 4 + 4 * 2 + 8 + 4 * 5;
            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;

            pname = Misc.Converter.BytesToString(value, idx, len);
            idx += len;

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

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


            comp = BitConverter.ToDouble(value, idx);
            idx += 8;

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


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

            dataBeginNo = BitConverter.ToInt32(value, idx);
            idx += 4;
            dataEndNo = BitConverter.ToInt32(value, idx);
            idx += 4;
            return true;
        }

        #endregion
    }
}
