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

namespace skyscraper5.Skyscraper.Scraper.StreamAutodetection.Contestants
{
	[SkyscraperPlugin]
	class EitContestant : Contestant, IEitEventHandler, IDisposable
    {
        public EitContestant(int pid) : base("EIT", pid)
        {
            PacketProcessor = new PsiDecoder(pid, new EitParser(this));
        }

        public void SetTransportStreamId(ushort transportStreamId)
        {
        }

        public void SetNetworkId(ushort networkId, bool forceOverwrite = false)
        {
        }

        public void OnEitEvent(EitEvent eitEvent)
        {
            if (string.IsNullOrEmpty(eitEvent.EventName))
                return;

            char[] charArray = eitEvent.EventName.ToCharArray();
            int required = charArray.Length / 2;
            int found = 0;
            foreach (char c in charArray)
                if (c > 32 && c < 126)
                    found++;

            if (found > required)
                Score++;
        }

        public override void Dispose()
        {
            
        }

        public override void DeclareWinner(SkyscraperContext skyscraperContext, int pid, ProgramContext programContext)
        {
			skyscraperContext.DvbContext.RegisterPacketProcessor(pid, new PsiDecoder(pid, new EitParser(skyscraperContext)));
		}

        public override void Introduce(ProgramContext programContext)
        {
	        
        }
    }
}
