#define variable
#include "Globals.h"

#include <Text.h>

#include "AddServerMenu.h"
#include "Game.h"
#include "Load.h"
#include "ServerSelection.h"

void initVariables()
{
    dlls = new DLLRegister();
    menuRegister = new HashMap<Framework::Text, Framework::RCPointer<Menu>>(
        10, [](Text txt) { return txt.hashCode(); });
    fontRegister = new HashMap<Framework::Text, Framework::RCPointer<Schrift>>(
        10, [](Text txt) { return txt.hashCode(); });
    window = 0;
    blockTypes = 0;
    blockTypeCount = 0;
    itemTypes = 0;
    itemTypeCount = 0;
    entityTypes = 0;
    entityTypeCount = 0;
}

void initMenus()
{
    LoadMenu *load = new LoadMenu(uiFactory.initParam.bildschirm);
    menuRegister->put("load",
        RCPointer<Menu>::of(load));
    menuRegister->put("serverSelection",
        RCPointer<Menu>::of(
            new ServerSelectionMenu(uiFactory.initParam.bildschirm)));
    menuRegister->put("addServer",
        RCPointer<Menu>::of(new AddServerMenu(uiFactory.initParam.bildschirm)));
    menuRegister->put(
        "game", RCPointer<Menu>::of(new Game(uiFactory.initParam.bildschirm)));

    menuRegister->get("load")->show();
}

void releaseVariables()
{
    if (World::INSTANCE) World::INSTANCE->release();
    menuRegister->release();
    fontRegister->release();
    dlls->release();
    for (int i = 0; i < blockTypeCount; i++)
        blockTypes[i]->release();
    delete[] blockTypes;
    for (int i = 0; i < itemTypeCount; i++)
        itemTypes[i]->release();
    delete[] itemTypes;
    for (int i = 0; i < entityTypeCount; i++)
        entityTypes[i]->release();
    delete[] entityTypes;
    blockTypes = 0;
    blockTypeCount = 0;
    itemTypes = 0;
    itemTypeCount = 0;
    entityTypes = 0;
    entityTypeCount = 0;
}

const ItemType* zItemType(int id)
{
    for (int i = 0; i < itemTypeCount; i++)
        if (itemTypes[i]->getId() == id) return itemTypes[i];
    return 0;
}