#include "xanimparts.h"

XAnimParts::XAnimParts()
    : XAsset()
    , mName()
    , mDataByteCount(0)
    , mDataShortCount(0)
    , mDataIntCount(0)
    , mRandomDataByteCount(0)
    , mRandomDataIntCount(0)
    , mNumFrames(0)
    , mIsLoop(false)
    , mIsDelta(false)
    , mBoneCount(12)
    , mNotifyCount(0)
    , mAssetType(0)
    , mPad(false)
    , mRandomDataShortCount(0)
    , mIndexCount(0)
    , mFramerate(0.0f)
    , mFrequency(0.0f)
    , mNames(0)
    , mDataByte(0)
    , mDataShort(0)
    , mDataInt(0)
    , mRandomDataShort(0)
    , mRandomDataByte(0)
    , mRandomDataInt(0)
    , mIndices()
    , mNotify()
    , mDeltaPart()
{
    SetType(ASSET_TYPE_XANIMPARTS);
    SetName("XAnimParts");
}

XAnimParts::~XAnimParts()
{

}

void XAnimParts::ParseData(QDataStream *aStream) {
    if (GetPtr() == -1) {

        mName.ParsePtr(aStream, false);

        // Parse all fields
        *aStream
            >> mDataByteCount
            >> mDataShortCount
            >> mDataIntCount
            >> mRandomDataByteCount
            >> mRandomDataIntCount
            >> mNumFrames;

        quint8 loopDelta;
        *aStream >> loopDelta;
        mIsLoop = (loopDelta & 0x1) != 0;
        mIsDelta = (loopDelta & 0x2) != 0;

        for (int i = 0; i < 12; i++)
        {
            *aStream >> mBoneCount[i];
        }

        *aStream
            >> mNotifyCount
            >> mAssetType;

        quint32 pad;
        *aStream >> pad;
        mPad = (pad != 0);

        qint32 namesPtr, dataBytePtr, dataShortPtr, dataIntPtr,
            randomDataShortPtr, randomDataBytePtr, randomDataIntPtr;

        *aStream
            >> mRandomDataShortCount
            >> mIndexCount
            >> mFramerate
            >> mFrequency
            >> namesPtr
            >> mNames
            >> dataBytePtr
            >> mDataByte
            >> dataShortPtr
            >> mDataShort
            >> dataIntPtr
            >> mDataInt
            >> randomDataShortPtr
            >> mRandomDataShort
            >> randomDataBytePtr
            >> mRandomDataByte
            >> randomDataIntPtr
            >> mRandomDataInt;

        // Parse indices
        mIndices.ParseData(aStream);
        mNotify.ParsePtr(aStream, false);
        mDeltaPart.ParsePtr(aStream, false);

        mName.ParseData(aStream);
        if (namesPtr)
        {

        }
        mNotify.ParseData(aStream);
        mDeltaPart.ParseData(aStream);
        if (dataBytePtr)
        {
            *aStream >> mDataByte;
        }
        if (dataShortPtr)
        {
            *aStream >> mDataShort;
        }
        if (dataIntPtr)
        {
            *aStream >> mDataInt;
        }
        if (randomDataShortPtr)
        {
            *aStream >> mDataInt;
        }
        if (randomDataBytePtr)
        {
            *aStream >> mRandomDataByte;
        }
        if (randomDataIntPtr)
        {
            *aStream >> mRandomDataInt;
        }
        mIndices.ParseData(aStream);
    }
}

void XAnimParts::Clear()
{

}
