using log4net;
using skyscraper5.Dvb.Psi;
using skyscraper5.Dvb.Psi.Model;
using skyscraper5.Mpeg2;
using skyscraper5.Skyscraper.Plugins;
using skyscraper5.Skyscraper.Scraper.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace skyscraper5.Skyscraper.Scraper.StreamAutodetection.Contestants
{
	[SkyscraperPlugin]
	class Pid0x11Contestant : Contestant, IBatEventHandler, ISdtEventHandler, IDisposable
    {
        public Pid0x11Contestant(int pid) : base("BAT/SDT", pid)
        {
            PacketProcessor = new PsiDecoder(pid,new Pid0x11Decoder(this, this));
        }

        public void OnBatBouquet(BatBouquet batBouquet)
        {
            hasBat = true;
            CheckAchievement();
        }

        public void OnBatTransportStream(BatBouquet batBouquet, BatTransportStream child)
        {
			hasBat = true;
			CheckAchievement();
		}

        public void OnSdtService(ushort transportStreamId, ushort originalNetworkId, SdtService sdtService)
        {
			hasSdt = true;
			CheckAchievement();
		}

        public override void Dispose()
        {
            
        }

        private static readonly ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name);
		public override void DeclareWinner(SkyscraperContext skyscraperContext, int pid, ProgramContext programContext)
        {
			//FreesatTunnelScraper fts = new FreesatTunnelScraper();
			//skyscraperContext.DvbContext.RegisterPacketProcessor(pid, new PsiDecoder(pid, new Pid0x11Decoder(fts, fts)));
			logger.ErrorFormat("{0} {1} not yet implemented", nameof(Pid0x11Contestant), nameof(DeclareWinner));
			skyscraperContext.DvbContext.RegisterPacketProcessor(pid, new PacketDiscarder());
		}

		public override void Introduce(ProgramContext programContext)
		{
			
		}

		public void SetNetworkId(ushort networkId)
        {
        }

        public void SetTransportStreamId(ushort transportStreamId)
        {
        }

        private bool hasBat, hasSdt;
        private void CheckAchievement()
        {
            if (hasBat && hasSdt)
            {
                Score++;
                hasBat = false;
                hasSdt = false;
            }
        }
    }
}
