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

local print_debug = NSGlobal.utils.register.print_debug

NSGlobal.system = {}

NSGlobal.system.rweapon_details = {}
NSGlobal.system.increased_sounds = false

local _default_rweapon_type = NSGlobal.register.default.rweapon_type

local _get_rweapon_type = NSGlobal.utils.register.get_rweapon_type
local _get_default_silenced_sound = NSGlobal.utils.register.get_default_silenced_sound
local _get_register_sound = NSGlobal.utils.register.get_rweapon_sound
local _get_register_silenced_sound = NSGlobal.utils.register.get_rweapon_silenced_sound

local _is_weapon_name_valid = NSGlobal.utils.register.is_weapon_valid
local _is_ammo_type_valid = NSGlobal.utils.register.is_ammo_type_valid

local _print_detail = function(prefix, detail, full_name)
	-- stylua: ignore start
	if detail.rtype and detail.silenced_swing_sound then
		local rtype = NSGlobal.register.info.rweapon_type_descriptions[detail.rtype]
		print_debug("[" .. prefix .. "] name: " .. full_name .. " | ammo: " .. detail.ammo_type .. " | volume: " .. detail.sound_volume .. " | radius: " .. detail.sound_radius .. " | rtype: " .. rtype .. " | sound: " .. detail.swing_sound .. " | override_sound: " .. tostring(detail.override_swing_sound) .. " | silenced_sound: " .. detail.silenced_swing_sound)
	else
		print_debug("[" .. prefix .. "] name: " .. full_name .. " | ammo: " .. detail.ammo_type .. " | volume: " .. detail.sound_volume .. " | radius: " .. detail.sound_radius .. " | rtype: " .. "NONE" .. " | sound: " .. detail.swing_sound .. " | override_sound: " .. tostring(detail.override_swing_sound) .. " | silenced_sound: " .. "NONE")
	end

	-- stylua: ignore end
end

local add_to_values_dicts = function(full_name, detail)
	local ammo_type = detail.ammo_type
	local sound_volume = detail.sound_volume
	local sound_radius = detail.sound_radius
	local rtype = _get_rweapon_type(full_name, true) or _default_rweapon_type
	local swing_sound = detail.swing_sound
	local override_swing_sound = _get_register_sound(full_name)
	local silenced_swing_sound = _get_register_silenced_sound(full_name) or _get_default_silenced_sound(rtype)

	if string.find(string.lower(full_name), "bayonet") ~= nil then
		silenced_swing_sound = swing_sound
	end

	local updated_detail = {
		ammo_type = ammo_type,
		sound_volume = sound_volume,
		sound_radius = sound_radius,
		rtype = rtype,
		swing_sound = swing_sound,
		override_swing_sound = override_swing_sound,
		silenced_swing_sound = silenced_swing_sound,
	}

	NSGlobal.system.rweapon_details[full_name] = updated_detail

	_print_detail("System Add", updated_detail, full_name)
end

local details = NSGlobal.utils.register.get_all_rweapon_details()
for full_name, detail in pairs(details) do
	local ammo_type = detail.ammo_type
	if _is_weapon_name_valid(full_name) and _is_ammo_type_valid(ammo_type) then
		add_to_values_dicts(full_name, detail)
	else
		_print_detail("System Skip", detail, full_name)
	end
end
