#include "FreqReplicatorProtocolTX.h"

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

}

FreqReplicatorProtocolTX::~FreqReplicatorProtocolTX()
{

}

DeviceFrameBaseDto * FreqReplicatorProtocolTX::frameFactory(int frameType)
{
    DeviceFrameBaseDto * frameData = new FreqReplicatorStatusDto();

    return frameData;
}

bool FreqReplicatorProtocolTX::parseDeviceFrameData(QByteArray rawData, DeviceFrameBaseDto * frameData, int frameType)
{
    if (frameType != 1)
        return false;

    QByteArray content = rawData.mid(4, rawData.size() - 5);
    QByteArray inputACByte = content.mid(1, 2);
    QByteArray inputBDByte = content.mid(3, 2);
    QByteArray outputACByte = content.mid(5, 8);
    QByteArray outputBDByte = content.mid(13, 8);

    QString inputAC = "";
    QString inputBD = "";
    QString outputAC = "";
    QString outputBD = "";

    if (content.at(0) == 0x01)
    {
        ((FreqReplicatorStatusDto *) frameData)->abOrCd = "AB";

        inputAC = QString("%1,%2").arg(inputACByte.at(1) == 0x01 ? "1" : "0").arg(inputACByte.at(0) == 0x01 ? "1" : "0");
        inputBD = QString("%1,%2").arg(inputBDByte.at(1) == 0x01 ? "1" : "0").arg(inputBDByte.at(0) == 0x01 ? "1" : "0");

        ((FreqReplicatorStatusDto *) frameData)->inValid.append(inputAC);
        ((FreqReplicatorStatusDto *) frameData)->inValid.append(inputBD);
        ((FreqReplicatorStatusDto *) frameData)->inValid.append("");
        ((FreqReplicatorStatusDto *) frameData)->inValid.append("");

        for (int i = 0; i < 8; i++)
        {
            outputAC += outputACByte.at(i) == 0x01 ? "1," : "0,";
            outputBD += outputBDByte.at(i) == 0x01 ? "1," : "0,";
        }

        ((FreqReplicatorStatusDto *) frameData)->outValid.append(outputAC.mid(0, outputAC.size() - 1));
        ((FreqReplicatorStatusDto *) frameData)->outValid.append(outputBD.mid(0, outputBD.size() - 1));
        ((FreqReplicatorStatusDto *) frameData)->outValid.append("");
        ((FreqReplicatorStatusDto *) frameData)->outValid.append("");

        ((FreqReplicatorStatusDto *)frameData)->frameType = FREQ_REPLICATOR_STATUS_AB_FRAME_TYPE;
    } else
    {
        ((FreqReplicatorStatusDto *) frameData)->abOrCd = "CD";

        inputAC = QString("%1,%2").arg(inputACByte.at(1) == 0x01 ? "1" : "0").arg(inputACByte.at(0) == 0x01 ? "1" : "0");
        inputBD = QString("%1,%2").arg(inputBDByte.at(1) == 0x01 ? "1" : "0").arg(inputBDByte.at(0) == 0x01 ? "1" : "0");

        ((FreqReplicatorStatusDto *) frameData)->inValid.append("");
        ((FreqReplicatorStatusDto *) frameData)->inValid.append("");
        ((FreqReplicatorStatusDto *) frameData)->inValid.append(inputAC);
        ((FreqReplicatorStatusDto *) frameData)->inValid.append(inputBD);

        for (int i = 0; i < 8; i++)
        {
            outputAC += outputACByte.at(i) == 0x01 ? "1," : "0,";
            outputBD += outputBDByte.at(i) == 0x01 ? "1," : "0,";
        }

        ((FreqReplicatorStatusDto *) frameData)->outValid.append("");
        ((FreqReplicatorStatusDto *) frameData)->outValid.append("");
        ((FreqReplicatorStatusDto *) frameData)->outValid.append(outputAC.mid(0, outputAC.size() - 1));
        ((FreqReplicatorStatusDto *) frameData)->outValid.append(outputBD.mid(0, outputBD.size() - 1));

        ((FreqReplicatorStatusDto *)frameData)->frameType = FREQ_REPLICATOR_STATUS_CD_FRAME_TYPE;
    }

    return true;
}

QList<QByteArray> FreqReplicatorProtocolTX::extractFrameList(QByteArray rawData)
{
    QList<QByteArray> resultList;
    if (rawData.indexOf(0x15) >= 0)
    {
        while (QByteUtil::binToHexString(rawData.mid(0, 3)) == "AA5500")
        {
            QByteArray ba;

            qint8 length = rawData.mid(3, 1).toHex().toInt(0, 16);
            if (rawData.length() >= length + 5) // 0x15
            {
                ba.append(rawData.mid(0, length + 5));

                resultList.append(ba);
                ba.clear();

                rawData = rawData.mid(length + 5, rawData.length() - length - 5);
            } else
            {
                break;
            }
        }
    } else if (rawData.indexOf(0x0B) >= 0)
    {
        while (QByteUtil::binToHexString(rawData.mid(0, 5)) == "AA55000B01")
        {
            QByteArray ba;

            if (rawData.length() >= 32) // 0x0B
            {
                ba.append(QByteUtil::hexStringToBytes("AA55001501"));
                ba.append(rawData.mid(5, 2));
                ba.append(rawData.mid(21, 2));
                ba.append(rawData.mid(7, 8));
                ba.append(rawData.mid(23, 8));
                ba.append(QByteUtil::hexStringToBytes("FF"));

                resultList.append(ba);
                ba.clear();

                rawData = rawData.right(rawData.length() - 32);
            } else
            {
                break;
            }
        }
    }

    return resultList;
}

int FreqReplicatorProtocolTX::checkFrame(QByteArray rawData)
{
    if (rawData.size() == 26)
        return 1;
    return 0;
}
