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

namespace skyscraper5.Dvb.Descriptors
{
	[SkyscraperPlugin]
	[TsDescriptor(0x56,"PMT")]
    [BannedTable("TSDT","EIT")]
    public class TeletextDescriptor : TsDescriptor
    {
        public TeletextDescriptor(byte[] buffer)
        {
            if (buffer.Length % 5 != 0)
                throw new DvbDescriptorBrokenException("Teletext descriptor length not divisible by 5!");

            Teletexts = new TeletextDescriptorTeletext[buffer.Length / 5];
            for (int i = 0; i < Teletexts.Length; i++)
            {
                Teletexts[i] = new TeletextDescriptorTeletext();
                Teletexts[i].Iso639LanguageCode = Encoding.ASCII.GetString(buffer, i * 5, 3);
                Teletexts[i].TeletextType = (TeletextType)(buffer[(i * 5) + 1] & 0xf8 >> 3);
                Teletexts[i].TeletextMagazineNumber = (buffer[(i * 5) + 1] & 0x07);
                Teletexts[i].TeletextPageNumber = buffer[(i * 5) + 2];
            }
        }

        public class TeletextDescriptorTeletext
        {
            public string Iso639LanguageCode { get; internal set; }
            public TeletextType TeletextType { get; internal set; }
            public int TeletextMagazineNumber { get; internal set; }
            public byte TeletextPageNumber { get; internal set; }
        }

        public enum TeletextType
        {
            Reserved = 0,
            InitialTeletextPage = 1,
            TeletextSubtitlePage = 2,
            AdditionalInformationPage = 3,
            ProgrammeSchedulePage = 4,
            TeletextSubtitlePageForHearingImpairedPeople = 5
        }

        public TeletextDescriptorTeletext[] Teletexts { get; private set; }
    }
}
