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

namespace skyscraper5.DsmCc.Descriptors
{
	[SkyscraperPlugin]
	[TsDescriptor(0x14,"PMT")]
    [BannedTable("TSDT")]
    class AssociationTagDescriptor : TsDescriptor
    {
        public AssociationTagDescriptor(byte[] buffer)
        {
            MemoryStream ms = new MemoryStream(buffer, false);
            Use = ms.ReadUInt16BE();
            SelectorLength = ms.ReadUInt8();
            if (Use == 0x0000)
            {
                TransactionId = ms.ReadUInt32BE();
                TimeOut = ms.ReadUInt32BE();
            }
            else if (Use == 0x0001)
            {
                if (SelectorLength != 0x00)
                    throw new DsmCcException("invalid {0}", nameof(AssociationTagDescriptor));
            }
            else
            {
                Selector = ms.ReadBytes(SelectorLength);
            }

            PrivateData = ms.ReadBytes(ms.GetAvailableBytes());
        }

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

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

        public uint? TimeOut { get; private set; }

        public uint? TransactionId { get; private set; }

        public byte SelectorLength { get; private set; }

        public ushort Use { get; private set; }
    }
}
