#include "script_component.hpp"

[QGVAR(handleEvents), {
    params ["_control", "_isConfirmDialog", "_message"];

    diag_log text format ["[FORGE::Client::Bank::XEH_postInit::handleEvents] Received event: '%1'", _message];

    _message = fromJSON _message;
    private _event = _message get "event";
    private _data = _message get "data";

    switch (_event) do {
        case "REQUEST::PLAYER::DATA": {
            private _playerData = createHashMap;
            private _playerList = [];

            {
                private _uid = getPlayerUID _x;
                private _name = name _x;
                private _funds = GETVAR(_x,FORGE_Bank,0); //TODO: Implement funds from server
                private _cash = GETVAR(_x,FORGE_Cash,0); //TODO: Implement cash from server

                private _playerInfo = createHashMapFromArray [
                    ["uid", _uid],
                    ["name", _name],
                    ["funds", _funds],
                    ["cash", _cash]
                ];

                _playerList pushBack _playerInfo;
            } forEach allPlayers;

            _playerData set ["players", _playerList];
            _control ctrlWebBrowserAction ["ExecJS", format ["handlePlayerDataRequest(%1)", (toJSON _playerList)]];
        };
        case "REQUEST::PLAYER::FUNDS": {
            private _playerData = createHashMap;
            private _balance = GETVAR(player,FORGE_Bank,0); //TODO: Implement balance from server
            private _cash = GETVAR(player,FORGE_Cash,0); //TODO: Implement cash from server

            private _payMultiplier = "MULTIPLYR" call BFUNC(getParamValue);
            private _rating = rating player; //TODO: Implement rating from server
            private _pending = _rating * _payMultiplier; //TODO: Implement pending from server

            private _playerData = createHashMapFromArray [
                ["balance", _balance],
                ["cash", _cash],
                ["pending", _pending]
            ];

            _control ctrlWebBrowserAction ["ExecJS", format ["handlePlayerFundsRequest(%1)", (toJSON _playerData)]];
        };
        case "REQUEST::TRANSACTION::HISTORY": {
            private _history = []; //TODO: Implement history from server
            private _historyData = createHashMapFromArray [["history", _history]];

            _control ctrlWebBrowserAction ["ExecJS", format ["handleTransactionHistoryRequest(%1)", (toJSON _historyData)]];
        };
        case "DEPOSIT::FUNDS": {
            _data params [["_amount", 0, [0]]];

            if (_amount <= 0) exitWith { systemChat "Invalid amount, must be greater than 0!"; false };
            ["forge_server_bank_handleEvents", ["BANK::DEPOSIT", [getPlayerUID player, _amount]]] call CFUNC(serverEvent);

            true
        };
        case "SUBMIT::TIMESHEET": {
            private _rating = rating player;
            private _uid = getPlayerUID player;

            ["forge_server_bank_handleEvents", ["BANK::SUBMIT::TIMESHEET", [_uid, _rating]]] call CFUNC(serverEvent);

            player addRating -_rating;

            true
        };
        case "TRANSFER::FUNDS": {
            _data params [["_uid", "", [""]], ["_amount", 0, [0]]];

            if ({_uid isEqualTo ""} || _amount <= 0) exitWith { systemChat "Invalid UID or amount, UID cannot be empty and amount must be greater than 0!"; false };
            ["forge_server_bank_handleEvents", ["BANK::TRANSFER", [getPlayerUID player, _uid, _amount]]] call CFUNC(serverEvent);

            true
        };
        case "WITHDRAW::FUNDS": {
            _data params [["_amount", 0, [0]]];

            if (_amount <= 0) exitWith { systemChat "Invalid amount, amount must be greater than 0!"; false };
            ["forge_server_bank_handleEvents", ["BANK::WITHDRAW", [getPlayerUID player, _amount]]] call CFUNC(serverEvent);

            true
        };
        default {
            diag_log format ["[FORGE::Client::Bank::XEH_postInit::handleEvents] Unhandled event: %1", _event];
        };
    };
}] call CFUNC(addEventHandler);