#include "zonefile.h"
#include "xassetlist.h"
#include "xfile.h"

#include <QFile>
#include <QDataStream>
#include <QDebug>

ZoneFile::ZoneFile() :
    mStem(),
    mSize(),
    mTagCount(),
    mTags(),
    mRecordCount(),
    mRecords(),
    mPlatform(),
    mGame(),
    mTypeMap(),
    mHeaderData(),
    mAssetList(this)
{

}

ZoneFile::~ZoneFile() {

}

ZoneFile::ZoneFile(const ZoneFile &aZoneFile) {
    mStem = aZoneFile.mStem;
    mSize = aZoneFile.mSize;
    mTagCount = aZoneFile.mTagCount;
    mTags = aZoneFile.mTags;
    mRecordCount = aZoneFile.mRecordCount;
    mRecords = aZoneFile.mRecords;
    mAssetList = aZoneFile.mAssetList;
    mHeaderData = aZoneFile.mHeaderData;
}

ZoneFile &ZoneFile::operator=(const ZoneFile &other) {
    if (this != &other) {
        mStem = other.mStem;
        mSize = other.mSize;
        mTagCount = other.mTagCount;
        mTags = other.mTags;
        mRecordCount = other.mRecordCount;
        mRecords = other.mRecords;
        mAssetList = other.mAssetList;
        mHeaderData = other.mHeaderData;
    }
    return *this;
}

bool ZoneFile::Load(const QByteArray aFileData)
{
    QDataStream zoneStream(aFileData);

    mHeaderData.ParseData(&zoneStream);
    mAssetList.ParseData(&zoneStream);

    return true;
}

bool ZoneFile::Load(const QString aFilePath)
{
    QFile file(aFilePath);
    if (!file.open(QIODevice::ReadOnly))
    {
        qDebug() << "Failed to open file: " << aFilePath;
        return false;
    }

    return Load(file.readAll());
}

QByteArray ZoneFile::GetBinaryData() {
    return {};
}

QString ZoneFile::GetStem() const {
    return mStem;
}

QString ZoneFile::GetBaseStem() const  {
    return mStem.split('.').first();
}

quint32 ZoneFile::GetSize() const {
    return mSize;
}

quint32 ZoneFile::GetTagCount() const {
    return mTagCount;
}

QStringList ZoneFile::GetTags() const {
    return mTags;
}

QStringList ZoneFile::GetNames() const {
    QStringList result;

    for (int i = 0; i < mAssetList.Size(); i++)
    {
        auto current = mAssetList.GetAsset(i);
        result.append(current->GetName());
    }

    return result;
}

QVector<XAssetType> ZoneFile::GetTypes() const
{
    QVector<XAssetType> result;

    for (int i = 0; i < mAssetList.Size(); i++)
    {
        result.append(mAssetList.GetAsset(i)->GetType());
    }

    return result;
}

XAssetList ZoneFile::GetAssetList() const {
   return mAssetList;
}

void ZoneFile::SetStem(const QString aStem) {
    mStem = aStem;
}

void ZoneFile::SetSize(quint32 aSize) {
    mSize = aSize;
}

void ZoneFile::SetTagCount(quint32 aTagCount) {
    mTagCount = aTagCount;
}

void ZoneFile::SetTags(const QStringList aTags) {
    mTags = aTags;
}

void ZoneFile::SetRecordCount(quint32 aRecordCount) {
    mRecordCount = aRecordCount;
}

void ZoneFile::SetRecords(const QStringList aRecords) {
    mRecords = aRecords;
}

void ZoneFile::SetHeaderData(XFile aHeaderData)
{
    mHeaderData = aHeaderData;
}

void ZoneFile::SetAssetList(XAssetList aAssetList) {
    mAssetList = aAssetList;
}

XAssetType ZoneFile::GetType(quint32 aRawType) const
{
    if (mTypeMap.contains(aRawType))
    {
        return mTypeMap[aRawType];
    }
    return ASSET_TYPE_NONE;
}

void ZoneFile::pSetType(quint32 aRawType, XAssetType aType)
{
    mTypeMap[aRawType] = aType;
}
