export default function inventory(globalData: IGlobalData): void {
    var invBrowser: BrowserMp = null;
    var itemIdArr;
    var itemAmountArr;
    var invWeight;
    var itemNameArr;
    var itemArr;
    var offerItemArr;
    var tradeItemArr;
    var vehItemArr;
    var trunkSize;

    var loaded = false;

    mp.events.add('loadInventoryCEF', () => {
        itemArr = [];
        vehItemArr = [];
        if (invBrowser !== null) {
            invBrowser.destroy
        }
        invBrowser = mp.browsers.new('package://assets/html/inventory/inventory.html');
    });

    mp.events.add('inventoryShow', () => {
        if (!globalData.InMenu && !loaded) {
            globalData.InMenu = true;
            mp.gui.cursor.show(true, true);
            loaded = true;

            invBrowser.execute(`showInventory()`);
        } else if (loaded) {
            globalData.InMenu = false;
            mp.gui.cursor.show(false, false);
            loaded = false;
            invBrowser.execute(`closeInventory()`);
        }
    });

    mp.events.addDataHandler("backpackItems", (entity: EntityMp, jsonItemArr) => {
        if (entity != mp.players.local) return;
        itemArr = JSON.parse(jsonItemArr);
        if (loaded)
            invBrowser.execute(`setBackpackItems('${JSON.stringify(itemArr)}',true)`);
    });

    mp.events.addDataHandler("setVehicleTrunk", (entity: EntityMp, size) => {
        if (entity != mp.players.local) return;
        trunkSize = size;
    });

    mp.events.addDataHandler("vehicleItems", (entity: EntityMp, jsonItemArr) => {
        if (entity != mp.players.local) return;
        vehItemArr = JSON.parse(jsonItemArr);

        if (loaded) {
            invBrowser.execute(`setVehicleItems('${JSON.stringify(vehItemArr)}', '${trunkSize}')`);
        }
    });

    mp.events.add("CEF:callVehicleInventory", () => {
        mp.events.callRemote("CLIENT:getVehicleInventory")
    });

    mp.events.add("CEF:backpackToVehicle", (jsonItemId, jsonAmount) => {
        mp.events.callRemote('transferToVehicle', jsonItemId, jsonAmount);
    });

    mp.events.add("CEF:vehicleToBackpack", (jsonItemId, jsonAmount) => {
        mp.events.callRemote('transferToBackpack', jsonItemId, jsonAmount);
    });

    mp.events.add("CEF:useItem", (type, amount, id) => {
        mp.events.callRemote("itemInteract", type, amount, id);
    });

    mp.events.add("removeItem", (id, amount) => {
        if (invBrowser !== null) {
            invBrowser.execute(`removeItems('${JSON.stringify(id)}','${JSON.stringify(amount)}')`);
        }
    });

    mp.events.add("aproveUse", (amount, name) => {
        if (invBrowser !== null) {
            mp.game.audio.playSoundFrontend(1, "LOCAL_PLYR_CASH_COUNTER_COMPLETE", "DLC_HEISTS_GENERAL_FRONTEND_SOUNDS", true);
            //invBrowser.execute(`alertGreen('${JSON.stringify(amount)}','${JSON.stringify(name)}')`);
            //mp.events.call("inventoryShow");
        }
    });

    mp.events.add("aproveThrow", (amount, name) => {
        if (invBrowser !== null) {
            mp.game.audio.playSoundFrontend(1, "ERROR", "HUD_AMMO_SHOP_SOUNDSET", true);
            invBrowser.execute(`alertRed('${JSON.stringify(amount)}','${JSON.stringify(name)}')`);
        }
    });

    mp.events.add("aproveTransfer", (amount, name, where) => {
        if (invBrowser !== null) {
            mp.game.audio.playSoundFrontend(1, "ERROR", "HUD_AMMO_SHOP_SOUNDSET", true);
            invBrowser.execute(`alertBlue('${JSON.stringify(amount)}','${JSON.stringify(name)}','${JSON.stringify(where)}')`);
        }
    });

    mp.events.add("Error", (msg) => {
        if (invBrowser !== null) {
            mp.game.audio.playSoundFrontend(1, "ERROR", "HUD_AMMO_SHOP_SOUNDSET", true);
            invBrowser.execute(`alertError('${JSON.stringify(msg)}')`);
        }
    });

    mp.events.add("CEF:acceptTrade", () => {
        if (invBrowser !== null) {
            mp.events.callRemote('acceptTrade');
            invBrowser.destroy()
            invBrowser = null;
            mp.gui.cursor.show(false, false);
        }
    });

    mp.events.add("CEF:declineTrade", () => {
        if (invBrowser !== null) {
            mp.events.callRemote('declineTrade');
            invBrowser.destroy()
            invBrowser = null;
            mp.gui.cursor.show(false, false);
        }
    });

    mp.events.add("CEF:saveItems", (saveItemsArr, saveAmountArr, saveItemsSlotArr) => {
        mp.events.callRemote('saveItems', saveItemsArr, saveAmountArr, saveItemsSlotArr);
    });
    mp.events.add("CEF:saveItemsHelper", (vehI5) => {
        mp.events.callRemote('saveHelper', vehI5);
    });

    mp.events.add("CEF:UseItemInv", (itemId) => {
        mp.events.callRemote('invUseItem', itemId);
    });
}