#include "..\script_component.hpp"

/*
 * Function: forge_client_db_fnc_initDB
 * Author: J. Schmidt
 * 
 * Description:
 * Initializes the database system, creating the interface for other modules
 *
 * Arguments:
 * None
 *
 * Return Value:
 * Database interface object
 */

private _dbInterface = createHashMapObject [
    ["#type", "IDatabase"],
    ["#create", {
        _self set ["collections", createHashMap];

        if (isNil { missionNamespace getVariable QGVAR(storage) }) then {
            missionNamespace setVariable [QGVAR(storage), createHashMap];
        };

        ["db_initialized"] call CBA_fnc_localEvent;
    }],
    ["createCollection", {
        params [["_name", "", [""]], ["_schema", [], [[]]]];
        private _collections = _self get "collections";
        
        if (_name == "") exitWith {
            ERROR_MSG("Collection name cannot be empty");
            nil
        };
        
        if (_collections getOrDefault [_name, nil] isNotEqualTo nil) exitWith {
            _collections get _name
        };
        
        private _collection = createHashMapObject _schema;
        _collections set [_name, _collection];
        
        _collection
    }],
    ["getCollection", {
        params [["_name", "", [""]]];
        private _collections = _self get "collections";
        
        _collections getOrDefault [_name, nil]
    }]
];

SETMVAR(GVAR(store),_dbInterface);
GETMVAR(GVAR(store),nil)