#include "xscriptstringlist.h"
#include "xstring.h"

XScriptStringList::XScriptStringList()
    : XAsset()
    , mCount(0)
    , mScriptStrings()
{
    SetName("Script String List");
}

int XScriptStringList::Size()
{
    return mScriptStrings.size();
}

void XScriptStringList::Clear()
{
    mCount = 0;
    mScriptStrings.clear();
}

void XScriptStringList::ParseData(XDataStream *aStream)
{
    if (GetPtr() == -1)
    {
        if (IsDebug())
        {
            qDebug() << QString("[%1] Parsing data for %2").arg(aStream->device()->pos(), 10, 10, QChar('0')).arg(GetName());
        }
        for (int i = 0; i < mCount; i++)
        {
            XString newScriptString;
            newScriptString.ParsePtr(aStream, false);
            mScriptStrings.append(newScriptString);
        }
        for (int i = 0; i < mCount; i++)
        {
            mScriptStrings[i].ParseData(aStream);
        }
    }
}

void XScriptStringList::ParsePtr(XDataStream *aStream, bool aDataFlag)
{
    qint32 stringsPtr;

    *aStream
        >> mCount
        >> stringsPtr;
    if (mDebug)
    {
        // Always treat as unsigned when displaying in hex
        quint64 raw = static_cast<quint64>(static_cast<quint32>(GetPtr()));

        QString hexPtr = QString("0x%1")
                             .arg(raw, 8, 16, QLatin1Char('0'))
                             .toUpper();

        qDebug() << QString("[%1] Parsed %2 ptr %3 (%4)").arg(aStream->device()->pos(), 10, 10, QChar('0')).arg(GetName()).arg(stringsPtr).arg(hexPtr);
        qDebug() << QString("[%1] mCount = %2").arg(aStream->device()->pos(), 10, 10, QChar('0')).arg(mCount);
    }

    SetPtr(stringsPtr);

    if (aDataFlag)
    {
        ParseData(aStream);
    }
}
