require "ISUI/ISInventoryPaneContextMenu"
require "SimpleSilencersModelTable"
require "SimpleSilencersCraftedSilencerBlacklist"

local function predicateNotBroken(item)
	return not item:isBroken()
end

SimpleSilencersSilencerContext = {}

SimpleSilencersSilencerContext.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(SimpleSilencersModelTable) do
			if weaponname == item:getFullType() then
				siltypeAssigned = true
				break
			end	
		end

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

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

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

function SimpleSilencersSilencerContext: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

			for i=0, weaponParts:size() - 1 do
				local part = weaponParts:get(i);
				if (part:getType() == "Silencer") and not item:getWeaponPart("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
				end
			end
		end
	end
end

function SimpleSilencersSilencerContext: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
			local metalSilencerFlag = false			
			for i=0, weaponParts:size() - 1 do
				local part = weaponParts:get(i);
				if (part:getType() == "MetalPipeSilencer") and not item:getWeaponPart("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

Events.OnPreFillInventoryObjectContextMenu.Add(SimpleSilencersSilencerContext.inventoryMenu)