#include "..\script_component.hpp"

/*
 * File: fnc_handleUIEvents.sqf
 * Author: IDSolutions
 * Date: 2025-12-16
 * Last Update: 2026-01-30
 * Public: No
 *
 * Description:
 * Handles the UI events.
 *
 * Arguments:
 * 0: [CONTROL] - The control that triggered the event
 * 1: [BOOL] - Whether the event is from a confirm dialog
 * 2: [STRING] - The message containing the event data
 *
 * Return Value:
 * UI events handled [BOOL]
 *
 * Example:
 * call forge_client_bank_fnc_handleUIEvents;
 */

params ["_control", "_isConfirmDialog", "_message"];

private _alert = fromJSON _message;
private _event = _alert get "event";
private _data = _alert get "data";
// private _display = displayChild findDisplay 46;

private _uid = GVAR(BankClass) get "uid";
private _account = GVAR(BankClass) get "account";
private _cash = _account get "cash";
private _bank = _account get "bank";
private _pin = _account get "pin";

diag_log format ["[FORGE:Client:Bank] Handling UI event: %1 with data: %2", _event, _data];

switch (_event) do {
    // ========================================================================
    // DATA REQUESTS
    // ========================================================================
    case "bank::sync": {
        private _org = 0; // TODO: Get org balance
        private _players = SREG(bank,IndexRegistry);
        private _accountData = createHashMapFromArray [
            ["uid", _uid],
            ["cash", _cash],
            ["bank", _bank],
            ["org", _org],
            ["pin", _pin],
            ["players", _players]
        ];

        _control ctrlWebBrowserAction ["ExecJS", format ["syncDataFromArma(%1)", toJSON _accountData]];
    };

    // ========================================================================
    // BANK OPERATIONS
    // ========================================================================
    case "bank::deposit": {
        private _amount = _data get "amount";
        if (_amount > _cash) exitWith { hint "Insufficient cash!"; };

        [SRPC(bank,requestDeposit), [_uid, _amount]] call CFUNC(serverEvent);
    };
    case "bank::withdraw": {
        private _amount = _data get "amount";
        if (_amount > _bank) exitWith { hint "Insufficient funds!"; };

        [SRPC(bank,requestWithdraw), [_uid, _amount]] call CFUNC(serverEvent);
    };
    case "bank::transfer": {
        private _amount = _data get "amount";
        private _from = _data get "from";
        private _target = _data get "target";

        if (_target isEqualTo _uid) exitWith {
            hint "Cannot transfer to yourself!";
            diag_log "[FORGE:Client:Bank] Attempted self-transfer blocked";
        };

        private _fromAmount = _account get _from;
        if (_amount > _fromAmount) exitWith { hint "Insufficient funds!"; };

        [SRPC(bank,requestTransfer), [_uid, _target, _from, _amount]] call CFUNC(serverEvent);
    };
    case "bank::close": { closeDialog 1; };

    // ========================================================================
    // ATM OPERATIONS
    // ========================================================================
    case "atm::withdraw": {
        private _amount = _data get "amount";
        if (_amount > _bank) exitWith { hint "Insufficient funds!"; };

        [SRPC(bank,requestWithdraw), [_uid, _amount]] call CFUNC(serverEvent);
    };
    case "atm::deposit": {
        private _amount = _data get "amount";
        if (_amount > _cash) exitWith { hint "Insufficient cash!"; };

        [SRPC(bank,requestDeposit), [_uid, _amount]] call CFUNC(serverEvent);
    };
    case "atm::close": { closeDialog 1; };
    default { diag_log format ["[FORGE:Client:Bank] Unhandled UI event: %1", _event]; };
};

true;
