#include "xstringtable.h"
#include "xstring.h"

XStringTable::XStringTable()
    : XAsset()
    , mName(new XString())
    , mValues(new XStringArray())
    , mColumnCount(0)
    , mRowCount(0) {

}

XStringTable::~XStringTable()
{
    delete mName;

    for (int i = 0; i < mValues->size(); i++)
    {
        delete mValues->at(i);
    }
}

void XStringTable::SetName(XString *aName) {
    if (aName == nullptr)
    {
        return;
    }

    mName = aName;
}

void XStringTable::SetName(const QString aName) {
    mName->SetString(aName);
}

XString *XStringTable::GetName() const {
    return mName;
}

void XStringTable::SetValues(XStringArray* aValues) {
    mValues = aValues;
}

XStringArray* XStringTable::GetValues() const {
    return mValues;
}

void XStringTable::SetColumnCount(int aColumnCount) {
    mColumnCount = aColumnCount;
}

int XStringTable::GetColumnCount() const {
    return mColumnCount;
}

void XStringTable::SetRowCount(int aRowCount) {
    mRowCount = aRowCount;
}

int XStringTable::GetRowCount() const {
    return mRowCount;
}

void XStringTable::Clear()
{
    mName->Clear();

    for (int i = 0; i < mValues->size(); i++)
    {
        delete mValues->at(i);
    }
    mValues->clear();

    mColumnCount = 0;
    mRowCount = 0;

    XAsset::Clear();
}

void XStringTable::ParseData(QDataStream *aStream) {
    qint32 valuesPtr;

    mName->ParsePtr(aStream, false);

    *aStream
        >> mColumnCount
        >> mRowCount
        >> valuesPtr;

    mName->ParseData(aStream);
    if (valuesPtr)
    {
        mValues = XString::ParseArray(aStream, mRowCount * mColumnCount);
    }
}
