using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using skyscraper5.Dvb.SystemSoftwareUpdate.Model;
using skyscraper5.Skyscraper.IO;
using skyscraper5.Dvb.SystemSoftwareUpdate.CarouselIntention;

namespace skyscraper5.Dvb.SystemSoftwareUpdate.CarouselIntention
{
    class GroupInfo
    {
        public GroupInfo(MemoryStream ms)
        {
            GroupId = ms.ReadUInt32BE();
            uint groupSize = ms.ReadUInt32BE();
            Compatibility = new CompatibilityDescriptor(ms);

            if (ms.GetAvailableBytes() < 2)
            {
                GroupInfoData = new byte[0];
                PrivateData = new byte[0];
                return;
            }

            ushort groupInfoLength = ms.ReadUInt16BE();
            if (ms.GetAvailableBytes() < groupInfoLength)
            {
                GroupInfoData = new byte[0];
                PrivateData = new byte[0];
                return;
            }
            GroupInfoData = ms.ReadBytes(groupInfoLength);

            if (ms.GetAvailableBytes() < 2)
            {
                PrivateData = new byte[0];
                return;
            }

            ushort privateDataLength = ms.ReadUInt16BE();
            PrivateData = ms.ReadBytes(privateDataLength);
        }

        public byte[] PrivateData { get; private set; }

        public byte[] GroupInfoData { get; private set; }

        public CompatibilityDescriptor Compatibility { get; private set; }

        public uint GroupId { get; private set; }
    }
}
