#include "..\script_component.hpp"

/*
 * Function: forge_client_org_fnc_create
 * Author: J. Schmidt
 * 
 * Description:
 * Creates a new organization for a player
 *
 * Arguments:
 * 0: _ownerUID - Player UID <STRING>
 * 1: _ownerName - Player name <STRING>
 * 2: _name - Organization name <STRING>
 *
 * Return Value:
 * Success <BOOL>
 */

params [["_ownerUID", "", [""]], ["_ownerName", "", [""]], ["_name", "", [""]]];

if (_ownerUID == "" || _ownerName == "" || _name == "") exitWith { TRACE_3("Invalid parameters for organization creation",_ownerUID,_ownerName,_name); false };

private _store = call FUNC(verifyOrgStore);
private _key = format ["%1_%2", _ownerUID, _name];
private _existingOrg = _store call ["getByKey", [_key]];
private _playerAlreadyInOrg = false;

{
    private _org = _x;
    private _members = _org get "members";
    if (!isNil { _members get _ownerUID }) exitWith {
        _playerAlreadyInOrg = true;
    };
} forEach (_store call ["get", []]);

if (_playerAlreadyInOrg) exitWith { TRACE_1("Player already in an organization",_ownerUID); false };

_store call ["post", [_ownerUID, _name, 0, 0]];
[_name, _ownerUID, _ownerName, "owner"] call FUNC(join);

TRACE_2("Organization created successfully",_name,_ownerUID);

true