using System;
using skyscraper5.Mpeg2;
using skyscraper5.Rds.Messages;

namespace skyscraper5.Rds
{
    class RdsPesProcessor : IPesProcessor
    {
        public RdsPesProcessor(int _sourcePid, int _programNumber, RdsEventHandler _eventHandler)
        {
            programNumber = _programNumber;
            decoder = new RdsDecoder();
            sourcePid = _sourcePid;
            eventHandler = _eventHandler;
        }
        private RdsDecoder decoder;
        private int sourcePid;
        private RdsEventHandler eventHandler;
        private int programNumber;
        private bool gotMessages;

        public void ProcessPesPacket(PesPacket packet)
        {
            if (packet.Payload == null)
                return;
            RdsMessage rdsMessage = decoder.DecodeRdsMessage(packet.Payload);
            if (rdsMessage == null)
                return;
            if (!gotMessages)
            {
                if (eventHandler.TestCanHandleRdsMessages(programNumber))
                    gotMessages = true;
                else
                    return;
            }
            
            string rdsMessageType = rdsMessage.GetType().Name;
            switch (rdsMessageType)
            {
                case nameof(OdaDataCommand):
                    OdaDataCommand odc = (OdaDataCommand)rdsMessage;
                    //ProcessOdaDataCommand(odc);
                    //File.WriteAllBytes(String.Format("odaData_{0:X4}_{1:X16}.bin", sourcePid, packet.PTS), odc.OdaData);
                    break;
                case nameof(OdaConfigurationAndShortMessageCommand):
                    //no clue what to do with these...
                    OdaConfigurationAndShortMessageCommand ocasmc = (OdaConfigurationAndShortMessageCommand)rdsMessage;
                    //File.WriteAllBytes(String.Format("odaShortMessage_{0:X4}_{1:X16}.bin", sourcePid, packet.PTS), ocasmc.Message);
                    break;
                case nameof(PS):
                    eventHandler.SetRdsProgrammeServiceName(programNumber,((PS)rdsMessage).ProgrammeService2);
                    break;
                case nameof(RT):
                    eventHandler.SetRdsRadioText(programNumber, ((RT)rdsMessage).Text);
                    break;
                case nameof(OdaFreeFormatGroup):
                    //no clue what to do with this...
                    OdaFreeFormatGroup offg = (OdaFreeFormatGroup)rdsMessage;
                    //File.WriteAllBytes(String.Format("odaFreeFormat_{0:X4}_{1:X16}.bin", sourcePid, packet.PTS), offg.Blocks);
                    break;
                case nameof(RealTimeClock):
                    eventHandler.SetCurrentTime(((RealTimeClock)rdsMessage).Timestamp);
                    break;
                case nameof(PTY):
                    eventHandler.SetCurrentProgrammeType(programNumber,((PTY)rdsMessage).Pty);
                    break;
                case nameof(TA_TP):
                    TA_TP tatp = (TA_TP)rdsMessage;
                    if (tatp.TP)
                        eventHandler.MarkAsRdsTrafficInformationProgramme(programNumber);
                    break;
                default:
                    throw new NotImplementedException(rdsMessageType);
            }
        }
    }
}
