using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using skyscraper5.Skyscraper.IO;

namespace skyscraper5.Mheg5
{
    class Mheg5DataBroadcastIdSelector
    {
        public Mheg5DataBroadcastIdSelector(byte[] buffer)
        {
            MemoryStream ms = new MemoryStream(buffer, false);
            List<Application> applications = new List<Application>();
            while (ms.GetAvailableBytes() >= 4)
            {
                Application child = new Application();
                child.ApplicationTypeCode = ms.ReadUInt16BE();
                child.BootPriorityHint = ms.ReadUInt8();
                byte applicationSpecificDataLength = ms.ReadUInt8();
                child.ApplicationSpecificData = ms.ReadBytes(applicationSpecificDataLength);
                applications.Add(child);
            }

            this.Applications = applications.AsReadOnly();
        }

        public ReadOnlyCollection<Application> Applications { get; private set; }

        public class Application
        {
            public ushort ApplicationTypeCode { get; set; }
            public byte BootPriorityHint { get; set; }
            public byte[] ApplicationSpecificData { get; set; }
        }
    }
}
