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

namespace skyscraper5.DsmCc
{
    class DsmCcAdaptionHeader
    {
        public DsmCcAdaptionHeader(byte[] buffer)
        {
            MemoryStream ms = new MemoryStream(buffer, false);
            AdaptionType = ms.ReadUInt8();
            byte adaptionLength = ms.ReadUInt8();
            if (adaptionLength > ms.GetAvailableBytes())
            {
                adaptionLength = 0;
                return;
            }
            if (AdaptionType == 0x01)
            {
                ConditionalAccessType = ms.ReadUInt16BE();
                adaptionLength = ms.ReadUInt8();
            }

            if (adaptionLength > ms.GetAvailableBytes())
            {
                adaptionLength = 0;
                return;
            }
            AdationData = ms.ReadBytes(adaptionLength);
        }

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

        public ushort? ConditionalAccessType { get; private set; }

        public byte AdaptionType { get; private set; }
    }
}
