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

local print_debug = NSGlobal.utils.register.print_debug

require("Items/ProceduralDistributions")

local add_distribution_tables = function(silencer_item_name, multiplier)
	if multiplier > 0.0 then
		-- table.insert(ProceduralDistributions.list.PoliceLockers.items, silencer_item_name)
		-- table.insert(ProceduralDistributions.list.PoliceLockers.items, 0.4 * multiplier)
		table.insert(ProceduralDistributions.list.PoliceStorageGuns.items, silencer_item_name)
		table.insert(ProceduralDistributions.list.PoliceStorageGuns.items, 0.65 * multiplier)
		table.insert(ProceduralDistributions.list.PawnShopGuns.items, silencer_item_name)
		table.insert(ProceduralDistributions.list.PawnShopGuns.items, 0.35 * multiplier)
		table.insert(ProceduralDistributions.list.GunStoreShelf.items, silencer_item_name)
		table.insert(ProceduralDistributions.list.GunStoreShelf.items, 0.75 * multiplier)
		table.insert(ProceduralDistributions.list.GunStoreCounter.items, silencer_item_name)
		table.insert(ProceduralDistributions.list.GunStoreCounter.items, 0.85 * multiplier)
		table.insert(ProceduralDistributions.list.HuntingLockers.items, silencer_item_name)
		table.insert(ProceduralDistributions.list.HuntingLockers.items, 1 * multiplier)
		table.insert(ProceduralDistributions.list.ArmyStorageGuns.items, silencer_item_name)
		table.insert(ProceduralDistributions.list.ArmyStorageGuns.items, 1.25 * multiplier)
		-- table.insert(ProceduralDistributions.list.GunStoreDisplayCase.items, silencer_item_name)
		-- table.insert(ProceduralDistributions.list.GunStoreDisplayCase.items, 0.75 * multiplier)
	end
end

local fn = function()
	local ammo_types_in_system = NSGlobal.utils.system.get_ammo_types()

	local silencer_dists = {}
	for _, ammo_type in ipairs(ammo_types_in_system) do
		local silencer_item_name = NSGlobal.utils.register.get_ammo_silencer_item_name(ammo_type)
		local silencer_item_dist_sandbox_name =
			NSGlobal.utils.register.convert_to_sandbox_name(ammo_type, "distribution")
		local silencer_item_dist = SandboxVars.NS[silencer_item_dist_sandbox_name]
		silencer_dists[silencer_item_name] = silencer_item_dist
	end

	for silencer_item_name, silencer_item_dist in pairs(silencer_dists) do
		local mult = SandboxVars.NS.BaseLootMultiplier
		if silencer_item_dist == 1 then
			mult = 0.0 -- none
		elseif silencer_item_dist == 2 then
			mult = mult * 0.05 -- incredibly rare
		elseif silencer_item_dist == 3 then
			mult = mult * 0.2 -- extremely rare
		elseif silencer_item_dist == 4 then
			mult = mult * 0.6 -- rare
		elseif silencer_item_dist == 5 then
			mult = mult * 1.0 -- normal
		elseif silencer_item_dist == 6 then
			mult = mult * 2.0 -- common
		elseif silencer_item_dist == 7 then
			mult = mult * 3.0 -- abundant
		end
		print_debug("[Distribution] silencer_item: " .. silencer_item_name .. " mult: " .. mult)
		add_distribution_tables(silencer_item_name, mult)
	end
	ItemPickerJava.Parse()
end

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