#include "..\script_component.hpp"

/*
 * Function: forge_client_locker_fnc_equipGear
 * Author: IDSolutions
 *
 * [Description]
 * Equips gear from the locker.
 *
 * Arguments:
 * N/A
 *
 * Return Value:
 * N/A
 *
 * Examples:
 * [] call forge_client_locker_fnc_equipGear;
 *
 * Public: Yes
 */

private _display = findDisplay IDD_LOCKERDIALOG;
private _playerItems = _display displayCtrl IDC_PLAYEREQUIPMENTLIST;
private _lockerItems = _display displayCtrl IDC_LOCKEREQUIPMENTLIST;
private _selectedItem = lbCurSel _lockerItems;
private _selectedItemData = _lockerItems lbData _selectedItem;

private _data = call compile _selectedItemData;
private _locker = GETVAR(player,FORGE_Locker,[]);

if ((isNil { _data })) exitWith { ctrlEnable [IDC_EQUIPBUTTON, true]; };

private _itemType = _data select 0;
private _item = _data select 1;
private _clear = true;

switch (_itemType) do {
    case "backpack": {
        if (isNull (unitBackpack player)) then {
            player addBackpack _item;
            playSound "FD_Finish_F";
        } else {
            _clear = false;
            ["You already have a backpack equiped!", "warning", 3, "right"] call EFUNC(misc,notify);
            playSound "FD_CP_Not_Clear_F";
        };
    };
    case "facewear": {
        if (goggles player == "") then {
            player addGoggles _item;
            playSound "FD_Finish_F";
        } else {
            _clear = false;
            ["You already have facewear equiped!", "warning", 3, "right"] call EFUNC(misc,notify);
            playSound "FD_CP_Not_Clear_F";
        };
    };
    case "headgear": {
        if (headgear player == "") then {
            player addHeadgear _item;
            playSound "FD_Finish_F";
        } else {
            _clear = false;
            ["You already have headgear equiped!", "warning", 3, "right"] call EFUNC(misc,notify);
            playSound "FD_CP_Not_Clear_F";
        };
    };
    case "hmd": {
        if (hmd player == "") then {
            player linkItem _item;
            playSound "FD_Finish_F";
        } else {
            _clear = false;
            ["You already have a HMD equiped!", "warning", 3, "right"] call EFUNC(misc,notify);
            playSound "FD_CP_Not_Clear_F";
        };
    };
    case "item": {
        if (player canAdd _item) then {
            player addItem _item;
            playSound "FD_Finish_F";
        } else {
            _clear = false;
            ["You don't have enough space left!", "warning", 3, "right"] call EFUNC(misc,notify);
            playSound "FD_CP_Not_Clear_F";
        };
    };
    case "magazine": {
        if (player canAdd (_item select 0)) then {
            player addMagazine [(_item select 0), (_item select 1)];
            playSound "FD_Finish_F";
        } else {
            _clear = false;
            ["You don't have enough space left!", "warning", 3, "right"] call EFUNC(misc,notify);
            playSound "FD_CP_Not_Clear_F";
        };
    };
    case "uniform": {
        if (uniform player == "") then {
            player forceAddUniform _item;
            playSound "FD_Finish_F";
        } else {
            _clear = false;
            ["You already have a uniform equiped!", "warning", 3, "right"] call EFUNC(misc,notify);
            playSound "FD_CP_Not_Clear_F";
        };
    };
    case "vest": {
        if (vest player == "") then {
            player addVest _item;
            playSound "FD_Finish_F";
        } else {
            _clear = false;
            ["You already have a vest equiped!", "warning", 3, "right"] call EFUNC(misc,notify);
            playSound "FD_CP_Not_Clear_F";
        };
    };
    case "weapon": {
        private _weaponType = getNumber (configFile >> "CfgWeapons" >> _item >> "type");
        private _hasSpace = switch (_weaponType) do {
            case 1: {primaryWeapon player == ""};    // Primary
            case 2: {handgunWeapon player == ""};    // Handgun
            case 4: {secondaryWeapon player == ""};  // Launcher
            case 131072: {false};                    // Binoculars
            default {true};
        };

        if (_hasSpace) then {
            private _compatibleMags = getArray (configFile >> "CfgWeapons" >> _item >> "magazines");
            private _baseType = (_compatibleMags select 0) select [0, (_compatibleMags select 0) find "_Tracer"];
            if (_baseType == "") then {_baseType = _compatibleMags select 0};

            private _magIndices = [];
            {
                if ((_x select 0) == "magazine") then {
                    private _magClass = (_x select 1) select 0;
                    if ((_magClass in _compatibleMags || {("ACE_" in _magClass) && {_baseType in _magClass}}) && 
                        {player canAdd _magClass}) then {
                        player addMagazine [_magClass, (_x select 1) select 1];
                        _magIndices pushBack _forEachIndex;
                    };
                };
            } forEach _locker;

            reverse _magIndices;
            {_locker deleteAt _x} forEach _magIndices;

            player addWeapon _item;
            playSound "FD_Finish_F";
        } else {
            _clear = false;
            ["You don't have enough space left!", "warning", 3, "right"] call EFUNC(misc,notify);
            playSound "FD_CP_Not_Clear_F";
        };
    };
};

if (_clear) then {
    lbClear _lockerItems;
    lbClear _playerItems;
    _lockerItems lbSetCurSel -1;
    _playerItems lbSetCurSel -1;

    private _index = _locker findIf { _x isEqualTo _data };

    _locker deleteAt _index;
    SETPVAR(player,FORGE_Locker,_locker);

    [] call FUNC(fetchLocker);
    [] call FUNC(fetchPlayer);

    ctrlEnable [IDC_EQUIPBUTTON, true];
};