#include "..\script_component.hpp"

params ["_control", "_selectedIndex"];

private _display = ctrlParent _control;
private _productList = _display displayCtrl IDC_PRODUCTLIST;
lbClear _productList;

private _selectedCategory = _control lbData _selectedIndex;
private _data = GVAR(currentStore);
private _items = _data select 1;

[format ["You have selected %1 as your filter.", _selectedCategory], "info", 6, "right"] call EFUNC(misc,notify);

{
    _x params ["_item", "_price", "_category", "_configType", "_itemType"];
    private _index = -1;
    
    if (_category == _selectedCategory) then {
        switch (_configType) do {
            case "item": {
                private _displayName = getText (configFile >> "CfgWeapons" >> _item >> "displayName");
                private _picture = getText (configFile >> "CfgWeapons" >> _item >> "picture");

                _index = _productList lbAdd _displayName;
                _productList lbSetData [_index, str [_item, _price, _category, _configType, _itemType]];
                _productList lbSetPicture [_index, _picture];
            };
            case "weapon": {
                private _displayName = getText (configFile >> "CfgWeapons" >> _item >> "displayName");
                private _picture = getText (configFile >> "CfgWeapons" >> _item >> "picture");

                _index = _productList lbAdd _displayName;
                _productList lbSetData [_index, str [_item, _price, _category, _configType, _itemType]];
                _productList lbSetPicture [_index, _picture];
            };
            case "magazine": {
                private _displayName = getText (configFile >> "CfgMagazines" >> _item >> "displayName");
                private _picture = getText (configFile >> "CfgMagazines" >> _item >> "picture");

                _index = _productList lbAdd _displayName;
                _productList lbSetData [_index, str [_item, _price, _category, _configType, _itemType]];
                _productList lbSetPicture [_index, _picture];
            };
            case "backpack": {
                private _displayName = getText (configFile >> "CfgVehicles" >> _item >> "displayName");
                private _picture = getText (configFile >> "CfgVehicles" >> _item >> "picture");

                _index = _productList lbAdd _displayName;
                _productList lbSetData [_index, str [_item, _price, _category, _configType, _itemType]];
                _productList lbSetPicture [_index, _picture];
            };
            case "vehicle": {
                private _displayName = getText (configFile >> "CfgVehicles" >> _item >> "displayName");
                private _picture = getText (configFile >> "CfgVehicles" >> _item >> "picture");

                _index = _productList lbAdd _displayName;
                _productList lbSetData [_index, str [_item, _price, _category, _configType, _itemType]];
                _productList lbSetPicture [_index, _picture];
            };
            default {
                private _displayName = getText (configFile >> "CfgWeapons" >> _item >> "displayName");
                private _picture = getText (configFile >> "CfgWeapons" >> _item >> "picture");

                _index = _productList lbAdd _displayName;
                _productList lbSetData [_index, str [_item, _price, _category, _configType, _itemType]];
                _productList lbSetPicture [_index, _picture];
            };
        };
    };
} forEach _items;