#include "xaudiochannelmap.h"

XAudioChannelMap::XAudioChannelMap()
    : XAsset()
    , mEntryCount(0)
    , mEntries(QVector<XAudioChannelMapEntry*>())
{
    SetName("Audio Channel Map");
}

XAudioChannelMap::~XAudioChannelMap()
{
    for (int i = 0; i < mEntries.size(); i++)
    {
        delete mEntries[i];
    }
    mEntries.clear();
}

quint8 XAudioChannelMap::GetEntryCount() const
{
    return mEntryCount;
}

XAudioChannelMapEntry *XAudioChannelMap::GetMapEntry(int aIndex) const
{
    return mEntries[aIndex];
}

void XAudioChannelMap::SetMapEntry(int aIndex, XAudioChannelMapEntry *aEntry)
{
    mEntries[aIndex] = aEntry;
}

void XAudioChannelMap::ParseData(XDataStream *aStream)
{
    mEntryCount = aStream->ParseUInt8(QString("%1 entry count").arg(GetName()));

    aStream->skipRawData(3);

    quint32 entriesPtr = aStream->ParseInt32(QString("%1 entries ptr").arg(GetName()));
    if (entriesPtr)
    {
        mEntries.resize(mEntryCount);
        for (int i = 0; i < mEntryCount; i++)
        {
            mEntries[i] = new XAudioChannelMapEntry();
            mEntries[i]->ParseData(aStream);
        }
    }
}

void XAudioChannelMap::Clear()
{
    mEntryCount = 0;

    for (int i = 0; i < mEntries.size(); i++)
    {
        delete mEntries[i];
    }
    mEntries.clear();
}
