#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("Animation Parts");
}

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

        mName.ParsePtr(aStream, false);

        // Parse all fields
        mDataByteCount = aStream->ParseUInt32(QString("%1 data byte count").arg(GetName()));
        mDataShortCount = aStream->ParseUInt32(QString("%1 data short count").arg(GetName()));
        mDataIntCount = aStream->ParseUInt32(QString("%1 data int count").arg(GetName()));
        mRandomDataByteCount = aStream->ParseUInt32(QString("%1 random date byte count").arg(GetName()));
        mRandomDataIntCount = aStream->ParseUInt32(QString("%1 random data int count").arg(GetName()));
        mNumFrames = aStream->ParseUInt32(QString("%1 # frames").arg(GetName()));

        quint8 loopDelta = aStream->ParseUInt8(QString("%1 loop delta").arg(GetName()));
        mIsLoop = (loopDelta & 0x1) != 0;
        mIsDelta = (loopDelta & 0x2) != 0;

        for (int i = 0; i < 12; i++)
        {
            mBoneCount[i] = aStream->ParseUInt8(QString("%1 bone count %2").arg(GetName()).arg(i));
        }

        mNotifyCount = aStream->ParseUInt8(QString("%1 notify count").arg(GetName()));
        mAssetType = aStream->ParseUInt8(QString("%1 asset type").arg(GetName()));
        mPad = aStream->ParseUInt32(QString("%1 pad").arg(GetName())) != 0;

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

        mRandomDataShortCount = aStream->ParseUInt32(QString("%1 random data short count").arg(GetName()));
        mIndexCount = aStream->ParseUInt32(QString("%1 index count").arg(GetName()));
        mFramerate = aStream->ParseSingle(QString("%1 frame rate").arg(GetName()));
        mFrequency = aStream->ParseSingle(QString("%1 frequency").arg(GetName()));
        namesPtr = aStream->ParseInt32(QString("%1 names ptr").arg(GetName()));
        dataBytePtr = aStream->ParseInt32(QString("%1 data byte ptr").arg(GetName()));
        dataShortPtr = aStream->ParseInt32(QString("%1 data short ptr").arg(GetName()));
        dataIntPtr = aStream->ParseInt32(QString("%1 data int ptr").arg(GetName()));
        randomDataShortPtr = aStream->ParseInt32(QString("%1 random data short ptr").arg(GetName()));
        randomDataBytePtr = aStream->ParseInt32(QString("%1 random data byte ptr").arg(GetName()));
        randomDataIntPtr = aStream->ParseInt32(QString("%1 random data int ptr").arg(GetName()));

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

        mName.ParseData(aStream);
        if (namesPtr)
        {
            mNames = aStream->ParseInt32(QString("%1 names").arg(GetName()));
        }
        mNotify.ParseData(aStream);
        mDeltaPart.ParseData(aStream);
        if (dataBytePtr)
        {
            mDataByte = aStream->ParseUInt8(QString("%1 data byte").arg(GetName()));
        }
        if (dataShortPtr)
        {
            mDataShort = aStream->ParseInt16(QString("%1 data short").arg(GetName()));
        }
        if (dataIntPtr)
        {
            mDataInt = aStream->ParseInt32(QString("%1 data int").arg(GetName()));
        }
        if (randomDataShortPtr)
        {
            mRandomDataShort = aStream->ParseInt16(QString("%1 random data short").arg(GetName()));
        }
        if (randomDataBytePtr)
        {
            mRandomDataByte = aStream->ParseUInt8(QString("%1 random data byte").arg(GetName()));
        }
        if (randomDataIntPtr)
        {
            mRandomDataInt = aStream->ParseInt32(QString("%1 random data int").arg(GetName()));
        }
        mIndices.ParseData(aStream);
    }
}

void XAnimParts::Clear()
{

}
