#include "..\script_component.hpp"

/*
 * Function: forge_client_org_fnc_initOrgStore
 * Author: J. Schmidt
 * 
 * Description:
 * Initializes player organization data using the database interface
 *
 * Arguments:
 * None
 *
 * Return Value:
 * Organization interface <OBJECT>
 */

private _orgStoreInterface = createHashMapObject [[
    ["#type", "IOrganizationStore"],
    ["#create", {
        private _store = GETMVAR(FORGE_STORE_REG,nil);

        if (isNil "_store") exitWith { ERROR_MSG("Store not initialized"); false };

        private _orgStore = _store call ["getStore", ["organizations"]];
        private _orgNameIndex = _store call ["getStore", ["organizationNameIndex"]];
        private _orgRegistry = GETVAR(profileNamespace,FORGE_ORG_REG,createHashMap);

        if (isNil "_orgStore" || isNil "_orgNameIndex") then { _orgStore = _store call ["createStore", ["organizations"]]; };
        if (isNil "_orgNameIndex") then { _orgNameIndex = _store call ["createStore", ["organizationNameIndex"]]; };

        private _name = _orgRegistry get "name";
        private _uid = _orgRegistry get "owner";

        if (!isNil "_name" && !isNil "_uid") then {
            private _key = format ["%1_%2", _uid, _name];

            _orgStore set [_key, _orgRegistry];
            _orgNameIndex set [_key, _name];
        };

        true
    }],
    ["post", {
        params [["_uid", "", [""]], ["_name", "", [""]], ["_initialFunds", 0, [0]], ["_initialReputation", 0, [0]]];

        if (_uid == "" || _name == "") exitWith { ERROR_MSG("Owner UID and, or name cannot be empty"); nil };

        private _store = GETMVAR(FORGE_STORE_REG,nil);
        private _key = format ["%1_%2", _uid, _name];

        private _timestamp = systemTimeUTC apply { if (_x < 10) then { "0" + str _x } else { str _x }};
        private _dateTime = format ["%1-%2-%3_%4:%5:%6.%7", _timestamp#0, _timestamp#1, _timestamp#2, _timestamp#3, _timestamp#4, _timestamp#5, _timestamp#6];

        private _orgRegistry = GETVAR(profileNamespace,FORGE_ORG_REG,createHashMap);

        private _existingOrgKey = _store call ["get", ["organizationNameIndex", _key]];
        private _existingOrg = nil;
        private _orgData = nil;

        if !(isNil "_existingOrgKey") then {
            _existingOrg = _store call ["get", ["organizations", _existingOrgKey]];
        };

        if !(isNil "_existingOrg") then {
            _orgData = _existingOrg;
        } else {
            _orgData = createHashMapFromArray [
                ["name", _name],
                ["owner", _uid],
                ["funds", _initialFunds],
                ["reputation", _initialReputation],
                ["assets", createHashMap],
                ["members", createHashMap],
                ["invites", createHashMap],
                ["logs", []],
                ["created", _dateTime]
            ];

            _orgRegistry set [_key, _orgData];

            SETVAR(profileNamespace,FORGE_ORG_REG,_orgRegistry);
            saveProfileNamespace;
        };

        _store call ["set", ["organizationNameIndex", _key, _name]];
        _store call ["set", ["organizations", _key, _orgData]];

        _orgData
    }],
    ["get", {
        private _store = GETMVAR(FORGE_STORE_REG,nil);
        private _orgStore = _store call ["getStore", ["organizations"]];
        
        if (isNil "_orgStore") exitWith { createHashMap };

        _orgStore
    }],
    ["getByKey", {
        params [["_key", "", [""]]];

        if (_key == "") exitWith { ERROR_MSG("Key cannot be empty"); nil };

        private _store = GETMVAR(FORGE_STORE_REG,nil);
        private _org = _store call ["get", ["organizations", _key]];

        if (isNil "_org") exitWith { ERROR_MSG_1("Organization with composite key %1 not found",_key); nil };

        _org
    }],
    ["delete", {
        params [["_key", "", [""]]];

        if (_key == "") exitWith { ERROR_MSG("Key cannot be empty"); false };

        private _store = GETMVAR(FORGE_STORE_REG,nil);
        private _key = _store call ["get", ["organizationNameIndex", _key, nil]];

        _store call ["delete", ["organizationNameIndex", _key]];

        if (isNil "_key") exitWith { ERROR_MSG_1("Key for organization not found: %1",_key); false };

        _store call ["delete", ["organizations", _key]];

        private _orgRegistry = GETVAR(profileNamespace,FORGE_ORG_REG,createHashMap);
        _orgRegistry deleteAt _key;
        SETVAR(profileNamespace,FORGE_ORG_REG,_orgRegistry);
        saveProfileNamespace;

        true
    }]
]];

SETMVAR(FORGE_ORG_STORE_REG,_orgStoreInterface);
GETMVAR(FORGE_ORG_STORE_REG,nil)