#include "..\script_component.hpp"

/*
 * Author: IDSolutions
 * Initializes the org class.
 *
 * Arguments:
 * None
 *
 * Return Value:
 * None
 *
 * Examples:
 * call forge_client_org_fnc_initOrgClass
 *
 * Public: Yes
 */

#pragma hemtt ignore_variables ["_self"]
GVAR(OrgClass) = createHashMapObject [[
    ["#type", "IOrgClass"],
    ["#create", {
        _self set ["uid", getPlayerUID player];
        _self set ["org", createHashMap];
        _self set ["isLoaded", false];
        _self set ["lastSave", time];

        private _org = createHashMap;
        _org set ["id", ""];
        _org set ["owner", ""];
        _org set ["name", ""];
        _org set ["funds", 0];
        _org set ["reputation", 0];
        _org set ["assets", createHashMap];
        _org set ["members", createHashMap];

        _self set ["org", _org];
    }],
    ["init", {
        private _uid = _self get "uid";
        private _org = _self get "org";

        [SRPC(org,requestInitOrg), [_uid, _org]] call CFUNC(serverEvent);

        systemChat format ["Org loaded for %1", (name player)];
        diag_log "[FORGE:Client:Org] Org Class Initialized!";
    }],
    ["save", {
        params [["_sync", false, [false]]];

        private _uid = _self get "uid";
        [SRPC(org,requestSaveOrg), [_uid, _sync]] call CFUNC(serverEvent);

        _self set ["lastSave", time];
    }],
    ["sync", {
        params [["_data", createHashMap, [createHashMap]], ["_jip", false, [false]]];

        private _isLoaded = _self get "isLoaded";
        private _org = _self get "org";

        if (_data isEqualTo createHashMap) exitWith {
            diag_log "[FORGE:Client:Org] Empty data received for sync, skipping.";
        };

        { _org set [_x, _y]; } forEach _data;

        _self set ["org", _org];

        if !(_isLoaded) then { _self set ["isLoaded", true]; };
        diag_log "[FORGE:Client:Org] Sync completed";
    }],
    ["get", {
        params [["_key", "", [""]], ["_default", nil, [[], "", 0, false, createHashMap]]];

        private _org = _self get "org";
        _org getOrDefault [_key, _default];
    }]
]];

GVAR(OrgClass)
