#include "xdatastream.h"

#include <QIODevice>
#include <QDebug>

XDataStream::XDataStream(QIODevice *aDevice)
    : QDataStream(aDevice)
    , mDebug(false)
{

}

XDataStream::XDataStream()
    : QDataStream()
    , mDebug(false)
{

}

XDataStream::XDataStream(const QByteArray &aData)
    : QDataStream(aData)
    , mDebug(false)
{

}

XDataStream::XDataStream(QByteArray *aData, OpenMode aFlags)
    : QDataStream(aData, aFlags)
    , mDebug(false)
{

}

XDataStream::~XDataStream()
{

}

void XDataStream::SetDebug(bool aDebug)
{
    mDebug = aDebug;
}

qint8 XDataStream::ParseInt8(const QString& aDebugString)
{
    qint64 start = this->device()->pos();

    qint8 val;
    *this >> val;

    if (mDebug)
    {
        qDebug() << QString("[%1-%2] Parsed %3: %4")
        .arg(start, 10, 10, QChar('0'))
            .arg(this->device()->pos(), 10, 10, QChar('0'))
            .arg(aDebugString)
            .arg(val);
    }
    return val;
}

quint8 XDataStream::ParseUInt8(const QString& aDebugString)
{
    qint64 start = this->device()->pos();

    quint8 val;
    *this >> val;

    if (mDebug)
    {
        qDebug() << QString("[%1-%2] Parsed %3: %4")
        .arg(start, 10, 10, QChar('0'))
            .arg(this->device()->pos(), 10, 10, QChar('0'))
            .arg(aDebugString)
            .arg(val);
    }
    return val;
}

qint16 XDataStream::ParseInt16(const QString& aDebugString)
{
    qint64 start = this->device()->pos();

    qint16 val;
    *this >> val;

    if (mDebug)
    {
        qDebug() << QString("[%1-%2] Parsed %3: %4")
        .arg(start, 10, 10, QChar('0'))
            .arg(this->device()->pos(), 10, 10, QChar('0'))
            .arg(aDebugString)
            .arg(val);
    }
    return val;
}

quint16 XDataStream::ParseUInt16(const QString& aDebugString)
{
    qint64 start = this->device()->pos();

    quint16 val;
    *this >> val;

    if (mDebug)
    {
        qDebug() << QString("[%1-%2] Parsed %3: %4")
        .arg(start, 10, 10, QChar('0'))
            .arg(this->device()->pos(), 10, 10, QChar('0'))
            .arg(aDebugString)
            .arg(val);
    }
    return val;
}

qint32 XDataStream::ParseInt32(const QString& aDebugString)
{
    qint64 start = this->device()->pos();

    qint32 val;
    *this >> val;

    if (mDebug)
    {
        qDebug() << QString("[%1-%2] Parsed %3: %4")
        .arg(start, 10, 10, QChar('0'))
            .arg(this->device()->pos(), 10, 10, QChar('0'))
            .arg(aDebugString)
            .arg(val);
    }
    return val;
}

quint32 XDataStream::ParseUInt32(const QString& aDebugString)
{
    qint64 start = this->device()->pos();

    quint32 val;
    *this >> val;

    if (mDebug)
    {
        qDebug() << QString("[%1-%2] Parsed %3: %4")
        .arg(start, 10, 10, QChar('0'))
            .arg(this->device()->pos(), 10, 10, QChar('0'))
            .arg(aDebugString)
            .arg(val);
    }
    return val;
}

qint64 XDataStream::ParseInt64(const QString& aDebugString)
{
    qint64 start = this->device()->pos();

    qint64 val;
    *this >> val;

    if (mDebug)
    {
        qDebug() << QString("[%1-%2] Parsed %3: %4")
        .arg(start, 10, 10, QChar('0'))
            .arg(this->device()->pos(), 10, 10, QChar('0'))
            .arg(aDebugString)
            .arg(val);
    }
    return val;
}

quint64 XDataStream::ParseUInt64(const QString& aDebugString)
{
    qint64 start = this->device()->pos();

    quint64 val;
    *this >> val;

    if (mDebug)
    {
        qDebug() << QString("[%1-%2] Parsed %3: %4")
        .arg(start, 10, 10, QChar('0'))
            .arg(this->device()->pos(), 10, 10, QChar('0'))
            .arg(aDebugString)
            .arg(val);
    }
    return val;
}

float XDataStream::ParseSingle(const QString& aDebugString)
{
    qint64 start = this->device()->pos();


    float val;
    quint32 rawVal;
    *this >> rawVal;
    memcpy(&val, &rawVal, sizeof(val));

    if (mDebug)
    {
        qDebug() << QString("[%1-%2] Parsed %3: %4")
        .arg(start, 10, 10, QChar('0'))
            .arg(this->device()->pos(), 10, 10, QChar('0'))
            .arg(aDebugString)
            .arg(val);
    }
    return val;
}

double XDataStream::ParseDouble(const QString& aDebugString)
{
    qint64 start = this->device()->pos();

    float val;
    *this >> val;

    if (mDebug)
    {
        qDebug() << QString("[%1-%2] Parsed %3: %4")
        .arg(start, 10, 10, QChar('0'))
            .arg(this->device()->pos(), 10, 10, QChar('0'))
            .arg(aDebugString)
            .arg(val);
    }
    return val;
}
