#include "..\script_component.hpp"

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

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

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

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

switch (_itemType) do {
    case "backpack": {
        {
            for "_i" from 1 to (_x select 1) do {
                _locker pushBack ["item", (_x select 0)];
            };
        } count ((getItemCargo backpackContainer player) call EFUNC(misc,cargoToPairs));

        {
            for "_i" from 1 to (_x select 1) do {
                _locker pushBack ["weapon", (_x select 0)];
            };
        } count ((getWeaponCargo backpackContainer player) call EFUNC(misc,cargoToPairs));

        {
            _locker pushBack ["magazine", [(_x select 0), (_x select 1), getNumber (configFile >> "CfgMagazines" >> (_x select 0) >> "count")]];
        } count (magazinesAmmoCargo backpackContainer player);

        _locker pushBack [_itemType, _item];
        // player setVariable ["FORGE_Locker", _locker, true];
        SETPVAR(player,FORGE_Locker,_locker);

        removeBackpack player;
        playSound "FD_Finish_F";
    };
    case "facewear": {
        _locker pushBack [_itemType, _item];
        // player setVariable ["FORGE_Locker", _locker, true];
        SETPVAR(player,FORGE_Locker,_locker);

        removeGoggles player;
        playSound "FD_Finish_F";
    };
    case "headgear": {
        _locker pushBack [_itemType, _item];
        // player setVariable ["FORGE_Locker", _locker, true];
        SETPVAR(player,FORGE_Locker,_locker);

        removeHeadgear player;
        playSound "FD_Finish_F";
    };
    case "hmd": {
        _locker pushBack [_itemType, _item];
        // player setVariable ["FORGE_Locker", _locker, true];
        SETPVAR(player,FORGE_Locker,_locker);

        player unassignItem _item;
        player removeItem _item;
        playSound "FD_Finish_F";
    };
    case "item": {
        _locker pushBack [_itemType, _item];
        // player setVariable ["FORGE_Locker", _locker, true];
        SETPVAR(player,FORGE_Locker,_locker);

        if (_item == (currentMuzzle player)) then {
            player action ["SWITCHWEAPON", player, player, -1];
            player unassignItem _item;
            player removeWeapon _item;
        } else {
            player unassignItem _item;
            player removeItem _item;
        };
        playSound "FD_Finish_F";
    };
    case "magazine": {
        _locker pushBack [_itemType, _item];
        // player setVariable ["FORGE_Locker", _locker, true];
        SETPVAR(player,FORGE_Locker,_locker);

        private _temp = magazinesAmmoFull player;

        switch (_item select 3) do {
            case "Backpack": {
                {
                    if ((_x select 4) == "Backpack" && (_x select 0) == (_item select 0)) then {
                        player removeItemFromBackpack (_item select 0);
                    };
                } count _temp;

                _temp deleteAt (_temp findIf {
                    (_x select 0) == (_item select 0) && (_x select 1) == (_item select 1) && (_x select 4) == (_item select 3)
                });

                {
                    if ((_x select 4) == "Backpack" && (_x select 0) == (_item select 0)) then {
                        (backpackContainer player) addMagazineAmmoCargo [(_x select 0), 1, (_x select 1)];
                    };
                } count _temp;
            };
            case "Uniform": {
                {
                    if ((_x select 4) == "Uniform" && (_x select 0) == (_item select 0)) then {
                        player removeItemFromUniform (_item select 0);
                    };
                } count _temp;

                _temp deleteAt (_temp findIf {
                    (_x select 0) == (_item select 0) && (_x select 1) == (_item select 1) && (_x select 4) == (_item select 3)
                });

                {
                    if ((_x select 4) == "Uniform" && (_x select 0) == (_item select 0)) then {
                        (uniformContainer player) addMagazineAmmoCargo [(_x select 0), 1, (_x select 1)];
                    };
                } count _temp;
            };
            case "Vest": {
                {
                    if ((_x select 4) == "Vest" && (_x select 0) == (_item select 0)) then {
                        player removeItemFromVest (_item select 0);
                    };
                } count _temp;

                _temp deleteAt (_temp findIf {
                    (_x select 0) == (_item select 0) && (_x select 1) == (_item select 1) && (_x select 4) == (_item select 3)
                });

                {
                    if ((_x select 4) == "Vest" && (_x select 0) == (_item select 0)) then {
                        (vestContainer player) addMagazineAmmoCargo [(_x select 0), 1, (_x select 1)];
                    };
                } count _temp;
            };
        };
        playSound "FD_Finish_F";
    };
    case "uniform": {
        {
            for "_i" from 1 to (_x select 1) do {
                _locker pushBack ["item", (_x select 0)];
            };
        } count ((getItemCargo uniformContainer player) call EFUNC(misc,cargoToPairs));

        {
            for "_i" from 1 to (_x select 1) do {
                _locker pushBack ["weapon", (_x select 0)];
            };
        } count ((getWeaponCargo uniformContainer player) call EFUNC(misc,cargoToPairs));

        {
            _locker pushBack ["magazine", [(_x select 0), (_x select 1), getNumber (configFile >> "CfgMagazines" >> (_x select 0) >> "count")]];
        } count (magazinesAmmoCargo uniformContainer player);

        _locker pushBack [_itemType, _item];
        // player setVariable ["FORGE_Locker", _locker, true];
        SETPVAR(player,FORGE_Locker,_locker);

        removeUniform player;
        playSound "FD_Finish_F";
    };
    case "vest": {
        {
            for "_i" from 1 to (_x select 1) do {
                _locker pushBack ["item", (_x select 0)];
            };
        } count ((getItemCargo vestContainer player) call EFUNC(misc,cargoToPairs));

        {
            for "_i" from 1 to (_x select 1) do {
                _locker pushBack ["weapon", (_x select 0)];
            };
        } count ((getWeaponCargo vestContainer player) call EFUNC(misc,cargoToPairs));

        {
            _locker pushBack ["magazine", [(_x select 0), (_x select 1), getNumber (configFile >> "CfgMagazines" >> (_x select 0) >> "count")]];
            true;
        } count (magazinesAmmoCargo vestContainer player);

        _locker pushBack [_itemType, _item];
        // player setVariable ["FORGE_Locker", _locker, true];
        SETPVAR(player,FORGE_Locker,_locker);

        removeVest player;
        playSound "FD_Finish_F";
    };
    case "weapon": {
        _locker pushBack [_itemType, _item];
        // player setVariable ["FORGE_Locker", _locker, true];
        SETPVAR(player,FORGE_Locker,_locker);

        private _temp = weaponsItems player;

        {
            if ((_x select 0) == _item) then {
                private _att1 = _x select 1;
                private _att2 = _x select 2;
                private _att3 = _x select 3;
                private _mag = _x select 4;
                private _mag2 = _x select 5;
                private _att4 = _x select 6;

                {
                    if (_x isNotEqualTo "") then {
                        _locker pushBack ["item", _x];
                    };
                } forEach [_att1, _att2, _att3, _att4];

                {
                    if (_x isNotEqualTo []) then {
                        _locker pushBack ["magazine", [(_x select 0), (_x select 1), getNumber (configFile >> "CfgMagazines" >> (_x select 0) >> "count")]];
                    };
                } forEach [_mag, _mag2];
            };
        } count _temp;

        player removeWeapon _item;
        playSound "FD_Finish_F";
    };
};

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

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

    ctrlEnable [IDC_STOREBUTTON, true];
};