///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2019-2023 Edouard Griffiths, F4EXB <f4exb06@gmail.com>          //
// Copyright (C) 2022 Jiří Pinkava <jiri.pinkava@rossum.ai>                      //
//                                                                               //
// This program is free software; you can redistribute it and/or modify          //
// it under the terms of the GNU General Public License as published by          //
// the Free Software Foundation as version 3 of the License, or                  //
// (at your option) any later version.                                           //
//                                                                               //
// This program is distributed in the hope that it will be useful,               //
// but WITHOUT ANY WARRANTY; without even the implied warranty of                //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the                  //
// GNU General Public License V3 for more details.                               //
//                                                                               //
// You should have received a copy of the GNU General Public License             //
// along with this program. If not, see <http://www.gnu.org/licenses/>.          //
///////////////////////////////////////////////////////////////////////////////////

#include <QDebug>
#include <QThread>
#include <algorithm>

#include "dsp/dspcommands.h"
#include "dsp/spectrumvis.h"
#include "maincore.h"

#include "ft8demodworker.h"
#include "pskreporterworker.h"
#include "ft8demodbaseband.h"

MESSAGE_CLASS_DEFINITION(FT8DemodBaseband::MsgConfigureFT8DemodBaseband, Message)

FT8DemodBaseband::FT8DemodBaseband() :
    m_channelizer(&m_sink),
    m_messageQueueToGUI(nullptr),
    m_spectrumVis(nullptr),
    m_lastProcessPeriodIndex(-1),
    m_deviceCenterFrequency(0)
{
    qDebug("FT8DemodBaseband::FT8DemodBaseband");
    m_sampleFifo.setSize(SampleSinkFifo::getSizePolicy(48000));
    m_ft8WorkerBuffer = new int16_t[FT8DemodSettings::m_ft8SampleRate*15];

    m_workerThread = new QThread();
    m_ft8DemodWorker = new FT8DemodWorker();
    m_ft8DemodWorker->moveToThread(m_workerThread);

    QObject::connect(
        m_workerThread,
        &QThread::finished,
        m_ft8DemodWorker,
        &QObject::deleteLater
    );

    QObject::connect(
        this,
        &FT8DemodBaseband::bufferReady,
        m_ft8DemodWorker,
        &FT8DemodWorker::processBuffer,
        Qt::QueuedConnection
    );

    m_workerThread->start();

    m_pskReporterThread = new QThread();
    m_pskReporterWorker = new PskReporterWorker();
    m_ft8DemodWorker->setPSKReportingMessageQueue(m_pskReporterWorker->getInputMessageQueue());
    m_pskReporterWorker->moveToThread(m_pskReporterThread);

    QObject::connect(
        m_pskReporterThread,
        &QThread::finished,
        m_pskReporterWorker,
        &QObject::deleteLater
    );

    m_pskReporterThread->start();

    QObject::connect(
        &m_sampleFifo,
        &SampleSinkFifo::dataReady,
        this,
        &FT8DemodBaseband::handleData,
        Qt::QueuedConnection
    );

    m_channelSampleRate = 0;
    m_sink.setFT8Buffer(&m_ft8Buffer);

    connect(&m_inputMessageQueue, SIGNAL(messageEnqueued()), this, SLOT(handleInputMessages()));
    connect(&MainCore::instance()->getMasterTimer(), SIGNAL(timeout()), this, SLOT(tick()));
}

FT8DemodBaseband::~FT8DemodBaseband()
{
    m_workerThread->exit();
	m_workerThread->wait();
    delete[] m_ft8WorkerBuffer;
    m_pskReporterThread->exit();
    m_pskReporterThread->wait();
}

void FT8DemodBaseband::reset()
{
    QMutexLocker mutexLocker(&m_mutex);
    m_sampleFifo.reset();
    m_channelSampleRate = 0;
    m_lastProcessPeriodIndex = -1;
}

void FT8DemodBaseband::setMessageQueueToGUI(MessageQueue *messageQueue)
{
    m_messageQueueToGUI = messageQueue;
    m_ft8DemodWorker->setGUIReportingMessageQueue(m_messageQueueToGUI);
}

void FT8DemodBaseband::setChannel(ChannelAPI *channel)
{
    m_sink.setChannel(channel);
    m_ft8DemodWorker->setChannel(channel);
}

void FT8DemodBaseband::feed(const SampleVector::const_iterator& begin, const SampleVector::const_iterator& end)
{
    m_sampleFifo.write(begin, end);
}

void FT8DemodBaseband::handleData()
{
    QMutexLocker mutexLocker(&m_mutex);

    while ((m_sampleFifo.fill() > 0) && (m_inputMessageQueue.size() == 0))
    {
		SampleVector::iterator part1begin;
		SampleVector::iterator part1end;
		SampleVector::iterator part2begin;
		SampleVector::iterator part2end;

        std::size_t count = m_sampleFifo.readBegin(m_sampleFifo.fill(), &part1begin, &part1end, &part2begin, &part2end);

		// first part of FIFO data
        if (part1begin != part1end) {
            m_channelizer.feed(part1begin, part1end);
        }

		// second part of FIFO data (used when block wraps around)
		if(part2begin != part2end) {
            m_channelizer.feed(part2begin, part2end);
        }

		m_sampleFifo.readCommit((unsigned int) count);
    }

    qreal rmsLevel, peakLevel;
    int numSamples;
    m_sink.getLevels(rmsLevel, peakLevel, numSamples);
    emit levelChanged(rmsLevel, peakLevel, numSamples);
}

void FT8DemodBaseband::handleInputMessages()
{
	Message* message;

	while ((message = m_inputMessageQueue.pop()) != nullptr)
	{
		if (handleMessage(*message)) {
			delete message;
		}
	}
}

bool FT8DemodBaseband::handleMessage(const Message& cmd)
{
    if (MsgConfigureFT8DemodBaseband::match(cmd))
    {
        QMutexLocker mutexLocker(&m_mutex);
        MsgConfigureFT8DemodBaseband& cfg = (MsgConfigureFT8DemodBaseband&) cmd;
        qDebug() << "FT8DemodBaseband::handleMessage: MsgConfigureFT8DemodBaseband";

        applySettings(cfg.getSettingsKeys(), cfg.getSettings(), cfg.getForce());

        return true;
    }
    else if (DSPSignalNotification::match(cmd))
    {
        QMutexLocker mutexLocker(&m_mutex);
        DSPSignalNotification& notif = (DSPSignalNotification&) cmd;
        qDebug() << "FT8DemodBaseband::handleMessage: DSPSignalNotification: basebandSampleRate: " << notif.getSampleRate();
        m_sampleFifo.setSize(SampleSinkFifo::getSizePolicy(notif.getSampleRate()));
        m_channelizer.setBasebandSampleRate(notif.getSampleRate());
        m_sink.applyChannelSettings(m_channelizer.getChannelSampleRate(), m_channelizer.getChannelFrequencyOffset());

        if (m_channelSampleRate != m_channelizer.getChannelSampleRate())
        {
            m_sink.applyFT8SampleRate(); // reapply when channel sample rate changes
            m_channelSampleRate = m_channelizer.getChannelSampleRate();
        }

        if (notif.getCenterFrequency() != m_deviceCenterFrequency)
        {
            m_ft8DemodWorker->invalidateSequence();
            m_deviceCenterFrequency = notif.getCenterFrequency();
            m_ft8DemodWorker->setBaseFrequency(m_deviceCenterFrequency + m_settings.m_inputFrequencyOffset);
        }

		return true;
    }
    else
    {
        return false;
    }
}

void FT8DemodBaseband::applySettings(const QStringList& settingsKeys, const FT8DemodSettings& settings, bool force)
{
    if ((settingsKeys.contains("inputFrequencyOffset") && (settings.m_inputFrequencyOffset != m_settings.m_inputFrequencyOffset)) || force)
    {
        m_ft8DemodWorker->invalidateSequence();
        m_ft8DemodWorker->setBaseFrequency(m_deviceCenterFrequency + settings.m_inputFrequencyOffset);
        m_channelizer.setChannelization(FT8DemodSettings::m_ft8SampleRate, settings.m_inputFrequencyOffset);
        m_sink.applyChannelSettings(m_channelizer.getChannelSampleRate(), m_channelizer.getChannelFrequencyOffset());

        if (m_channelSampleRate != m_channelizer.getChannelSampleRate())
        {
            m_sink.applyFT8SampleRate(); // reapply when channel sample rate changes
            m_channelSampleRate = m_channelizer.getChannelSampleRate();
        }
    }

    if ((settings.m_filterBank[settings.m_filterIndex].m_spanLog2 != m_settings.m_filterBank[settings.m_filterIndex].m_spanLog2) || force)
    {
        if (m_spectrumVis)
        {
            DSPSignalNotification *msg = new DSPSignalNotification(FT8DemodSettings::m_ft8SampleRate/(1<<settings.m_filterBank[settings.m_filterIndex].m_spanLog2), 0);
            m_spectrumVis->getInputMessageQueue()->push(msg);
        }
    }

    if ((m_settings.m_filterBank[m_settings.m_filterIndex].m_lowCutoff != settings.m_filterBank[settings.m_filterIndex].m_lowCutoff) || force) {
        m_ft8DemodWorker->setLowFrequency(settings.m_filterBank[settings.m_filterIndex].m_lowCutoff);
    }

    if ((m_settings.m_filterBank[m_settings.m_filterIndex].m_rfBandwidth != settings.m_filterBank[settings.m_filterIndex].m_rfBandwidth) || force) {
        m_ft8DemodWorker->setHighFrequency(settings.m_filterBank[settings.m_filterIndex].m_rfBandwidth);
    }

    if ((settingsKeys.contains("recordWav") && (settings.m_recordWav != m_settings.m_recordWav)) || force) {
        m_ft8DemodWorker->setRecordSamples(settings.m_recordWav);
    }

    if ((settingsKeys.contains("logMessages") && (settings.m_logMessages != m_settings.m_logMessages)) || force) {
        m_ft8DemodWorker->setLogMessages(settings.m_logMessages);
    }

    if ((settingsKeys.contains("enablePSKReporter") && (settings.m_enablePSKReporter != m_settings.m_enablePSKReporter)) || force) {
        m_ft8DemodWorker->setEnablePskReporter(settings.m_enablePSKReporter);
    }

    if ((settingsKeys.contains("pskReporterCallsign") && (settings.m_pskReporterCallsign != m_settings.m_pskReporterCallsign)) || force) {
        m_pskReporterWorker->setMyCallsign(settings.m_pskReporterCallsign);
    }

    if ((settingsKeys.contains("pskReporterLocator") && (settings.m_pskReporterLocator != m_settings.m_pskReporterLocator)) || force) {
        m_pskReporterWorker->setMyLocator(settings.m_pskReporterLocator);
    }

    if ((settingsKeys.contains("pskReporterSoftware") && (settings.m_pskReporterSoftware != m_settings.m_pskReporterSoftware)) || force) {
        m_pskReporterWorker->setDecoderInfo(settings.m_pskReporterSoftware);
    }

    if ((settingsKeys.contains("nbDecoderThreads") && (settings.m_nbDecoderThreads != m_settings.m_nbDecoderThreads)) || force) {
        m_ft8DemodWorker->setNbDecoderThreads(settings.m_nbDecoderThreads);
    }

    if ((settingsKeys.contains("decoderTimeBudget") && (settings.m_decoderTimeBudget != m_settings.m_decoderTimeBudget)) || force) {
        m_ft8DemodWorker->setDecoderTimeBudget(settings.m_decoderTimeBudget);
    }

    if ((settingsKeys.contains("useOSD") && (settings.m_useOSD != m_settings.m_useOSD)) || force) {
        m_ft8DemodWorker->setUseOSD(settings.m_useOSD);
    }

    if ((settingsKeys.contains("osdDepth") && (settings.m_osdDepth != m_settings.m_osdDepth)) || force) {
        m_ft8DemodWorker->setOSDDepth(settings.m_osdDepth);
    }

    if ((settingsKeys.contains("osdLDPCThreshold") && (settings.m_osdLDPCThreshold != m_settings.m_osdLDPCThreshold)) || force) {
        m_ft8DemodWorker->setOSDLDPCThreshold(settings.m_osdLDPCThreshold);
    }

    if ((settingsKeys.contains("verifyOSD") && (settings.m_verifyOSD != m_settings.m_verifyOSD)) || force) {
        m_ft8DemodWorker->setVerifyOSD(settings.m_verifyOSD);
    }

    if ((settingsKeys.contains("decoderMode") && (settings.m_decoderMode != m_settings.m_decoderMode)) || force)
    {
        m_ft8DemodWorker->setDecoderMode(settings.m_decoderMode);
        m_pskReporterWorker->setTxMode(FT8DemodSettings::getDecoderModeString(settings.m_decoderMode));
        m_ft8DemodWorker->invalidateSequence();
        m_lastProcessPeriodIndex = -1;
    }

    m_sink.applySettings(settingsKeys, settings, force);

    if (force) {
        m_settings = settings;
    } else {
        m_settings.applySettings(settingsKeys, settings);
    }
}

int FT8DemodBaseband::getChannelSampleRate() const
{
    return m_channelizer.getChannelSampleRate();
}


void FT8DemodBaseband::setBasebandSampleRate(int sampleRate)
{
    m_channelizer.setBasebandSampleRate(sampleRate);
    m_sink.applyChannelSettings(m_channelizer.getChannelSampleRate(), m_channelizer.getChannelFrequencyOffset());
}

void FT8DemodBaseband::tick()
{
    QDateTime nowUTC = QDateTime::currentDateTimeUtc();
    const int periodMs = FT8DemodSettings::getDecoderFrameDurationMs(m_settings.m_decoderMode);
    const int activeWindowMs = std::max(periodMs - 1000, 1);
    const qint64 nowMs = nowUTC.toMSecsSinceEpoch();
    const qint64 periodIndex = nowMs / periodMs;
    const int periodOffsetMs = nowMs % periodMs;

    if (periodOffsetMs < activeWindowMs && m_lastProcessPeriodIndex != periodIndex)
    {
        const qint64 previousPeriodStartMs = (periodIndex - 1) * periodMs;
        QDateTime periodTs = QDateTime::fromMSecsSinceEpoch(previousPeriodStartMs, Qt::UTC);
        const int frameSamples = FT8DemodSettings::getDecoderFrameSamples(m_settings.m_decoderMode);
        m_ft8Buffer.getCurrentBuffer(m_ft8WorkerBuffer, frameSamples);
        emit bufferReady(m_ft8WorkerBuffer, periodTs);
        m_lastProcessPeriodIndex = periodIndex;
    }
}
