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

local print_debug = NSGlobal.utils.register.print_debug

local bullet_multipliers = NSGlobal.utils.system.get_ammo_types_multipliers()
local get_sound_details = NSGlobal.utils.system.get_sound_details

local update_weapon_details = function(_, inventoryItem)
	if inventoryItem == nil or (inventoryItem:getType() ~= Type.Weapon and not inventoryItem:getAmmoType()) then
		return
	end

	local script_item = inventoryItem:getScriptItem()
	local full_name = script_item:getFullName()
	local curr_sound_radius = script_item:getSoundRadius()
	local curr_sound_volume = script_item:getSoundVolume()
	local curr_swing_sound = script_item:getSwingSound()

	local tmp_detail

	-- one time checking and loading due to server lua files loading after this event function
	if not NSGlobal.system.increased_sounds then
		tmp_detail = get_sound_details(script_item:getAmmoType())
		curr_sound_radius = curr_sound_radius + tmp_detail.sound_radius
		curr_sound_volume = curr_sound_volume + tmp_detail.sound_volume
	end

	-- stylua: ignore
	-- print_debug("[C] name: " .. full_name .. " v: " .. curr_sound_volume .. " r: " .. curr_sound_radius .. " s: " .. tostring(curr_swing_sound))

	if inventoryItem:getCanon() then
		local canon_name = inventoryItem:getCanon():getFullType()

		if string.find(canon_name, "Silencer") then
			local multiplier = bullet_multipliers[inventoryItem:getAmmoType()]
			local new_sound_volume = curr_sound_volume * multiplier
			local new_sound_radius = curr_sound_radius * multiplier

			local detail = NSGlobal.system.rweapon_details[full_name]
			local rtype = detail.rtype
			local new_silenced_swing_sound = detail.silenced_swing_sound

			if multiplier == 0 or rtype == nil then
				return
			end

			inventoryItem:setSoundRadius(new_sound_radius)
			inventoryItem:setSoundVolume(new_sound_volume)
			inventoryItem:setSwingSound(new_silenced_swing_sound)

			-- stylua: ignore
			print_debug("[S] name: " .. full_name .. " v: " .. new_sound_volume .. " r: " .. new_sound_radius .. " m: " .. multiplier .. " s: " .. tostring(new_silenced_swing_sound))
		end
	else
		local detail = NSGlobal.system.rweapon_details[full_name]

		if detail then
			curr_sound_radius = detail.sound_radius
			curr_sound_volume = detail.sound_volume
			if SandboxVars.NS.OverrideBaseSounds then
				curr_swing_sound = NSGlobal.system.rweapon_details[full_name].override_swing_sound or curr_swing_sound
			else
				curr_swing_sound = detail.swing_sound
			end
		end

		-- one time checking and loading due to server lua files loading after this event function
		if not NSGlobal.system.increased_sounds then
			curr_sound_radius = curr_sound_radius + tmp_detail.sound_radius
			curr_sound_volume = curr_sound_volume + tmp_detail.sound_volume
		end

		inventoryItem:setSoundRadius(curr_sound_radius)
		inventoryItem:setSoundVolume(curr_sound_volume)
		inventoryItem:setSwingSound(curr_swing_sound)
		-- stylua: ignore
		print_debug("[R] name: " .. full_name .. " v: " .. curr_sound_volume .. " r: " .. curr_sound_radius .. " s: " .. tostring(curr_swing_sound))
	end
end

-- general use-case update
Events.OnEquipPrimary.Add(update_weapon_details)

-- essential when logging in with a silencer attached to a Weapon
Events.OnGameStart.Add(function()
	local player = getPlayer()
	update_weapon_details(player, player:getPrimaryHandItem())
end)
