#include "TimeSwitcherProtocolBM.h"

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

}

TimeSwitcherProtocolBM::~TimeSwitcherProtocolBM()
{

}

DeviceFrameBaseDto * TimeSwitcherProtocolBM::frameFactory(int frameType)
{
    DeviceFrameBaseDto * frameData = 0;
    switch (frameType) {
        case TimeSwitcherProtocolBM::TIME_SWITCHER_FRAME_TYPE::INTERFACE_FRAME :
            frameData = new TimeSwitcherInterfaceDto();
            break;

        case TimeSwitcherProtocolBM::TIME_SWITCHER_FRAME_TYPE::STATUS_FRAME :
            frameData = new TimeSwitcherStatusDto();
            break;

        default:
            frameData = nullptr;
            break;
    }

    return frameData;
}

bool TimeSwitcherProtocolBM::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 TimeSwitcherProtocolBM::TIME_SWITCHER_FRAME_TYPE::INTERFACE_FRAME :

            ((TimeSwitcherInterfaceDto *)frameData)->ctrlStatus = subList.at(0);
            ((TimeSwitcherInterfaceDto *)frameData)->frameType = TIME_SWITCHER_INTERFACE_FRAME_TYPE;
            break;

        case TimeSwitcherProtocolBM::TIME_SWITCHER_FRAME_TYPE::STATUS_FRAME :

            ((TimeSwitcherStatusDto *)frameData)->devStatus = subList.at(0);
            ((TimeSwitcherStatusDto *)frameData)->refSelectType = subList.at(1);
            ((TimeSwitcherStatusDto *)frameData)->refTmSrc = subList.at(2);
            ((TimeSwitcherStatusDto *)frameData)->oppsPhaseAccOut = subList.at(13).toLongLong();
            ((TimeSwitcherStatusDto *)frameData)->bacRatio = subList.at(14).toInt();
            ((TimeSwitcherStatusDto *)frameData)->bacRange = subList.at(15).toFloat();
            for (int i = 0; i < 5; i++)
            {
                //
                QString tmSrcStatus = subList.at(i + 3);
                ((TimeSwitcherStatusDto *)frameData)->tmSrcStatusList.append(tmSrcStatus);

                //
                double localTmDiff = subList.at(i + 8).toDouble();
                ((TimeSwitcherStatusDto *)frameData)->localTmDiffList.append(localTmDiff);

                //
                qlonglong delayComp = subList.at(i + 16).toLongLong();
                ((TimeSwitcherStatusDto *)frameData)->delayCompensList.append(delayComp);

                //
                QString oppsOutStatus = subList.at(21).mid(i, 1);
                ((TimeSwitcherStatusDto *)frameData)->oppsOutStatusList.append(oppsOutStatus);

                //
                QString bdcOutStatus = subList.at(22).mid(i, 1);
                ((TimeSwitcherStatusDto *)frameData)->bdcOutStatusList.append(bdcOutStatus);

                //
                QString bacOutStatus = subList.at(23).mid(i, 1);
                ((TimeSwitcherStatusDto *)frameData)->bacOutStatusList.append(bacOutStatus);
            }

            ((TimeSwitcherStatusDto *)frameData)->frameType = TIME_SWITCHER_STATUS_FRAME_TYPE;
            break;

    }

    return true;
}

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

    // 帧头不是$GL
    if (TIME_SWITCHER_INTERFACE_FRAME_HEAD.toLocal8Bit() == rawData.mid(0, TIME_SWITCHER_INTERFACE_FRAME_HEAD.size()))
    {
        // 帧长度小于最小的长度
        if (rawData.size() < TIME_SWITCHER_INTERFACE_FRAME_MIN_LENGTH)
        {
            return TIME_SWITCHER_FRAME_TYPE::UNKNOW_FRAME;
        } else
        {
            return TIME_SWITCHER_FRAME_TYPE::INTERFACE_FRAME;
        }
    } else if (TIME_SWITCHER_STATUS_FRAME_HEAD.toLocal8Bit() == rawData.mid(0, TIME_SWITCHER_STATUS_FRAME_HEAD.size()))
    {
        // 帧长度小于最小的长度
        if (rawData.size() < TIME_SWITCHER_STATUS_FRAME_MIN_LENGTH)
        {
            return TIME_SWITCHER_FRAME_TYPE::UNKNOW_FRAME;
        } else
        {
            return TIME_SWITCHER_FRAME_TYPE::STATUS_FRAME;
        }
    } else
    {
        return TIME_SWITCHER_FRAME_TYPE::UNKNOW_FRAME;
    }
}
