#include "xsurface.h"

XSurface::XSurface()
    : XAsset()
    , mTileMode(0)
    , mDeformed(false)
    , mVertCount(0)
    , mTriCount(0)
    , mTriIndices(0)
    , mVertInfo()
    , mVert()
    , mVertBuffer()
    , mVertListCount(0)
    , mVertList()
    , mIndexBuffer()
    , mPartBits(4)
{
    SetName("Surface");
}

XSurface::~XSurface()
{

}

void XSurface::ParseData(XDataStream *aStream)
{
    *aStream >> mTileMode;
    if (IsDebug())
    {
        qDebug() << QString("[%1] mTileMode = %2").arg(aStream->device()->pos(), 10, 10, QChar('0')).arg(mTileMode);
    }

    *aStream >> mDeformed;
    if (IsDebug())
    {
        qDebug() << QString("[%1] mDeformed = %2").arg(aStream->device()->pos(), 10, 10, QChar('0')).arg(mDeformed);
    }

    *aStream >> mVertCount;
    if (IsDebug())
    {
        qDebug() << QString("[%1] mVertCount = %2").arg(aStream->device()->pos(), 10, 10, QChar('0')).arg(mVertCount);
    }

    *aStream >> mTriCount;
    if (IsDebug())
    {
        qDebug() << QString("[%1] mTriCount = %2").arg(aStream->device()->pos(), 10, 10, QChar('0')).arg(mTriCount);
    }

    aStream->skipRawData(2);

    qint32 triIndicesPtr;
    *aStream >> triIndicesPtr;
    if (IsDebug())
    {
        qDebug() << QString("[%1] triIndicesPtr = %2").arg(aStream->device()->pos(), 10, 10, QChar('0')).arg(triIndicesPtr);
    }

    mVertInfo.SetPtr(-1);
    mVertInfo.ParseData(aStream);
    mVert.ParsePtr(aStream, false);
    mVertBuffer.ParseData(aStream);

    *aStream >> mVertListCount;
    if (IsDebug())
    {
        qDebug() << QString("[%1] mVertListCount = %2").arg(aStream->device()->pos(), 10, 10, QChar('0')).arg(mVertListCount);
    }

    mVertList.ParseData(aStream);
    mIndexBuffer.ParseData(aStream);

    *aStream
        >> mPartBits[0]
        >> mPartBits[1]
        >> mPartBits[2]
        >> mPartBits[3];
    if (IsDebug())
    {
        qDebug() << QString("[%1] mPartBits = %2").arg(aStream->device()->pos(), 10, 10, QChar('0')).arg(*mPartBits.data());
    }

    //mVertInfo.ParseData(aStream);
    mVert.ParseData(aStream);
    mVertBuffer.ParseData(aStream);
    //mVertList.ParseData(aStream);

    if (triIndicesPtr == -1)
    {
        for (int i = 0; i < mTriCount; i++)
        {
            quint16 newTri;
            *aStream >> newTri;
            if (IsDebug())
            {
                qDebug() << QString("[%1] newTri = %2").arg(aStream->device()->pos(), 10, 10, QChar('0')).arg(newTri);
            }
            mTriIndices.append(newTri);
        }
    }
}

void XSurface::Clear()
{
    
}
