using skyscraper5.Mpeg2;
using skyscraper5.src.Mpeg2.PacketFilter;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace skyscraper8.Skyscraper.Math
{
    public class PidEntryCalculatorFilter : IPacketFilter
    {
        public bool PassPacket(TsPacket packet)
        {
            if (entropyCalculatorStreams == null)
                entropyCalculatorStreams = new EntropyCalculatorStream[0x1fff];

            if (packet.GetPayload() != null)
            {
                uint pid = packet.PID;
                if (entropyCalculatorStreams[pid] == null)
                    entropyCalculatorStreams[pid] = new EntropyCalculatorStream();
                entropyCalculatorStreams[pid].Write(packet.GetPayload());
            }
            return true;
        }

        private EntropyCalculatorStream[] entropyCalculatorStreams;

        public double GetEntropy(int pid)
        {
            if (pid < 0)
                throw new ArgumentOutOfRangeException(nameof(pid));

            if (pid > entropyCalculatorStreams.Length)
                throw new ArgumentOutOfRangeException(nameof(pid));

            EntropyCalculatorStream stream = entropyCalculatorStreams[pid];
            if (stream == null)
                return double.NaN;

            return stream.Entropy;
        }
    }
}
