#include "..\script_component.hpp"

/*
 * File: fnc_initBankClass.sqf
 * Author: IDSolutions
 * Date: 2025-12-16
 * Last Update: 2026-01-30
 * Public: No
 *
 * Description:
 * Initializes the bank class.
 *
 * Arguments:
 * None
 *
 * Return Value:
 * Bank class object [HASHMAP OBJECT]
 *
 * Example:
 * call forge_client_bank_fnc_initBankClass
 */

#pragma hemtt ignore_variables ["_self"]
GVAR(BankClass) = createHashMapObject [[
    ["#type", "IBankClass"],
    ["#create", {
        _self set ["uid", getPlayerUID player];
        _self set ["account", createHashMap];
        _self set ["isLoaded", false];
        _self set ["lastSave", time];
    }],
    ["init", {
        private _uid = _self get "uid";

        [SRPC(bank,requestInitBank), [_uid]] call CFUNC(serverEvent);

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

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

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

        private _account = _self get "account";
        private _isLoaded = _self get "isLoaded";

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

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

        _self set ["account", _account];

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

        private _account = _self get "account";
        _account getOrDefault [_key, _default];
    }]
]];

GVAR(BankClass)
