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 loaded = false;

    mp.events.add('inventoryShow', () => {
        if (invBrowser !== null) {
            invBrowser.destroy()
            invBrowser = null;
            globalData.InInput = false;
            loaded = false;
            mp.gui.cursor.show(false, false);
            return;
        }
        if (!globalData.InInput) {
            globalData.InInput = true;
            mp.gui.cursor.show(true, true);
            itemArr = [];
            vehItemArr = [];
            invBrowser = mp.browsers.new('package://assets/html/inventory/inventory.html');
        }
    });
    var offer = 0;

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

    mp.events.add('openInventory', () => {
            globalData.InInput = true;
            mp.gui.cursor.show(true, true);
            itemArr = [];
            vehItemArr = [];
            invBrowser = mp.browsers.new('package://assets/html/inventory/inventory.html');        
    });
    mp.events.add('closeInventory', () => {
            invBrowser.destroy()
            invBrowser = null;
            globalData.InInput = false;
            loaded = false;
            mp.gui.cursor.show(false, false);
        return;
    });

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

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

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

    mp.events.add("CEF:InventoryLoaded", () => {
        invBrowser.execute(`setBackpackItems('${JSON.stringify(itemArr)}')`);
        if (vehItemArr.length != 0) {
            invBrowser.execute(`setVehicleItems('${JSON.stringify(vehItemArr)}')`);
        }
       
        //invBrowser.execute(`setTradeItems('${JSON.stringify(tradeItemArr)}')`);
        //invBrowser.execute(`setOfferItems('${JSON.stringify(offerItemArr)}')`);
        loaded = true;
    });

    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("closeInventory");
        }
    });

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

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