using DVBServices;
using skyscraper5.Mpeg2;
using skyscraper5.Skyscraper.Plugins;
using skyscraper5.Skyscraper.Scraper;
using skyscraper5.Skyscraper.Scraper.StreamAutodetection;
using skyscraper8.EPGCollectorPort.SkyscraperSide.MediaHighway2;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace skyscraper8.EPGCollectorPort.SkyscraperSide.OpenTV
{
	////Disabled due to the Huffmann Table being dynamically determined.
	//[SkyscraperPlugin]
	internal class OpenTvSummaryContestant : Contestant, OpenTvHandler
	{
		public OpenTvSummaryContestant(int pid)
			: base("OpenTV Summaries", pid)
		{
			PacketProcessor = new PsiDecoder(pid, new OpenTvSummaryParser(this));
		}

		public override void Dispose()
		{

		}

		public override void DeclareWinner(SkyscraperContext skyscraperContext, int pid, ProgramContext programContext)
		{
			OpenTvScraper openTvScraper = skyscraperContext.PluginContext.FirstOrDefault(x => x is OpenTvScraper) as OpenTvScraper;
			if (openTvScraper == null)
			{
				openTvScraper = new OpenTvScraper(skyscraperContext);
				skyscraperContext.PluginContext.Add(openTvScraper);
			}

			skyscraperContext.DvbContext.RegisterPacketProcessor(pid, new PsiDecoder(pid, new OpenTvSummaryParser(openTvScraper)));
		}

		public override void Introduce(ProgramContext programContext)
		{

		}

		private byte[] nonTrafficMarkers;
		public void OnNonOpenTvTraffic(int sourcePid, OpenTvExpectedDataType titles, int sectionTableId)
		{
			if (nonTrafficMarkers == null)
				nonTrafficMarkers = new byte[byte.MaxValue];

			nonTrafficMarkers[sectionTableId]++;
			if (nonTrafficMarkers[sectionTableId] == 1)
				Score++;
		}

		public void OnTitles(int sourcePid, int sectionTableId, OpenTVTitleSection titleSection)
		{
			throw new NotImplementedException();
		}

		public void OnSummaries(int sourcePid, int sectionTableId, OpenTVSummarySection summarySection)
		{
			Score++;
		}
	}
}
