// ################################
// ##        Kontext hry         ##
// ##                            ##
// ################################

#pragma once
#ifndef CONTEXT_H
#define CONTEXT_H

#include "common.h"

#include "dbconn.h"
#include "cache_list.h"
#include "server.h"
#include "datetime.h"

#include "engine.h"
#include "spaceindex.h"
#include "space.h"

// ================================

enum startingGameResult
{
	SGR_OK,
	SGR_SPACE,
	SGR_FAIL
};

// ================================

class context 
{
public:
    /* databaze */
    dbClient db;

    /* cache listy */
    cacheRealmsListClass            cacheRealmsList;
	cacheFortressesListClass        cacheFortressesList;
	cacheFortressClassesListClass   cacheFortressClassesList;
    cacheItemsListClass             cacheItemsList;
    cacheItemsPatternsListClass     cacheItemsPatternsList;
	cacheNullItemsListClass         cacheNullItemsList;
    cacheSpaceObjectListClass       cacheSpaceObjectList;

    spaceIndex                      spaceObjectIndex;
	spacePlanetObject             * startingPlanet;

    dv_time :: datetime             lastSynchronization;

    void initialize_cache_lists(void);
    void initialize_events(void);

    void synchronize(bool force = false);

    static const unsigned long index = 0;
};

/* forward deklarace */
class engine;

/* globalni kontext */
class global_context {
public:
    /* server */
    server srv;

    /* engine hry */
    engine eng;

    void closeClient(id clientID);
    void sendToClient(id clientID, char* message);

    void startEngine(void);
    void stopEngine(void);

	startingGameResult startGame(const std :: string& server, const std :: string& database, 
		const std :: string& username, const std :: string& password, int port, int maxConnections);
    void stopGame();

    bool createUniverse(const std :: string& server, const std :: string& database, 
		const std :: string& username, const std :: string& password, dist_t stars);
};

extern context* actx;
extern global_context gctx;

context* getContextById(unsigned long contextID);
unsigned long getActualContextID(void);
void setContextById(unsigned long contextID);

// ================================

void updateFortress(dtIdentifier fortressID, dtIdentifier realmID);
void startReadOnlyRequest();
void stopReadOnlyRequest();

// ================================

#endif