-- Original name: zNS_zzAddModels.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_zzAddModels.lua")
	return
end

local print_debug = NSGlobal.utils.register.print_debug

local _rweapon_details = NSGlobal.system.rweapon_details
local _table_contains = NSGlobal.utils.system.table_contains

local _get_ammo_silencer_item_name = NSGlobal.utils.register.get_ammo_silencer_item_name
local _silencer_items_not_in_system = NSGlobal.utils.system.get_silencer_items_not_in_system()

local silencers_with_weapon_list = {}

local _get_modelweaponpart = function(full_name, silencer_item_name)
	local var
	if getActivatedMods():contains("VFExpansion1") then
		var = NSGlobal.register.vfe_model_exceptions[full_name]
	else
		var = NSGlobal.register.model_exceptions[full_name]
	end
	if var ~= nil then
		return "ModelWeaponPart = " .. silencer_item_name .. " " .. var .. " muzzle muzzle"
	end
	return nil
end

-- find all valid weapons and store corresponding silencer model
for full_name, detail in pairs(_rweapon_details) do
	local script_item = ScriptManager.instance:getItem(full_name)

	if script_item then
		local rtype = detail.rtype
		local silencer_item_name = _get_ammo_silencer_item_name(detail.ammo_type)

		if silencer_item_name and rtype ~= nil then
			if silencers_with_weapon_list[silencer_item_name] == nil then
				silencers_with_weapon_list[silencer_item_name] = {}
			end

			local added = true
			local param_text = _get_modelweaponpart(full_name, silencer_item_name)

			if param_text ~= nil then
				print("param_text " .. param_text)
				script_item:DoParam(param_text)
			else -- default values
				-- Pistol, Revolver, Sub-machine Gun, Others
				if rtype == "0" or rtype == "1" or rtype == "2" then
					script_item:DoParam("ModelWeaponPart = " .. silencer_item_name .. " Base.SilencerS muzzle muzzle")
				-- Assault Rifle, Semi-auto Rifle, Machine Gun, Lever/Bolt-action Rifle and Machine Gun
				elseif rtype == "3" or rtype == "4" or rtype == "5" or rtype == "7" then
					script_item:DoParam("ModelWeaponPart = " .. silencer_item_name .. " Base.SilencerM muzzle muzzle")
				elseif rtype == "6" then -- Shotgun
					if
						not string.find(string.lower(full_name), "sawn")
						and not string.find(string.lower(full_name), "doublebarrel")
					then
						script_item:DoParam(
							"ModelWeaponPart = " .. silencer_item_name .. " Base.SilencerL muzzle muzzle"
						)
					else
						added = false
					end
				end
			end

			if added == true then
				table.insert(silencers_with_weapon_list[silencer_item_name], full_name)
			end
		end
	end
end

-- add all valid weapons to corresponding silencer models
for silencer_item_name, weapons in pairs(silencers_with_weapon_list) do
	local script_item = ScriptManager.instance:getItem(silencer_item_name)
	print_debug("[Model] silencer_item: " .. silencer_item_name .. " | weapons: " .. #weapons)
	if script_item then
		local mount_on_var = ""
		for _, full_name in ipairs(weapons) do
			mount_on_var = mount_on_var .. full_name .. "; "
		end

		script_item:DoParam("MountOn = " .. mount_on_var .. ",")
	end
end

for _, silencer_item_name in ipairs(_silencer_items_not_in_system) do
	local script_item = ScriptManager.instance:getItem(silencer_item_name)
	if script_item then
		script_item:DoParam("MountOn = " .. ",")
	end
end
