require "ISUI/ISInventoryPaneContextMenu"
require "ISILModelTable"
require "ISILCraftedSilencerBlacklist"

-- I need a copy of this I guess
local function predicateNotBroken(item)
	return not item:isBroken()
end

ISILSilencerContext = {}

ISILSilencerContext.inventoryMenu = function(playerid, context, items)
    local player = getSpecificPlayer(playerid)
    for _, v in ipairs(items) do
		local item = v
		if not instanceof(v, "InventoryItem") then
			item = v.items[1]
		end

		local siltypeAssigned = false
		for weaponname, siltype in pairs(ISILModelTable) do
			if weaponname == item:getFullType() then
				siltypeAssigned = true
				break
			end	
		end

		if siltypeAssigned then
			ISILSilencerContext:UpgradeSilencer(item, index, player, context)
		end

		local blacklisted = false
        -- Crafted Silencer
        for index, preset in ipairs(ISILCraftedSilencerBlacklist) do
            if preset == item:getFullType() then
                blacklisted = true
                break
            end	
        end

		local siltypeAssigned = false
        if not blacklisted then
			for weaponname, siltype in pairs(ISILModelTable) do
				if weaponname == item:getFullType() then
					siltypeAssigned = true
					break
				end	
			end
			if siltypeAssigned then
            	ISILSilencerContext:UpgradeCraftedSilencer(item, index, player, context)
			end
        end
    end
end

function ISILSilencerContext:UpgradeSilencer(item, index, player, context)
	local hasScrewdriver = player:getInventory():containsTagEvalRecurse("Screwdriver", predicateNotBroken)
	if item and instanceof(item, "HandWeapon") and item:isRanged() and hasScrewdriver then
		-- add parts
		local weaponParts = player:getInventory():getItemsFromCategory("WeaponPart");
		if weaponParts and not weaponParts:isEmpty() then
			local silencerFlag = false
			local metalSilencerFlag = false
			for i=0, weaponParts:size() - 1 do
				local part = weaponParts:get(i);
				local canon = item:getWeaponPart("Canon")
				if (part:getFullType() == "Base.Silencer") and not canon and not silencerFlag then
					-- To do: Localization
					local listEntry = context:addOption(getText("IGUI_ContextMenu_AddSilencer"), item, ISInventoryPaneContextMenu.onUpgradeWeapon, part, player);
					local tooltip = ISInventoryPaneContextMenu.addToolTip();
					tooltip.description = getText("IGUI_ContextMenu_AddSilencerDescription")
					tooltip:setName(part:getDisplayName())
					tooltip.texture = part:getTex()
					listEntry.toolTip = tooltip;
					silencerFlag = true
				elseif (part:getFullType() == "Base.MetalPipeSilencer") and not canon and not metalSilencerFlag then
					-- To do: Localization
					local listEntry = context:addOption(getText("IGUI_ContextMenu_AddMetalPipeSilencer"), item, ISInventoryPaneContextMenu.onUpgradeWeapon, part, player);
					local tooltip = ISInventoryPaneContextMenu.addToolTip();
					tooltip.description = getText("IGUI_ContextMenu_AddMetalPipeSilencerDescription")
					tooltip:setName(part:getDisplayName())
					tooltip.texture = part:getTex()
					listEntry.toolTip = tooltip;
					metalSilencerFlag = true
				end
			end
		end
	end
end

function ISILSilencerContext:UpgradeCraftedSilencer(item, index, player, context)
	local hasScrewdriver = player:getInventory():containsTagEvalRecurse("Screwdriver", predicateNotBroken)
	if item and instanceof(item, "HandWeapon") and item:isRanged() and hasScrewdriver then
		-- add parts
		local weaponParts = player:getInventory():getItemsFromCategory("WeaponPart");
		if weaponParts and not weaponParts:isEmpty() then
			local torchSilencerFlag = false
			local bottleSilencerFlag = false
			for i=0, weaponParts:size() - 1 do
				local part = weaponParts:get(i);
				local canon = item:getWeaponPart("Canon")
				if (part:getFullType() == "Base.TorchSilencer") and not canon and not torchSilencerFlag then
					-- To do: Localization
					local listEntry = context:addOption(getText("IGUI_ContextMenu_AddTorchSilencer"), item, ISInventoryPaneContextMenu.onUpgradeWeapon, part, player);
					local tooltip = ISInventoryPaneContextMenu.addToolTip();
					tooltip.description = getText("IGUI_ContextMenu_AddTorchSilencerDescription")
					tooltip:setName(part:getDisplayName())
					tooltip.texture = part:getTex()
					listEntry.toolTip = tooltip;
					torchSilencerFlag = true
				elseif (part:getFullType() == "Base.WaterBottleSilencer") and not canon and not bottleSilencerFlag then
					-- To do: Localization
					local listEntry = context:addOption(getText("IGUI_ContextMenu_AddWaterBottleSilencer"), item, ISInventoryPaneContextMenu.onUpgradeWeapon, part, player);
					local tooltip = ISInventoryPaneContextMenu.addToolTip();
					tooltip.description = getText("IGUI_ContextMenu_AddWaterBottleSilencerDescription")
					tooltip:setName(part:getDisplayName())
					tooltip.texture = part:getTex()
					listEntry.toolTip = tooltip;
					bottleSilencerFlag = true
				end
			end
		end
	end
end

Events.OnPreFillInventoryObjectContextMenu.Add(ISILSilencerContext.inventoryMenu)