#include "..\script_component.hpp"

/*
 * Author: IDSolutions
 * Handles the UI events.
 *
 * Arguments:
 * None
 *
 * Return Value:
 * None
 *
 * Example:
 * call forge_client_org_fnc_handleUIEvents;
 *
 * Public: No
 */

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

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

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

switch (_event) do {
    case "org::close": { closeDialog 1; };
    case "org::ready": {
        private _orgData = GVAR(OrgClass) get "org";
        private _name = _orgData getOrDefault ["name", "Unknown"];
        private _id = _orgData getOrDefault ["id", ""];
        private _funds = _orgData getOrDefault ["funds", 0];
        private _reputation = _orgData getOrDefault ["reputation", 0];
        private _membersRaw = _orgData getOrDefault ["members", []];
        private _membersList = [];

        // Handle members
        private _fnc_processMember = {
            params ["_mData"];

            private _mName = _mData getOrDefault ["name", "Unknown"];
            createHashMapFromArray [
                ["name", _mName],
                ["rank", "Member"],
                ["online", true]
            ]
        };

        {
            _membersList pushBack ([_y] call _fnc_processMember);
        } forEach _membersRaw;

        private _totalMembers = count _membersList;

        // Handle assets
        private _assetsRaw = _orgData getOrDefault ["assets", createHashMap];
        private _assetsList = [];

        private _fnc_processAsset = {
            params ["_aData"];

            private _aName = _aData getOrDefault ["name", "Unknown Asset"];
            private _aLocation = _aData getOrDefault ["location", "Unknown Location"];
            private _aIcon = _aData getOrDefault ["icon", "📦"];

            createHashMapFromArray [
                ["name", _aName],
                ["location", _aLocation],
                ["icon", _aIcon]
            ]
        };

        {
            _assetsList pushBack ([_y] call _fnc_processAsset);
        } forEach _assetsRaw;

        // Construct HashMap payload
        private _payload = createHashMapFromArray [
            ["org", createHashMapFromArray [
                ["name", _name],
                ["tag", _id],
                ["status", "Active"]
            ]],
            ["stats", createHashMapFromArray [
                ["totalMembers", _totalMembers],
                ["onlineMembers", _totalMembers],
                ["balance", _funds],
                ["reputation", _reputation]
            ]],
            ["membersOnline", _membersList],
            ["assets", _assetsList],
            ["activities", []],
            ["missions", []]
        ];

        private _json = toJSON _payload;

        _control ctrlWebBrowserAction ["ExecJS", format ["updateOrgDashboard(%1)", _json]];
    };
    default { hint format ["Unhandled UI event: %1", _event]; };
};

true;
