

export default function inventory(globalData: GlobalData): void {

    var invBrowser: BrowserMp = null;
    var itemIdArr;
    var itemAmountArr;
    var invWeight;
    var itemNameArr;

    var ivehArr;

    var Players;
    
    mp.events.add('setVehiclesInventory', (vehInvArr) => {
        ivehArr = vehInvArr;
        invBrowser.execute(`setVehiclesInv('${JSON.stringify(ivehArr)}');`);
    });

    mp.events.add('showVehInventory', () => {
        invBrowser.execute(`execVehInv();`);
    });
    var open = false;
    mp.events.add('inventoryShow', (iWeight, iNameArr, iAmountArr, iIdArr, playersArr) => {
        if (invBrowser !== null) {
            invBrowser.destroy()
            invBrowser = null;
            globalData.InInput = false;
            mp.gui.cursor.show(false, false);
            return;
        }
        if (!globalData.InInput) {
            globalData.InInput = true;
            mp.gui.cursor.show(true, true);
            invBrowser = mp.browsers.new('package://assets/html/inventory/inventory.html');
            Players = playersArr;
            itemIdArr = iIdArr;
            itemAmountArr = iAmountArr;
            itemNameArr = iNameArr;
            invWeight = iWeight;
        }     
    });
    var offer = 0;

    var tradeItemID;
    var tradeItemAmount;
    var tradeItemName;
    var tradeItemWeight;

    mp.events.addDataHandler("tradeOffer", (entity, value) => {
        if (entity.type === "player") {
            offer = parseInt(value);
            if (invBrowser != null)
                invBrowser.execute(`changeTradeStatus('${JSON.stringify(offer)}')`);
        }
    });

    mp.events.add("addTradeItems", (itemId, itemAmount, itemName, weight) => {
        tradeItemID = JSON.stringify(itemId);
        tradeItemAmount = JSON.stringify(itemAmount);
        tradeItemName = JSON.stringify(itemName);
        tradeItemWeight = JSON.stringify(weight);
        if (invBrowser != null)
            invBrowser.execute(`setAnfrage('${tradeItemID}','${tradeItemAmount}','${tradeItemName}','${tradeItemWeight}');`);
    });
  
    mp.events.add("CEF:saveVehicleTransfer", (val, jsonOrigin, jsonInvID, jsonInvAmount, jsonVehAmount) => {
        if (val == 0) {
            mp.events.callRemote('saveInventory', jsonInvID, jsonInvAmount, jsonOrigin, jsonVehAmount);
        } else if(val == 1){
            mp.events.callRemote('saveVehicleInventory', jsonInvID, jsonInvAmount, jsonOrigin, jsonVehAmount);
        }
            
    });

    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.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:InventoryLoaded", () => {
        if (offer == 1) {
            invBrowser.execute(`changeTradeStatus('${JSON.stringify(offer)}')`);
            invBrowser.execute(`setAnfrage('${tradeItemID}','${tradeItemAmount}','${tradeItemName}','${tradeItemWeight}');`);
        }
            
        invBrowser.execute(`setItems('${JSON.stringify(itemNameArr)}','${JSON.stringify(itemIdArr)}','${JSON.stringify(itemAmountArr)}','${JSON.stringify(Players)}');`);
        invBrowser.execute(`setWeight('${JSON.stringify(invWeight)}');`);
    });


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


    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);
    });

    mp.events.add("CEF:tradeItem", (money, User, tradeItems, tradeItemAmount) => {
        if (invBrowser !== null) {
            try {
                invBrowser.destroy()
                invBrowser = null;
                mp.events.callRemote('tradeItem', 0, User, tradeItems, tradeItemAmount);
            } finally {
                mp.gui.cursor.show(false, false);
            }
        }
    });

}