-- Configuration
local unique_mod_id = "nickwick"
local body_section = "head"

local target_character_id = "jowi"
local old_object_name = "g_head_john_wick"
local new_unit = "units/pd2_mod_john/characters/head_jowi/head_jowi"

-- Functionality
local old_function_name = "_pre_" .. unique_mod_id .. "_set_beardlib_character_visual_state"
CriminalsManager[old_function_name] = CriminalsManager[old_function_name] or CriminalsManager.set_beardlib_character_visual_state
function CriminalsManager.set_beardlib_character_visual_state(unit, character_name, visual_state)
	CriminalsManager[old_function_name](unit, character_name, visual_state)

	if not alive(unit) then return end
	if unit:camera() then return end

	if character_name ~= target_character_id then return end

	local body_replacement = tweak_data.blackmarket:get_player_style_value(visual_state.player_style, character_name, "third_body_replacement") or {}
	local section_replaced = not not body_replacement[body_section]

	local spawn_manager = unit:spawn_manager()
	if spawn_manager then
		spawn_manager:remove_unit("replaced_" .. body_section .. "_mesh")
	end

	if not section_replaced then
		local old_object = unit:get_object(Idstring(old_object_name))
		if old_object then
			old_object:set_visibility(false)
		end

		if spawn_manager then
			spawn_manager:spawn_and_link_unit("_char_joint_names", "replaced_" .. body_section .. "_mesh", new_unit)
		end
	end

	if unit:interaction() then
		unit:interaction():refresh_material()
	end

	if unit:contour() then
		unit:contour():update_materials()
	end
end