using Parser.BinaryParser.Attributes;
using Parser.BinaryParser.MessageEntity;
using Parser.BinaryParser.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WayneChina_IcCardReader_SinoChem.MessageEntity
{
    public class IcCardReaderMessageBase : MessageTemplateBase
    {
        [Format(1, EncodingType.BIN, -9999)]
        public byte Prefix0xFA { get { return 0xFA; } set { } }

        [Format(1, EncodingType.BIN, -9980)]
        public byte RawMessageSeqNumberAndSourceAddress { get; set; }

        /// <summary>
        /// Gets or sets the message seq number, range must be in 0 to 15
        /// </summary>
        public byte MessageSeqNumber
        {
            get { return (byte)(this.RawMessageSeqNumberAndSourceAddress >> 4 & 0x0F); }
            set
            {
                if (value > 0x0F) throw new ArgumentOutOfRangeException("message Token max value is 0x0F");
                //E0 = 1110 0000
                this.RawMessageSeqNumberAndSourceAddress = RawMessageSeqNumberAndSourceAddress.SetBit(4, 7, value);
            }
        }

        /// <summary>
        /// Gets or sets the SourceAddress, range must be in 0 to 15
        /// </summary>
        public byte SourceAddress
        {
            get { return (byte)(this.RawMessageSeqNumberAndSourceAddress & 0x0F); }
            set
            {
                if (value > 0x0F) throw new ArgumentOutOfRangeException("message SourceAddress max value is 0x0F");
                //E0 = 1110 0000
                this.RawMessageSeqNumberAndSourceAddress = RawMessageSeqNumberAndSourceAddress.SetBit(0, 3, value);
            }
        }

        [Format(2, EncodingType.BCD, -9940)]
        public int MessageLength { get; set; }

        [EnumerableFormat("MessageLength", -9920)]
        public List<byte> RawData { get; set; }

        [EnumerableFormat(2, -9900)]
        public List<byte> CRC { get; set; }

        public override string ToLogString()
        {
            return this.GetType().Name + "  " + base.ToLogString() + "        MessageSeqNumber: " + this.MessageSeqNumber;
        }
    }
}