#include "zonefile_cod4_360.h"
#include "utils.h"
#include "file.h"
#include "assetlist.h"

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

ZoneFile_COD4_360::ZoneFile_COD4_360()
    : ZoneFile() {

}

ZoneFile_COD4_360::~ZoneFile_COD4_360() {

}

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

    // DB_LoadXFileData()
    XFile headerData;
    zoneStream >> headerData.size << headerData.externalSize;
    for (int i = 0; i < 7; i++) {
        zoneStream >> headerData.blockSize[i];
    }

    // Load_XAssetListCustom();
    XAssetList assetList;
    zoneStream >> assetList.stringList.count;
    zoneStream.skipRawData(4);
    zoneStream >> assetList.assetCount;
    zoneStream.skipRawData(4);

    zoneStream.skipRawData(4);

    for (int i = 0; i < assetList.stringList.count; i++) {
        qint32 stringPtr;
        zoneStream >> stringPtr;

        assetList.stringList.stringPtrs.append(stringPtr);
    }

    for (int i = 0; i < assetList.stringList.count; i++) {
        qint32 currentStringPtr = assetList.stringList.stringPtrs[i];
        if (currentStringPtr == -1) {
            QString currentString;
            char currentChar;

            zoneStream >> currentChar;
            while (currentChar != '\0') {
                currentString.append(currentChar);
                zoneStream >> currentChar;
            }
            assetList.stringList.scriptStrings.append(currentString);
        }
    }
    zoneStream.skipRawData(4);

    for (int i = 0; i < assetList.assetCount; i++) {
        XAsset asset;
        zoneStream >> asset.type;
        zoneStream.skipRawData(4);
    }

    // for (int i = 0; i < assetList.assetCount; i++) {
    //     XAssetType type = assetList.assets[i].type;
    //     switch (type) {
    //     case ASSET_TYPE_XANIMPARTS:
    //         assets.assetMap.animations << assets.Load_Animation(&zoneStream);
    //         break;
    //     case ASSET_TYPE_XMODEL:
    //         assets.assetMap.models << assets.Load_Model(&zoneStream);
    //         break;
    //     case ASSET_TYPE_MATERIAL:
    //         assets.assetMap.materials << assets.Load_Material(&zoneStream);
    //         break;
    //     case ASSET_TYPE_PIXELSHADER:
    //         assets.assetMap.pixelShaders << assets.Load_MaterialPixelShader(&zoneStream);
    //         break;
    //     case ASSET_TYPE_TECHNIQUE_SET:
    //         assets.assetMap.techSets << assets.Load_MaterialTechSet(&zoneStream);
    //         break;
    //     case ASSET_TYPE_IMAGE:
    //         assets.assetMap.images << assets.Load_GfxImage(&zoneStream);
    //         break;
    //     case ASSET_TYPE_SOUND:
    //         assets.assetMap.sounds << assets.Load_SoundAliasList(&zoneStream);
    //         break;
    //     case ASSET_TYPE_SOUND_CURVE:
    //         assets.assetMap.soundCurves << assets.Load_SoundCurve(&zoneStream);
    //         break;
    //     case ASSET_TYPE_LOADED_SOUND:
    //         assets.assetMap.loadedSounds << assets.Load_LoadedSound(&zoneStream);
    //         break;
    //     case ASSET_TYPE_CLIPMAP:
    //     case ASSET_TYPE_CLIPMAP_PVS:
    //         assets.assetMap.clipMaps << assets.Load_ClipMap(&zoneStream);
    //         break;
    //     case ASSET_TYPE_COMWORLD:
    //         assets.assetMap.comWorlds << assets.Load_ComWorld(&zoneStream);
    //         break;
    //     case ASSET_TYPE_GAMEWORLD_SP:
    //         assets.assetMap.gameWorldSPs << assets.Load_GameWorldSp(&zoneStream);
    //         break;
    //     case ASSET_TYPE_GAMEWORLD_MP:
    //         assets.assetMap.gameWorldMPs << assets.Load_GameWorldMp(&zoneStream);
    //         break;
    //     case ASSET_TYPE_MAP_ENTS:
    //         assets.assetMap.mapEntities << assets.Load_MapEnts(&zoneStream);
    //         break;
    //     case ASSET_TYPE_GFXWORLD:
    //         assets.assetMap.gfxWorlds << assets.Load_GfxWorld(&zoneStream);
    //         break;
    //     case ASSET_TYPE_LIGHT_DEF:
    //         assets.assetMap.gfxLightDefs << assets.Load_GfxLightPtr(&zoneStream);
    //         break;
    //     case ASSET_TYPE_FONT:
    //         assets.assetMap.fonts << assets.Load_GameFont(&zoneStream);
    //         break;
    //     case ASSET_TYPE_MENULIST:
    //         assets.assetMap.menuLists << assets.Load_MenuList(&zoneStream);
    //         break;
    //     case ASSET_TYPE_MENU:
    //         assets.assetMap.menuDefinitions << assets.Load_MenuDef(&zoneStream);
    //         break;
    //     case ASSET_TYPE_LOCALIZE_ENTRY:
    //         assets.assetMap.localizeEntries << assets.Load_LocalizeEntry(&zoneStream);
    //         break;
    //     case ASSET_TYPE_WEAPON:
    //         assets.assetMap.weaponDefinitions << assets.Load_WeaponDef(&zoneStream);
    //         break;
    //     case ASSET_TYPE_SNDDRIVER_GLOBALS:
    //         assets.assetMap.soundDrivers << assets.Load_SoundDriver(&zoneStream);
    //         break;
    //     case ASSET_TYPE_FX:
    //         assets.assetMap.effectDefinitions << assets.Load_EffectDef(&zoneStream);
    //         break;
    //     case ASSET_TYPE_IMPACT_FX:
    //         assets.assetMap.impactTables << assets.Load_ImpactTable(&zoneStream);
    //         break;
    //     case ASSET_TYPE_RAWFILE:
    //         assets.assetMap.rawFiles << assets.Load_RawFile(&zoneStream);
    //         break;
    //     case ASSET_TYPE_STRINGTABLE:
    //         assets.assetMap.stringTables << assets.Load_StringTable(&zoneStream);
    //         break;
    //     case ASSET_TYPE_COUNT:
    //     case ASSET_TYPE_ASSETLIST:
    //     case ASSET_TYPE_XMODELPIECES:
    //     case ASSET_TYPE_PHYSPRESET:
    //     case ASSET_TYPE_UI_MAP:
    //     case ASSET_TYPE_AITYPE:
    //     case ASSET_TYPE_MPTYPE:
    //     case ASSET_TYPE_CHARACTER:
    //     case ASSET_TYPE_XMODELALIAS:
    //     default:
    //         break;
    //     }
    // }

    return true;
}

Utils::AssetType ZoneFile_COD4_360::AssetStrToEnum(const QString aAssetType) {
    const QString cleanedType = aAssetType.toUpper();
    if (cleanedType == "00000000") {
        return Utils::ASSET_XMODELPIECES;
    } else if (cleanedType == "00000001") {
        return Utils::ASSET_PHYSPRESET;
    } else if (cleanedType == "00000002") {
        return Utils::ASSET_Animation;
    } else if (cleanedType == "00000003") {
        return Utils::ASSET_XMODEL;
    } else if (cleanedType == "00000004") {
        return Utils::ASSET_MATERIAL;
    } else if (cleanedType == "00000005") {
        return Utils::ASSET_PIXELSHADER;
    } else if (cleanedType == "00000006") {
        return Utils::ASSET_TECHNIQUE_SET;
    } else if (cleanedType == "00000007") {
        return Utils::ASSET_IMAGE;
    } else if (cleanedType == "00000008") {
        return Utils::ASSET_SOUND;
    } else if (cleanedType == "00000009") {
        return Utils::ASSET_SOUND_CURVE;
    } else if (cleanedType == "0000000A") {
        return Utils::ASSET_LOADED_SOUND;
    } else if (cleanedType == "0000000B") {
        return Utils::ASSET_CLIPMAP;
    } else if (cleanedType == "0000000C") {
        return Utils::ASSET_CLIPMAP_PVS;
    } else if (cleanedType == "0000000D") {
        return Utils::ASSET_COMWORLD;
    } else if (cleanedType == "0000000E") {
        return Utils::ASSET_GAMEWORLD_SP;
    } else if (cleanedType == "0000000F") {
        return Utils::ASSET_GAMEWORLD_MP;
    } else if (cleanedType == "00000010") {
        return Utils::ASSET_MAP_ENTS;
    } else if (cleanedType == "00000011") {
        return Utils::ASSET_GFXWORLD;
    } else if (cleanedType == "00000012") {
        return Utils::ASSET_LIGHT_DEF;
    } else if (cleanedType == "00000013") {
        return Utils::ASSET_UI_MAP;
    } else if (cleanedType == "00000014") {
        return Utils::ASSET_FONT;
    } else if (cleanedType == "00000015") {
        return Utils::ASSET_MENULIST;
    } else if (cleanedType == "00000016") {
        return Utils::ASSET_MENU;
    } else if (cleanedType == "00000017") {
        return Utils::ASSET_LOCALIZE_ENTRY;
    } else if (cleanedType == "00000018") {
        return Utils::ASSET_WEAPON;
    } else if (cleanedType == "00000019") {
        return Utils::ASSET_SNDDRIVER_GLOBALS;
    } else if (cleanedType == "0000001A") {
        return Utils::ASSET_FX;
    } else if (cleanedType == "0000001B") {
        return Utils::ASSET_IMPACT_FX;
    } else if (cleanedType == "0000001C") {
        return Utils::ASSET_AITYPE;
    } else if (cleanedType == "0000001D") {
        return Utils::ASSET_MPTYPE;
    } else if (cleanedType == "0000001E") {
        return Utils::ASSET_CHARACTER;
    } else if (cleanedType == "0000001F") {
        return Utils::ASSET_XMODELALIAS;
    } else if (cleanedType == "00000020") {
        return Utils::ASSET_RAWFILE;
    } else if (cleanedType == "00000021") {
        return Utils::ASSET_STRINGTABLE;
    } else if (cleanedType == "00000022") {
        return Utils::ASSET_COUNT;
    } else if (cleanedType == "00000022") {
        return Utils::ASSET_STRING;
    } else if (cleanedType == "00000023") {
        return Utils::ASSET_ASSETLIST;
    }
    return Utils::ASSET_NONE;
}
