#include "FrequencyTuningProtocolBM.h"

FrequencyTuningProtocolBM::FrequencyTuningProtocolBM(QObject *parent) : DeviceProtocolBase(parent)
{

}

FrequencyTuningProtocolBM::~FrequencyTuningProtocolBM()
{

}

DeviceFrameBaseDto * FrequencyTuningProtocolBM::frameFactory(int frameType)
{
    DeviceFrameBaseDto * frameData = 0;
    switch (frameType) {
        case FrequencyTuningProtocolBM::FREQUENCY_TUNING_FRAME_TYPE::FREQ_FRAME :
            frameData = new FrequencyTuningStatusFreqDto();
            break;

        case FrequencyTuningProtocolBM::FREQUENCY_TUNING_FRAME_TYPE::PULSE_FRAME :
            frameData = new FrequencyTuningStatusPulseDto();
            break;

        default:
            frameData = nullptr;
            break;
    }

    return frameData;
}

bool FrequencyTuningProtocolBM::parseDeviceFrameData(QByteArray rawData, DeviceFrameBaseDto * ftFrameData, int frameType)
{
    // get the raw data between ',' and '*'
    int start = rawData.indexOf(FRAME_CONTENT_SEP);
    int end = rawData.indexOf(FRAME_SUM_SEP);

    QByteArray content = rawData.mid(start + 1, end - start - 1);
    QString contentStr = QString(content);
    QStringList subList = contentStr.split(",");

    switch (frameType) {
        case FrequencyTuningProtocolBM::FREQUENCY_TUNING_FRAME_TYPE::FREQ_FRAME :

            ((FrequencyTuningStatusFreqDto *)ftFrameData)->freqAdjustAcc = subList.at(16).toLongLong();
            ((FrequencyTuningStatusFreqDto *)ftFrameData)->pulseAdjustAcc = subList.at(17).toLongLong();
            ((FrequencyTuningStatusFreqDto *)ftFrameData)->devStatus = subList.at(23);
            ((FrequencyTuningStatusFreqDto *)ftFrameData)->inputTimeType = subList.at(24);
            ((FrequencyTuningStatusFreqDto *)ftFrameData)->inputValid = subList.at(25);
            ((FrequencyTuningStatusFreqDto *)ftFrameData)->frameType = FREQUENCY_TUNING_FREQ_FRAME_TYPE;
            break;

        case FrequencyTuningProtocolBM::FREQUENCY_TUNING_FRAME_TYPE::PULSE_FRAME :

            ((FrequencyTuningStatusPulseDto *)ftFrameData)->synchStatus = subList.at(0);
            ((FrequencyTuningStatusPulseDto *)ftFrameData)->secondDiff = subList.at(1).toLongLong();
            ((FrequencyTuningStatusPulseDto *)ftFrameData)->refValid = subList.at(2);
            ((FrequencyTuningStatusPulseDto *)ftFrameData)->phaseShiftAcc = subList.at(3).toLongLong();
            ((FrequencyTuningStatusPulseDto *)ftFrameData)->pulseWidth = subList.at(4).toLongLong();
            ((FrequencyTuningStatusPulseDto *)ftFrameData)->frameType = FREQUENCY_TUNING_PULSE_FRAME_TYPE;
            break;
    }

    return true;
}

QList<QByteArray> FrequencyTuningProtocolBM::extractFrameList(QByteArray rawData)
{
    QList<QByteArray> resultList;

    int start = rawData.indexOf("$GL");
    if (start < 0)
    {
        return resultList;
    }
    rawData = rawData.right(rawData.size() - start);

    if (rawData.endsWith(QByteArray("\r\n")) == true && rawData.startsWith("$") == true)
    {
        QByteArray ba;
        for (int i = 0; i < rawData.size() - 1; i++)
        {
            if (rawData.at(i) != '\r')
            {
                ba.append(rawData.at(i));
            } else
            {
                if (rawData.at(i + 1) == '\n')
                {
                    ba.append("\r\n");
                    resultList.append(ba);
                    ba.clear();
                    i++;
                } else
                {
                    ba.append(rawData.at(i));
                }
            }
        }
    }

    return resultList;
}

int FrequencyTuningProtocolBM::checkFrame(QByteArray rawData)
{
    // 帧尾不是\r\n
    if (FRAME_TAIL.toLocal8Bit() != rawData.mid(rawData.size() - FRAME_TAIL.size()))
    {
        return FREQUENCY_TUNING_FRAME_TYPE::UNKNOW_FRAME;
    }

    // 帧头不是$GL
    if (FREQUENCY_TUNING_FREQ_FRAME_HEAD.toLocal8Bit() == rawData.mid(0, FREQUENCY_TUNING_FREQ_FRAME_HEAD.size()))
    {
        // 帧长度小于最小的长度
        if (rawData.size() < FREQUENCY_TUNING_FREQ_FRAME_MIN_LENGTH)
        {
            return FREQUENCY_TUNING_FRAME_TYPE::UNKNOW_FRAME;
        } else
        {
            return FREQUENCY_TUNING_FRAME_TYPE::FREQ_FRAME;
        }
    } else if (FREQUENCY_TUNING_PULSE_FRAME_HEAD.toLocal8Bit() == rawData.mid(0, FREQUENCY_TUNING_PULSE_FRAME_HEAD.size()))
    {
        // 帧长度小于最小的长度
        if (rawData.size() < FREQUENCY_TUNING_PULSE_FRAME_MIN_LENGTH)
        {
            return FREQUENCY_TUNING_FRAME_TYPE::UNKNOW_FRAME;
        } else
        {
            return FREQUENCY_TUNING_FRAME_TYPE::PULSE_FRAME;
        }
    } else
    {
        return FREQUENCY_TUNING_FRAME_TYPE::UNKNOW_FRAME;
    }
}
