-- Original name: zNS_Recipes.lua
-- Original author: Nihilist Penguin (https://steamcommunity.com/id/bickback/)

-- Copyright 2023 Nihilist Penguin. This item is not authorized for posting on Steam,
-- except under the Steam account named Nihilist Penguin, or with the copyright holder's explicit permission.
-- Redistribution of this mod without explicit permission from the original creator is prohibited under any circumstances.
-- This includes, but not limited to, uploading these mods to the Steam Workshop or any other site,
-- distribution as part of another mod or modpack, and distribution of modified versions.

if not NSGlobal then
	print("NSGlobal not found, skipping zNS_Recipes.lua")
	return
end

local print_debug = NSGlobal.utils.register.print_debug

function Recipe.GetItemTypes.Silencer(scriptItems)
	scriptItems:addAll(getScriptManager():getItemsTag("Silencer"))
end

local fn = function()
	local get_ammo_type_craftable = NSGlobal.utils.register.get_ammo_type_craftable
	local get_silencer_item_name = NSGlobal.utils.register.get_ammo_silencer_item_name
	local table_contains = NSGlobal.utils.system.table_contains
	local ammo_types_in_system = NSGlobal.utils.system.get_ammo_types()
	local silencer_items_not_in_system = NSGlobal.utils.system.get_silencer_items_not_in_system()

	local crafting_mw_required = SandboxVars.NS.CraftingMetalWeldingRequired
	local crafting_aiming_required = SandboxVars.NS.CraftingAimingRequired
	local conversion_mw_required = SandboxVars.NS.ConversionMetalWeldingRequired
	local conversion_aiming_required = SandboxVars.NS.ConversionAimingRequired
	local conversion_allowed = SandboxVars.NS.ConversionAllowed

	local hidden_silencer_item_names = {}
	local silencer_item_names = {}

	-- hidden silencer items due to ammo type not in system
	for _, silencer_item_name in ipairs(silencer_items_not_in_system) do
		table.insert(hidden_silencer_item_names, silencer_item_name)
	end

	for _, ammo_type in ipairs(ammo_types_in_system) do
		local silencer_item_name = get_silencer_item_name(ammo_type)
		local craftable = get_ammo_type_craftable(ammo_type)

		if not craftable then
			if not table_contains(hidden_silencer_item_names, silencer_item_name) then
				table.insert(hidden_silencer_item_names, silencer_item_name)
			end
		else
			if not table_contains(silencer_item_names, silencer_item_name) then
				table.insert(silencer_item_names, silencer_item_name)
			end
		end
	end

	local recipes = getScriptManager():getAllRecipes()
	for i = 0, recipes:size() - 1 do
		local recipe = recipes:get(i)
		local recipe_name = recipe:getOriginalname()
		local result_item_name = recipe:getResult():getFullType()
		if result_item_name then
			for _, silencer_item_name in ipairs(hidden_silencer_item_names) do
				if result_item_name == silencer_item_name then
					recipe:setIsHidden(true)
					print_debug("[Recipe] hiding recipe: " .. recipe_name)
				end
			end

			for _, silencer_item_name in ipairs(silencer_item_names) do
				if result_item_name == silencer_item_name then
					if string.find(recipe_name, "ConvertSilencer") then
						if not conversion_allowed then
							recipe:setIsHidden(true)
							print_debug("[Recipe] hiding recipe: " .. recipe_name)
						else
							recipe:clearRequiredSkills()
							if conversion_mw_required ~= 0 then
								recipe:addRequiredSkill(Perks.MetalWelding, conversion_mw_required)
							end
							if conversion_aiming_required ~= 0 then
								recipe:addRequiredSkill(Perks.Aiming, conversion_aiming_required)
							end
							-- stylua: ignore
							print_debug("[Recipe] modifying conversion recipe: " .. recipe_name .. " metalworking: " .. tostring(conversion_mw_required) .. " aiming: " .. tostring(conversion_aiming_required))
						end
					elseif string.find(recipe_name, "CraftSilencer") then
						recipe:clearRequiredSkills()
						if crafting_mw_required ~= 0 then
							recipe:addRequiredSkill(Perks.MetalWelding, crafting_mw_required)
						end
						if crafting_aiming_required ~= 0 then
							recipe:addRequiredSkill(Perks.Aiming, crafting_aiming_required)
						end
						-- stylua: ignore
						print_debug("[Recipe] modifying crafting recipe: " .. recipe_name .. " metalworking: " .. tostring(crafting_mw_required) .. " aiming: " .. tostring(crafting_aiming_required))
					end
				end
			end
		end
	end
end

Events.OnGameStart.Add(fn) -- sp
Events.OnServerStarted.Add(fn) -- mp
