using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using skyscraper5.Skyscraper.Plugins;
using skyscraper5.T2MI;
using skyscraper5.T2MI.Packets;
using skyscraper8.T2MI.Packets;

namespace skyscraper5.Skyscraper.Scraper.StreamAutodetection.Contestants
{
	[SkyscraperPlugin]
	internal class T2MiContestant : Contestant, T2MIEventHandler
    {
        public T2MiContestant(int pid) : base("T2-MI", pid)
        {
            PacketProcessor = new T2MIDecoder(pid, this);
        }
        

        public override void Dispose()
        {
            
        }

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

        public override void Introduce(ProgramContext programContext)
        {
	        
        }

        public void OnT2MiPacketLoss(int pid, byte expectedPacket, T2MIHeader header)
        {
            if (Score > 0)
                Score = 0;
            Score--;
        }

        private bool hasPackets;
        public void OnT2MiPacket(int pid, byte basebandFramePlpId, byte[] basebandPacket)
        {
            Score++;
            hasPackets = true;
        }

        public void OnT2MiTimestamp(int pid, _0x20_DvbT2Timestamp t2Timestamp)
        {
            if (t2Timestamp.Valid && hasPackets)
                Score++;
        }

        public void OnT2MiIqData(int relatedPid, _0x31_IqData iqData)
        {
            if (hasPackets)
                Score++;
        }

        public void OnT2MiIqData(int relatedPid, _0x01_IqData iqData2)
        {
            if (hasPackets)
                Score++;
        }

        public void OnT2MiError(int relatedPid, int skyscraperErrorCode)
        {
            Score--;
        }

        public void OnT2MiL1Current(int relatedPid, _0x10_L1Current l1Current)
        {
            if (hasPackets)
                Score++;
        }

        public void OnT2MiArbitraryCellInsertion(int relatedPid, _0x02_ArbitraryCellInsertion arbitraryCellInsertion)
        {
            if (hasPackets)
                Score++;
        }

        public void OnT2MiBalancingCells(int relatedPid, byte frameIndex, uint numActiveBiasCellsPerP2)
        {
            if (hasPackets)
                Score++;
        }

        public void OnT2MiL1Future(int relatedPid, _0x11_L1Future l1Future)
        {
            if (hasPackets)
                Score++;
        }

        public void OnT2MiIndividualAddressing(int relatedPid, _0x21_IndividualAddressing individualAddressing)
        {
            if (hasPackets)
                Score++;
        }

        void T2MIEventHandler.OnFramingAndTimingInformation(int relatedPid, _0xF0_FramingTimingInformation fti)
        {
            if (fti.NumberOfPlps > 0)
                Score++;
        }
    }
}
