#include "xmaterial.h"

XMaterial::XMaterial()
    : XAsset()
    , mInfo()
    , mStateBitsEntry(26)
    , mTextureCount(0)
    , mConstantCount(0)
    , mStateBitsCount(0)
    , mStateFlags(0)
    , mCameraRegion(0)
    , mTechniqueSet()
    , mTextureTable()
    , mConstantTable()
    , mStateBitsTable()
{
    SetType(ASSET_TYPE_MATERIAL);
    SetName("Material");
}

XMaterial::~XMaterial()
{

}

void XMaterial::ParseData(XDataStream *aStream)
{
    mInfo.ParseData(aStream);

    for (int i = 0; i < 26; i++)
    {
        *aStream >> mStateBitsEntry[i];
        if (IsDebug())
        {
            qDebug() << QString("[%1] mStateBitsEntry[%2] = %3").arg(aStream->device()->pos(), 10, 10, QChar('0')).arg(i).arg(mStateBitsEntry[i]);
        }
    }


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

    aStream->skipRawData(1);

    mTechniqueSet.ParsePtr(aStream, false);
    mTextureTable.ParsePtr(aStream, false);
    mConstantTable.ParsePtr(aStream, false);
    mStateBitsTable.ParsePtr(aStream, false);

    mTechniqueSet.ParseData(aStream);
    mTextureTable.ParseData(aStream);
    mConstantTable.ParseData(aStream);
    mStateBitsTable.ParseData(aStream);
}

void XMaterial::Clear()
{
    mInfo.Clear();
    mStateBitsEntry.clear();
    mTextureCount = 0;
    mConstantCount = 0;
    mStateBitsCount = 0;
    mStateFlags = 0;
    mCameraRegion = 0;
    mTechniqueSet =  XMaterialTechniqueSet();
    mTextureTable =  XMaterialTextureDef();
    mConstantTable =  XMaterialConstantDef();
    mStateBitsTable =  XGfxStateBits();
}
