#include "SignalGeneratorProtocolBM.h"
#include <iostream>

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

}

SignalGeneratorProtocolBM::~SignalGeneratorProtocolBM()
{

}

DeviceFrameBaseDto * SignalGeneratorProtocolBM::frameFactory(int frameType)
{
    DeviceFrameBaseDto * sgFrameData = 0;
    switch (frameType) {
        case SignalGeneratorProtocolBM::SIGNAL_GENERATOR_FRAME_TYPE::INTERFACE_FRAME :
            sgFrameData = new SignalGeneratorInterfaceDto();
            break;

        case SignalGeneratorProtocolBM::SIGNAL_GENERATOR_FRAME_TYPE::STATUS_FRAME :
            sgFrameData = new SignalGeneratorStatusDto();
            break;

        case SignalGeneratorProtocolBM::SIGNAL_GENERATOR_FRAME_TYPE::ZDA_FRAME :
            sgFrameData = new SignalGeneratorZDATimeDto();
            break;

        case SignalGeneratorProtocolBM::SIGNAL_GENERATOR_FRAME_TYPE::MJD_FRAME :
            sgFrameData = new SignalGeneratorMJDTimeDto();
            break;

        default:
            sgFrameData = nullptr;
            break;
    }

    return sgFrameData;
}

bool SignalGeneratorProtocolBM::parseDeviceFrameData(QByteArray rawData, DeviceFrameBaseDto * sgFrameData, 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 SignalGeneratorProtocolBM::SIGNAL_GENERATOR_FRAME_TYPE::INTERFACE_FRAME :

            ((SignalGeneratorInterfaceDto *)sgFrameData)->keyControl = subList.at(0);
            ((SignalGeneratorInterfaceDto *)sgFrameData)->timeType = subList.at(1);
            ((SignalGeneratorInterfaceDto *)sgFrameData)->frameType = SIGNAL_GENERATOR_INTERFACE_FRAME_TYPE;
            break;

        case SignalGeneratorProtocolBM::SIGNAL_GENERATOR_FRAME_TYPE::STATUS_FRAME :

            ((SignalGeneratorStatusDto *)sgFrameData)->devStatus = subList.at(0);
            ((SignalGeneratorStatusDto *)sgFrameData)->leapSec = subList.at(1);
            ((SignalGeneratorStatusDto *)sgFrameData)->leapTimestamp = subList.at(2);
            ((SignalGeneratorStatusDto *)sgFrameData)->freqSignalStatus = subList.at(3);
            ((SignalGeneratorStatusDto *)sgFrameData)->freqSignalType = subList.at(4);
            ((SignalGeneratorStatusDto *)sgFrameData)->oppsSignalStatus = subList.at(5);
            ((SignalGeneratorStatusDto *)sgFrameData)->phaseDiff = subList.at(6).toFloat();
            ((SignalGeneratorStatusDto *)sgFrameData)->pulseWidth = subList.at(7).toLongLong();
            ((SignalGeneratorStatusDto *)sgFrameData)->phaseShiftAcc = subList.at(8).toLongLong();
            ((SignalGeneratorStatusDto *)sgFrameData)->bacRatio = subList.at(9).toUInt();
            ((SignalGeneratorStatusDto *)sgFrameData)->bacRange = subList.at(10).toFloat();
            ((SignalGeneratorStatusDto *)sgFrameData)->frameType = SIGNAL_GENERATOR_STATUS_FRAME_TYPE;
            break;

        case SignalGeneratorProtocolBM::SIGNAL_GENERATOR_FRAME_TYPE::ZDA_FRAME :

            ((SignalGeneratorZDATimeDto *)sgFrameData)->zdaTime = subList.at(0);
            ((SignalGeneratorZDATimeDto *)sgFrameData)->zdaDay = subList.at(1);
            ((SignalGeneratorZDATimeDto *)sgFrameData)->zdaMon = subList.at(2);
            ((SignalGeneratorZDATimeDto *)sgFrameData)->zdaYear = subList.at(3);
            ((SignalGeneratorZDATimeDto *)sgFrameData)->leapSec = subList.at(4);
            ((SignalGeneratorZDATimeDto *)sgFrameData)->valid = subList.at(5);
            ((SignalGeneratorZDATimeDto *)sgFrameData)->frameType = SIGNAL_GENERATOR_ZDA_FRAME_TYPE;
            break;

        case SignalGeneratorProtocolBM::SIGNAL_GENERATOR_FRAME_TYPE::MJD_FRAME :

            ((SignalGeneratorMJDTimeDto *)sgFrameData)->mjdTime = subList.at(0);
            ((SignalGeneratorMJDTimeDto *)sgFrameData)->mjdDay = subList.at(1);
            ((SignalGeneratorMJDTimeDto *)sgFrameData)->leapSec = subList.at(2);
            ((SignalGeneratorMJDTimeDto *)sgFrameData)->valid = subList.at(3);
            ((SignalGeneratorMJDTimeDto *)sgFrameData)->frameType = SIGNAL_GENERATOR_MJD_FRAME_TYPE;
            break;

    }

    return true;
}

QList<QByteArray> SignalGeneratorProtocolBM::extractFrameList(QByteArray rawData)
{
    QList<QByteArray> resultList;
//    int start = rawData.indexOf("$GL");
//    if (start < 0)
//    {
//        return resultList;
//    }
//    rawData = rawData.right(rawData.size() - start);

//    int end = rawData.lastIndexOf("\r\n");
//    if (end < 0)
//    {
//        return resultList;
//    }
//    rawData = rawData.left(end);

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

    // 帧头不是$GL
    if (SIGNAL_GENERATOR_INTERFACE_FRAME_HEAD.toLocal8Bit() == rawData.mid(0, SIGNAL_GENERATOR_INTERFACE_FRAME_HEAD.size()))
    {
        // 帧长度小于最小的长度
        if (rawData.size() < SIGNAL_GENERATOR_INTERFACE_FRAME_MIN_LENGTH)
        {
            return SIGNAL_GENERATOR_FRAME_TYPE::UNKNOW_FRAME;
        } else
        {
            return SIGNAL_GENERATOR_FRAME_TYPE::INTERFACE_FRAME;
        }
    } else if (SIGNAL_GENERATOR_STATUS_FRAME_HEAD.toLocal8Bit() == rawData.mid(0, SIGNAL_GENERATOR_STATUS_FRAME_HEAD.size()))
    {
        // 帧长度小于最小的长度
        if (rawData.size() < SIGNAL_GENERATOR_STATUS_FRAME_MIN_LENGTH)
        {
            return SIGNAL_GENERATOR_FRAME_TYPE::UNKNOW_FRAME;
        } else
        {
            return SIGNAL_GENERATOR_FRAME_TYPE::STATUS_FRAME;
        }
    } else if (SIGNAL_GENERATOR_MJD_FRAME_HEAD.toLocal8Bit() == rawData.mid(0, SIGNAL_GENERATOR_MJD_FRAME_HEAD.size()))
    {
        // 帧长度小于最小的长度
        if (rawData.size() < SIGNAL_GENERATOR_MJD_FRAME_MIN_LENGTH)
        {
            return SIGNAL_GENERATOR_FRAME_TYPE::UNKNOW_FRAME;
        } else
        {
            return SIGNAL_GENERATOR_FRAME_TYPE::MJD_FRAME;
        }
    } else if (SIGNAL_GENERATOR_ZDA_FRAME_HEAD.toLocal8Bit() == rawData.mid(0, SIGNAL_GENERATOR_ZDA_FRAME_HEAD.size()))
    {
        // 帧长度小于最小的长度
        if (rawData.size() < SIGNAL_GENERATOR_ZDA_FRAME_MIN_LENGTH)
        {
            return SIGNAL_GENERATOR_FRAME_TYPE::UNKNOW_FRAME;
        } else
        {
            return SIGNAL_GENERATOR_FRAME_TYPE::ZDA_FRAME;
        }
    } else
    {
        return SIGNAL_GENERATOR_FRAME_TYPE::UNKNOW_FRAME;
    }
}
