#include "xfxelemdef.h"

XFxElemDef::XFxElemDef()
    : XAsset()
    , mFlags(0)
    , mSpawn()
    , mSpawnRange()
    , mFadeInRange()
    , mFadeOutRange()
    , mSpawnFrustumCullRadius(0)
    , mSpawnDelayMsec()
    , mLifeSpanMsec()
    , mSpawnOrigin(3)
    , mSpawnOffsetRadius()
    , mSpawnOffsetHeight()
    , mSpawnAngles(3)
    , mAngularVelocity(3)
    , mInitialRotation()
    , mGravity()
    , mReflectionFactor()
    , mAtlas()
    , mElemType(0)
    , mVisualCount(0)
    , mVelIntervalCount(0)
    , mVisStateIntervalCount(0)
    , mVelSamplesPtr(0)
    , mVelSamples()
    , mVisSamplesPtr(0)
    , mVisSamples()
    , mVisuals()
    , mCollMins()
    , mCollMaxs()
    , mEffectOnImpact()
    , mEffectOnDeath()
    , mEffectEmitted()
    , mEmitDist()
    , mEmitDistVariance()
    , mTrailDef()
    , mSortOrder(0)
    , mLightingFrac(0)
    , mUseItemClip(0)
{

}

XFxElemDef::~XFxElemDef()
{
    
}

void XFxElemDef::Clear()
{
    mFlags = 0;
    mSpawn.Clear();
    mSpawnRange.Clear();
    mFadeInRange.Clear();
    mFadeOutRange.Clear();
    mSpawnFrustumCullRadius = 0;
    mSpawnDelayMsec.Clear();
    mLifeSpanMsec.Clear();
    mSpawnOrigin.clear();
    mSpawnOffsetRadius.Clear();
    mSpawnOffsetHeight.Clear();
    mSpawnAngles.clear();
    mAngularVelocity.clear();
    mInitialRotation.Clear();
    mGravity.Clear();
    mReflectionFactor.Clear();
    mAtlas.Clear();
    mElemType = 0;
    mVisualCount = 0;
    mVelIntervalCount = 0;
    mVisStateIntervalCount = 0;
    mVelSamplesPtr = 0;
    mVelSamples.clear();
    mVisSamplesPtr = 0;
    mVisSamples.clear();
    mVisuals.Clear();
    mCollMins = QVector3D();
    mCollMaxs = QVector3D();
    mEffectOnImpact.Clear();
    mEffectOnDeath.Clear();
    mEffectEmitted.Clear();
    mEmitDist.Clear();
    mEmitDistVariance.Clear();
    mTrailDef.Clear();
    mSortOrder = 0;
    mLightingFrac = 0;
    mUseItemClip = 0;
}

void XFxElemDef::ParseData(QDataStream *aStream)
{
    *aStream >> mFlags;

    mSpawn.ParseData(aStream);
    mSpawnRange.ParseData(aStream);
    mFadeInRange.ParseData(aStream);
    mFadeOutRange.ParseData(aStream);

    *aStream >> mSpawnFrustumCullRadius;

    mSpawnDelayMsec.ParseData(aStream);
    mLifeSpanMsec.ParseData(aStream);
    for (int i = 0; i < 3; i++)
    {
        mSpawnOrigin[i].ParseData(aStream);
    }
    mSpawnOffsetRadius.ParseData(aStream);
    mSpawnOffsetHeight.ParseData(aStream);
    for (int i = 0; i < 3; i++)
    {
        mSpawnAngles[i].ParseData(aStream);
    }
    for (int i = 0; i < 3; i++)
    {
        mAngularVelocity[i].ParseData(aStream);
    }
    mInitialRotation.ParseData(aStream);
    mGravity.ParseData(aStream);
    mReflectionFactor.ParseData(aStream);
    mAtlas.ParseData(aStream);

    qint32 velSamplesPtr, visSamplesPtr;
    *aStream
        >> mElemType
        >> mVisualCount
        >> mVelIntervalCount
        >> mVisStateIntervalCount
        >> velSamplesPtr
        >> visSamplesPtr;

    mVisuals.ParseData(aStream);

    *aStream
        >> mCollMins[0]
        >> mCollMins[1]
        >> mCollMins[2]
        >> mCollMaxs[0]
        >> mCollMaxs[1]
        >> mCollMaxs[2];

    mEffectOnImpact.ParseData(aStream);
    mEffectOnDeath.ParseData(aStream);
    mEffectEmitted.ParseData(aStream);
    mEmitDist.ParseData(aStream);
    mEmitDistVariance.ParseData(aStream);
    mEffectOnImpact.ParseData(aStream);

    qint32 trailDefPtr;
    *aStream
        >> trailDefPtr
        >> mSortOrder
        >> mLightingFrac
        >> mUseItemClip;

    aStream->skipRawData(1);

    if (velSamplesPtr)
    {
        for (int i = 0; i < mVelIntervalCount + 1; i++)
        {
            XFxElemVelStateSample newVelSample;
            newVelSample.ParseData(aStream);
            mVelSamples.append(newVelSample);
        }
    }

    if (visSamplesPtr)
    {
        for (int i = 0; i < mVisStateIntervalCount + 1; i++)
        {
            XFxElemVisStateSample newVisSample;
            newVisSample.ParseData(aStream);
            mVisSamples.append(newVisSample);
        }
    }

    // TODO: Finish this ugh
}
