-- Original name: zSA_SystemUtils.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.

-- This file contains utility functions that are used to access or edit mod's active system data

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

local print_debug = NSGlobal.utils.register.print_debug

NSGlobal.utils.system = {}

-- Returns a table of all ammo types and their corresponding multipliers
-- @return: table containing ammo types and their corresponding multipliers
NSGlobal.utils.system.get_ammo_types_multipliers = function()
	local out = {}
	local supported_ammo_types = NSGlobal.register.default.supported_ammo_types
	local get_ammo_type_multiplier = NSGlobal.utils.register.get_ammo_type_multiplier
	for _, ammo_type in ipairs(supported_ammo_types) do
		out[ammo_type] = get_ammo_type_multiplier(ammo_type)
	end
	return out
end

-- Returns a table of all ammo types that are currently in the game system
-- @return: table containing ammo types that are in the system
NSGlobal.utils.system.get_ammo_types = function()
	local ammo_types = {}
	for _, details in pairs(NSGlobal.system.rweapon_details) do
		ammo_types[details.ammo_type] = true
	end
	local out = {}
	for ammo_type, _ in pairs(ammo_types) do
		table.insert(out, ammo_type)
	end
	return out
end

-- Returns a table of all ammo types that are not currently in the game system
-- @return: table containing ammo types that are not in the system
NSGlobal.utils.system.get_ammo_types_not_in_system = function()
	local ammo_types_in_system = NSGlobal.utils.system.get_ammo_types()
	local supported_ammo_types = NSGlobal.register.default.supported_ammo_types

	-- find whats not in the system
	local out = {}
	for _, ammo_type in ipairs(supported_ammo_types) do
		local found = false
		for _, ammo_type_in_system in ipairs(ammo_types_in_system) do
			if ammo_type == ammo_type_in_system then
				found = true
				break
			end
		end
		if not found then
			table.insert(out, ammo_type)
		end
	end
	return out
end

-- Get silencer item names that are in the system
-- @return: table containing silencer item names that are in the system
NSGlobal.utils.system.get_silencer_items = function()
	local _rweapon_details = NSGlobal.system.rweapon_details
	local _get_silencer_item_name = NSGlobal.utils.register.get_ammo_silencer_item_name
	local out = {}
	for _, detail in pairs(_rweapon_details) do
		local silencer_item_name = _get_silencer_item_name(detail.ammo_type)
		if out[silencer_item_name] == nil then
			table.insert(out, silencer_item_name)
		end
	end
	return out
end

-- Get silencer item names that are not in the system
-- @return: table containing silencer item names that are not in the system
NSGlobal.utils.system.get_silencer_items_not_in_system = function()
	local _in_system = NSGlobal.utils.system.get_silencer_items()
	local out = {}

	for _, silencer_item_name in ipairs(NSGlobal.register.default.silencer_item_names) do
		local found = false
		for _, silencer_item_in_system in ipairs(_in_system) do
			if silencer_item_name == silencer_item_in_system then
				found = true
				break
			end
		end
		if not found then
			table.insert(out, silencer_item_name)
		end
	end
	return out
end

-- Get corresponding sound increase values for ammo type
-- @param ammo_type: ammo type to get sound increase values for
-- @return: table containing sound increase values
NSGlobal.utils.system.get_sound_details = function(ammo_type)
	local sound_increase = NSGlobal.utils.register.get_ammo_type_sound_increase(ammo_type)
	local out = {}
	if sound_increase then
		out["sound_radius"] = sound_increase
		out["sound_volume"] = sound_increase
	else
		out["sound_radius"] = 0
		out["sound_volume"] = 0
	end
	return out
end

-- Returns true if table contains x, false otherwise
-- @param tbl: table to search
-- @param x: value to search for
-- @return: true if table contains x, false otherwise
NSGlobal.utils.system.table_contains = function(tbl, x)
	for _, v in pairs(tbl) do
		if v == x then
			return true
		end
	end
	return false
end
