using System.IO;
using skyscraper5.Mpeg2;
using skyscraper5.Skyscraper.Scraper;
using skyscraper5.Skyscraper.Scraper.Storage.InMemory;

namespace skyscraper5.Abertis
{
    internal class AbertisDemoProgram : AbertisDecoderEventHandler
    {
        public AbertisDemoProgram(FileInfo fi)
        {
            this.fi = fi;
        }

        private FileInfo fi;

        public void Run()
        {
            TsContext ts = new TsContext();
            AbertisDecoder decoder = new AbertisDecoder(0x02be,this);
            ts.RegisterPacketProcessor(0x02be, decoder);

            InMemoryScraperStorage scraper = new InMemoryScraperStorage();
            SkyscraperContext context = new SkyscraperContext(ts, scraper);
            FileStream fileStream = fi.OpenRead();
            context.IngestFromStream(fileStream);
        }

        private FileStream outStream;
        public void OnAbertisPacket(int pid, byte[] outBuffer)
        {
            if (outStream == null)
                outStream = File.OpenWrite("abertis2.ts");
            outStream.Write(outBuffer, 0, 188);
        }

        public void OnAbertisSyncLoss(int pid, int oldPosition = -1, long newPosition = -1)
        {
        }
    }
}
