using System.Linq;

namespace InABox.DigitalMatter
{
    public class DMGenericBluetoothTag : DMBluetoothTag
    {
        public byte[] Data { get; set; }
        public ushort TagType { get; private set; }
        public byte[] MacAddress { get; private set; }
        public byte PayloadLength { get; private set; }
        public byte[] Payload { get; set; }

        protected override void DoDecode(IDMReadBuffer buffer)
        {
            TagType = buffer.TakeUInt16();
            MacAddress = buffer.TakeBytes(6);
            PayloadLength = buffer.TakeByte();
            Payload = buffer.TakeBytes(DataLength);
        }

        protected override void DoEncode(IDMWriteBuffer buffer)
        {
            buffer.AddUInt16(TagType);
            buffer.AddBytes(MacAddress);
            buffer.AddByte(PayloadLength);
            buffer.AddBytes(Payload);
        }

        public override string ID()
        {
            return string.Join(":", MacAddress.Reverse().Select(x => x.ToString("X2")));
            //BitConverter.ToString(MacAddress);
        }

        public override string Type()
        {
            return string.Format("Type {0}", TagType);
        }
    }
}