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

namespace skyscraper5.Dvb.TvAnytime
{
    internal class Pid0x12Decoder : IPsiProcessor
    {

        public Pid0x12Decoder(IEitEventHandler eitEventHandler)
        {
            eitParser = new EitParser(eitEventHandler);
        }
        private EitParser eitParser;


        public void GatherPsi(PsiSection section, int sourcePid)
        {
            byte[] data = section.GetData();
            byte tableId = data[0];
            byte syntax = data[1];
            bool sectionSyntaxIndicator = (syntax & 0x80) != 0;
            bool privateIndicator = (syntax & 0x40) != 0;

            bool actualSchedule = ((tableId >= 0x50) && (tableId <= 0x5f));
            bool otherSchedule = ((tableId >= 0x60) && (tableId <= 0x6f));
            
            if (tableId == 0x4e || tableId == 0x4f || actualSchedule || otherSchedule)
            {
                if (sectionSyntaxIndicator)
                {
                    eitParser.GatherPsi(section, sourcePid);
                }
                return;
            }
            else if (tableId == 0x77 && sectionSyntaxIndicator && privateIndicator)
            {
                throw new NotImplementedException("CIT");   // ETSI TS 102 323 V1.5.1 (2012-01)
            }
        }
    }
}
