#include "script_component.hpp"

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

    diag_log format ["[FORGE::Client::Admin::XEH_postInit] 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 _player = _x;
                private _uid = getPlayerUID _player;
                private _name = name _player;
                private _paygrade = GETVAR(_player,FORGE_PayGrade,QUOTE(E1)); //TODO: Implement paygrade from server
                private _funds = GETVAR(_player,FORGE_Bank,0); //TODO: Implement funds from server

                private _playerInfo = createHashMapFromArray [
                    ["uid", _uid],
                    ["name", _name],
                    ["paygrade", _paygrade],
                    ["funds", _funds],
                    ["side", str (side _player)]
                ];

                _playerList pushBack _playerInfo;
            } forEach allPlayers;

            _playerData set ["players", _playerList];
            _control ctrlWebBrowserAction ["ExecJS", format ["handlePlayerDataRequest(%1)", (toJSON _playerList)]];
        };
        case "REQUEST::PAYGRADE::DATA": {
            private _payGrades = (missionConfigFile >> "CfgPaygrades" >> "payGrades") call BIS_fnc_getCfgData;
            private _paygradeData = createHashMap;
            private _paygradeList = [];

            {
                private _paygradeInfo = createHashMapFromArray [
                    ["paygrade", _x select 0],
                    ["bonus", _x select 1]
                ];

                _paygradeList pushBack _paygradeInfo;
            } forEach _payGrades;

            _paygradeData set ["paygrades", _paygradeList];
            _control ctrlWebBrowserAction ["ExecJS", format ["handlePaygradeDataRequest(%1)", (toJSON _paygradeList)]];
        };
        case "BROADCAST::MESSAGE": {
            _data params ["_uid", "_message"];

            if ((isNil "_message") || {_message isEqualTo ""}) exitWith { systemChat "Message cannot be empty!"; };

            ["forge_server_admin_handleEvents", ["ADMIN::SEND::MESSAGE", [_uid, _message]]] call CFUNC(serverEvent);
        };
        case "SEND::MESSAGE": {
            _data params ["_uid", "_message"];

            if ((isNil "_uid") || {_uid isEqualTo ""}) exitWith { systemChat "You did not select a player!"; };

            ["forge_server_admin_handleEvents", ["ADMIN::SEND::MESSAGE", [_uid, _message]]] call CFUNC(serverEvent);
        };
        case "UPDATE::PAYGRADE": {
            _data params ["_uid", "_paygrade"];

            if ((isNil "_uid") || {_uid isEqualTo ""}) exitWith { systemChat "You did not select a player!"; };

            ["forge_server_admin_handleEvents", ["ADMIN::UPDATE::PAYGRADE", [_uid, _paygrade]]] call CFUNC(serverEvent);
        };
        case "HANDLE::TRANSFER": {
            _data params ["_condition", "_amount", "_uid"];

            ["forge_server_admin_handleEvents", ["ADMIN::TRANSFER", [_condition, _amount, _uid]]] call CFUNC(serverEvent);
        };
        case "ADVANCE::ALL": {
            _data params ["_amount"];

            ["forge_server_admin_handleEvents", ["ADMIN::ADVANCE::ALL", [_amount]]] call CFUNC(serverEvent);
        };
        case "HANDLE::PAYDAY": {
            ["forge_server_admin_handleEvents", ["ADMIN::PAYDAY"]] call CFUNC(serverEvent);
        };
        default {
            diag_log format ["[FORGE::Client::Admin::XEH_postInit] Unhandled event: %1", _event];
        };
    };
}] call CFUNC(addEventHandler);