using skyscraper5.Dvb;
using skyscraper5.Mpeg2;
using skyscraper5.PrivateDataSpecifiers.SimpliTV;
using skyscraper5.Skyscraper;
using skyscraper5.Skyscraper.IO;
using skyscraper5.Skyscraper.Plugins;

namespace skyscraper5.SimpliTV.Descriptors
{
    [SkyscraperPlugin]
    [UserDefinedDescriptor(0x1b0,0x83,"BAT")]
    [DescriptorPluginBatHandler(typeof(BatHook))]
    class NorDigPrivateLogicalChannelDescriptor : TsDescriptor
    {
        public NorDigPrivateLogicalChannelDescriptor(byte[] buffer)
        {
            LogicalChannels = new LogicalChannelNumberEntry[buffer.Length / 4];
            MemoryStream ms = new MemoryStream(buffer, false);
            for (int i = 0; i < LogicalChannels.Length; i++)
            {
                LogicalChannelNumberEntry lcn = new LogicalChannelNumberEntry();
                LogicalChannels[i] = lcn;

                lcn.ServiceId = ms.ReadUInt16BE();
                ushort readUInt16Be = ms.ReadUInt16BE();
                lcn.VisibleServiceFlag = (readUInt16Be & 0x8000) != 0;
                lcn.LogicalChannelNumber = (readUInt16Be & 0x3fff);
            }
        }

        public LogicalChannelNumberEntry[] LogicalChannels { get; private set; }

        public class LogicalChannelNumberEntry : BaseLcn
        {
            public bool VisibleServiceFlag { get; set; }
        }
    }
}
