// ################################
// ##         Engine hry         ##
// ##                            ##
// ################################

#pragma once
#ifndef ENGINE_H
#define ENGINE_H

#include "common.h"

#include "independent_constants.h"
#include "protocol.h"
#include "time_axis.h"
#include "threads.h"
#include "server.h"

#include <string>
#include <map>
#include <list>

// --------------------------------

#define EMT_QUEUE_MAX_ITEMS 67108864

class engine;
class engineMainThread : public threads :: runnable
{
private:
    threads :: mutex queueLock_;
    threads :: mutex workLock_;
    threads :: semaphore sem_;

    std :: list<pair<id, message*>> requests_;
    std :: list<triggerEventClass*> events_;

    engine* eng_;
    volatile bool stopping_;
public:
    engineMainThread();
    void start(void);
    void stop(void);
    void operator()(threads :: thread* thd); 

    void beginWork(void);
    void endWork(void);

    void addRequest(id cl, message* ms);
    void addEvent(triggerEventClass* ev);
};

// ################################
// ################################

class engine
{
private:
    timeAxis tAxis_;
    engineMainThread emt_;
    threads :: thread* thread_;
    volatile bool active_;
	bool suppressClientUpdates_; // Urzova promenna, nedavat do databaze, ignorovat
public:
	engine();
    void start(void);
    void stop(void);

    void beginWork(void);
    void endWork(void);

    id processRequest(id cl, const std :: string& requestName, 
							std :: map<std :: string, std :: string>& parameters);

    void addRequest(id cl, message* ms);
    void addEvent(triggerEventClass* ev);
    void registerEvent(dv_time:: datetime gameTime, triggerEventClass* ev, unsigned long long& lastEventID);

	bool isClientUpdateSuppressed() { return this->suppressClientUpdates_; };
	void suppressClientUpdates   () { this->suppressClientUpdates_ = 1   ; };
	void renewClientUpdates      () { this->suppressClientUpdates_ = 0   ; };
};

// ################################

void init_request_table(void);

// ################################

#endif