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

namespace InABox.DigitalMatter
{
    public class DMBluetoothTagList : DMField
    {
        private List<DMBluetoothTagListItem> _items = new();
        public override byte Type => 0x1D;

        public DMBluetoothTagListItem[] Items
        {
            get => _items.ToArray();
            set => _items = value.ToList();
        }

        protected override void DoDecode()
        {
            var bytestaken = 0;
            _items.Clear();
            while (bytestaken < Length && BufferSize > 0)
            {
                var item = new DMBluetoothTagListItem();
                item.Decode(Buffer);
                _items.Add(item);
                TakeBytes(item.DataLength + 3);
                bytestaken += item.DataLength + 3;
            }
        }

        protected override void DoEncode()
        {
            foreach (var tag in _items)
                AddBytes(tag.Encode());
        }

        public override string ToString()
        {
            return string.Join(", ", _items.Select(x => x.ToString()));
        }

        public override string Dump()
        {
            return $"Bluetooth Tags: [{string.Join(',', _items.Select(x => x.Tag))}]";
        }

        public override bool IsValid()
        {
            return _items.Any();
        }
    }
}