#include "xgfxcell.h"

XGfxCell::XGfxCell()
    : XAsset()
    , mMins()
    , mMaxs()
    , mAabbTreeCount(0)
    , mAabbTree()
    , mPortalCount(0)
    , mPortals()
    , mCullGroupCount(0)
    , mCullGroups()
    , mReflectionProbeCount(0)
    , mReflectionProbes()
{
    SetName("GFX Cell");
}

XGfxCell::~XGfxCell()
{

}

void XGfxCell::ParseData(XDataStream *aStream)
{
    qint32 aabbTreePtr, portalsPtr, cullGroupsPtr, reflectionProbesPtr;

    mMins.setX(aStream->ParseSingle(QString("%1 mins x").arg(GetName())));
    mMins.setY(aStream->ParseSingle(QString("%1 mins y").arg(GetName())));
    mMins.setZ(aStream->ParseSingle(QString("%1 mins z").arg(GetName())));

    mMaxs.setX(aStream->ParseSingle(QString("%1 maxs x").arg(GetName())));
    mMaxs.setY(aStream->ParseSingle(QString("%1 maxs y").arg(GetName())));
    mMaxs.setZ(aStream->ParseSingle(QString("%1 maxs z").arg(GetName())));

    mAabbTreeCount = aStream->ParseInt32(QString("%1 aabb tree count").arg(GetName()));
    aabbTreePtr = aStream->ParseInt32(QString("%1 aabb tree ptr").arg(GetName()));

    mPortalCount = aStream->ParseInt32(QString("%1 portal count").arg(GetName()));
    portalsPtr = aStream->ParseInt32(QString("%1 portals ptr").arg(GetName()));

    mCullGroupCount = aStream->ParseInt32(QString("%1 cull group count").arg(GetName()));
    cullGroupsPtr = aStream->ParseInt32(QString("%1 cull groups ptr").arg(GetName()));

    mReflectionProbeCount = aStream->ParseUInt8(QString("%1 reflection probe count").arg(GetName()));
    reflectionProbesPtr = aStream->ParseInt32(QString("%1 reflection probes ptr").arg(GetName()));

    if (aabbTreePtr)
    {
        for (int i = 0; i < mAabbTreeCount; i++)
        {
            XGfxAabbTree newTree;
            newTree.ParseData(aStream);
            mAabbTree.append(newTree);
        }
    }

    if (portalsPtr)
    {
        for (int i = 0; i < mPortalCount; i++)
        {
            XGfxPortal newPortal;
            newPortal.ParseData(aStream);
            mPortals.append(newPortal);
        }
    }

    if (cullGroupsPtr)
    {
        for (int i = 0; i < mCullGroupCount; i++)
        {
            qint32 newCullGroup = aStream->ParseInt32(QString("%1 cull group %2").arg(GetName()).arg(i));
            mCullGroups.append(newCullGroup);
        }
    }

    if (reflectionProbesPtr)
    {
        for (int i = 0; i < mReflectionProbeCount; i++)
        {
            quint8 newReflectionProbe = aStream->ParseUInt8(QString("%1 reflection probe %2").arg(GetName()).arg(i));
            mReflectionProbes.append(newReflectionProbe);
        }
    }
}

void XGfxCell::Clear()
{
    mMins = QVector3D();
    mMaxs = QVector3D();
    mAabbTreeCount = 0;
    mAabbTree.clear();
    mPortalCount = 0;
    mPortals.clear();
    mCullGroupCount = 0;
    mCullGroups.clear();
    mReflectionProbeCount = 0;
    mReflectionProbes.clear();
}
