#include "FreqSwitcherProtocolBM.h"

FreqSwitcherProtocolBM::FreqSwitcherProtocolBM(QObject *parent) : DeviceStatusProtocolBase(parent)
{

}

FreqSwitcherProtocolBM::~FreqSwitcherProtocolBM()
{

}

DeviceFrameBaseDto * FreqSwitcherProtocolBM::frameFactory(int frameType)
{
    DeviceFrameBaseDto * frameData = 0;
    switch (frameType) {
        case FreqSwitcherProtocolBM::FREQ_SWITCHER_FRAME_TYPE::INTERFACE_FRAME :
            frameData = new FreqSwitcherInterfaceDto();
            break;

        case FreqSwitcherProtocolBM::FREQ_SWITCHER_FRAME_TYPE::STATUS_FRAME :
            frameData = new FreqSwitcherStatusDto();
            break;

        default:
            frameData = nullptr;
            break;
    }

    return frameData;
}

bool FreqSwitcherProtocolBM::parseDeviceFrameData(QByteArray rawData, DeviceFrameBaseDto * frameData, 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 FreqSwitcherProtocolBM::FREQ_SWITCHER_FRAME_TYPE::INTERFACE_FRAME :

            ((FreqSwitcherInterfaceDto *)frameData)->ctrlStatus = subList.at(0);
            ((FreqSwitcherInterfaceDto *)frameData)->frameType = FREQ_SWITCHER_INTERFACE_FRAME_TYPE;
            break;

        case FreqSwitcherProtocolBM::FREQ_SWITCHER_FRAME_TYPE::STATUS_FRAME :

            ((FreqSwitcherStatusDto *)frameData)->devStatus = subList.at(17);
            ((FreqSwitcherStatusDto *)frameData)->lockPhaseType = subList.at(0);
            ((FreqSwitcherStatusDto *)frameData)->digitalLockPhaseStatus = subList.at(1);
            ((FreqSwitcherStatusDto *)frameData)->refSelectType = subList.at(2);
            ((FreqSwitcherStatusDto *)frameData)->refFreqSrc = subList.at(3);
            ((FreqSwitcherStatusDto *)frameData)->freqAdjustAcc = subList.at(10).toLongLong();
            ((FreqSwitcherStatusDto *)frameData)->phaseAdjustAcc = subList.at(11).toLongLong();
            for (int i = 0; i < 5; i++)
            {
                //
                QString freqRefStatus = subList.at(4).mid(i, 1);
                ((FreqSwitcherStatusDto *)frameData)->freqRefStatusList.append(freqRefStatus);

                //
                qlonglong localFreqDiff = subList.at(i + 5).toLongLong();
                ((FreqSwitcherStatusDto *)frameData)->localFreqDiffList.append(localFreqDiff);

                //
                float freqRefWeight = subList.at(i + 12).toFloat();
                ((FreqSwitcherStatusDto *)frameData)->freqRefWeightList.append(freqRefWeight);

                //
                QString inputFreqType = subList.at(18).mid(i, 1);
                ((FreqSwitcherStatusDto *)frameData)->inputFreqTypeList.append(inputFreqType);
            }

            for (int i = 0; i < 6; i++)
            {
                //
                QString outTenStatus = subList.at(19).mid(i, 1);
                ((FreqSwitcherStatusDto *)frameData)->outTenStatusList.append(outTenStatus);

                //
                QString outFiveStatus = subList.at(20).mid(i, 1);
                ((FreqSwitcherStatusDto *)frameData)->outFiveStatusList.append(outFiveStatus);
            }

            ((FreqSwitcherStatusDto *)frameData)->frameType = FREQ_SWITCHER_STATUS_FRAME_TYPE;
            break;

    }

    return true;
}

QList<QByteArray> FreqSwitcherProtocolBM::extractFrameList(QByteArray rawData)
{
    QList<QByteArray> resultList;
    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 FreqSwitcherProtocolBM::checkFrame(QByteArray rawData)
{
    // 帧尾不是\r\n
    if (FRAME_TAIL.toLocal8Bit() != rawData.mid(rawData.size() - FRAME_TAIL.size()))
    {
        return FREQ_SWITCHER_FRAME_TYPE::UNKNOW_FRAME;
    }

    // 帧头不是$GL
    if (FREQ_SWITCHER_INTERFACE_FRAME_HEAD.toLocal8Bit() == rawData.mid(0, FREQ_SWITCHER_INTERFACE_FRAME_HEAD.size()))
    {
        // 帧长度小于最小的长度
        if (rawData.size() < FREQ_SWITCHER_INTERFACE_FRAME_MIN_LENGTH)
        {
            return FREQ_SWITCHER_FRAME_TYPE::UNKNOW_FRAME;
        } else
        {
            return FREQ_SWITCHER_FRAME_TYPE::INTERFACE_FRAME;
        }
    } else if (FREQ_SWITCHER_STATUS_FRAME_HEAD.toLocal8Bit() == rawData.mid(0, FREQ_SWITCHER_STATUS_FRAME_HEAD.size()))
    {
        // 帧长度小于最小的长度
        if (rawData.size() < FREQ_SWITCHER_STATUS_FRAME_MIN_LENGTH)
        {
            return FREQ_SWITCHER_FRAME_TYPE::UNKNOW_FRAME;
        } else
        {
            return FREQ_SWITCHER_FRAME_TYPE::STATUS_FRAME;
        }
    } else
    {
        return FREQ_SWITCHER_FRAME_TYPE::UNKNOW_FRAME;
    }
}
