--[[

Custom trade injector made for CW weapon pack

Author: HarukaSai
Credits: Arti, for creating trader_autoinject.script and being amazing

Last edit: 19-10-2021
]]

-- this is where you add custom trade profiles for specific dudes
-- by default only suppliers are able to sell faction stuff
-- you can bypass this by adding trader in this table
-- [name that you can see when you look at npc in debug] = ini_file("path to your ini")

local ini_by_name = {}

local ini_by_faction = {
    ["stalker"] = ini_file("items\\trade_melee_rework\\trade_stalker.ltx"),
    ["bandit"] = ini_file("items\\trade_melee_rework\\trade_bandit.ltx"),
    ["csky"] = ini_file("items\\trade_melee_rework\\trade_csky_spore.ltx"),
    ["dolg"] = ini_file("items\\trade_melee_rework\\trade_duty.ltx"),
    ["freedom"] = ini_file("items\\trade_melee_rework\\trade_freedom.ltx"),
    ["killer"] = ini_file("items\\trade_melee_rework\\trade_mercenary.ltx"),
    ["army"] = ini_file("items\\trade_melee_rework\\trade_military_esc.ltx"),
    --["ecolog"] = ini_file("items\\trade_melee_rework\\trade_ecolog.ltx"),
    ["monolith"] = ini_file("items\\trade_melee_rework\\trade_monolith.ltx"),
    ["renegade"] = ini_file("items\\trade_melee_rework\\trade_renegade.ltx"),
    ["greh"] = ini_file("items\\trade_melee_rework\\trade_greh.ltx"),
    ["isg"] = ini_file("items\\trade_melee_rework\\trade_isg.ltx")
}

local format_table = utils_data.print_table
local to_array = utils_data.parse_ini_section_to_array
local math_random = math.random

function vks_spawn_stock(npc)
    if trader_autoinject.get_trader_type(npc) ~= trader_autoinject.SUPPLIER then
        return
    end
    
	local npc_name = npc:section()
    local community = get_trader_community(npc)
    local supply_level = trader_autoinject.supply_level(npc)

	if not supply_level then return end

	local ini_trade = ini_by_name[npc_name] or ini_by_faction[community]
	local supply_table = to_array(ini_trade, supply_level)

	if not supply_table then
		printf("! Trader has no supplies for this supply level [%s] - [%s]", npc_name, supply_level)
		return 
	end

	local to_spawn = {}

	for item, amount in pairs(supply_table) do
		local str_amount = str_explode(amount, ",")
		to_spawn[item] = chance_for_each(tonumber(str_amount[1]), tonumber(str_amount[2]))
	end
	
	trader_autoinject.spawn_items(npc, to_spawn)
end

TraderAuto = trader_autoinject.update

function trader_autoinject.update(npc)
    TraderAuto(npc)
    vks_spawn_stock(npc)
end

function chance_for_each(n, chance)
    local total = 0
    for i = 1, n do
        if math_random() < chance then
            total = total + 1
        end
    end
    return total
end

local furniture = {
    ["esc_m_trader"] = true,
    ["red_m_lesnik"] = true
}

local blacklisted_comms = {
    ["trader"] = true,
    ["monster"] = true
}

function get_trader_community(npc, default)
    if furniture[npc:name()] then
        return "stalker"
    end
    local community = character_community(npc)
    if not blacklisted_comms[community] then
        return community
    end
    local squad_community = get_object_squad(npc):get_squad_community()
    if not blacklisted_comms[squad_community] then
        return squad_community
    else
        return default
    end
end