local HudPlayerControlSystem = inherited("HudPlayerControlSystem", SystemMap)

function HudPlayerControlSystem:new()
	local ms = instance(self)
	return ms
end

HudPlayerControlSystem.key = "hud_control"

function HudPlayerControlSystem:wants(ent)
	return ent.aspects.player_input
end

function HudPlayerControlSystem:ent_added(ent)
end

function HudPlayerControlSystem:ent_removed(ent)
end

function HudPlayerControlSystem:on_pickup_added(ent)
	local compare_ent = self.compare_ent
	local cmp = compare_ent and compare_ent.inventory:get_selected_item_of_type(ent.item:get_slot_type())
	if self.show_pickups then
		ent:set_hud_name_display(true, cmp)
	else
		ent:set_hud_name_display_fade(1, cmp)
	end
end

function HudPlayerControlSystem:step()
	local show_pickups = false
	local compare_ent = nil
	for ent, _ in pairs(self.ents) do
		local player = ent.player
		if player then
			if player.input:is_held("pickups") then
				show_pickups = true
				compare_ent = player.ent
				break
			end
		end
	end	
	if show_pickups ~= self.show_pickups or compare_ent ~= self.compare_ent then
		self.show_pickups = show_pickups
		self.compare_ent = compare_ent
		local pickupables = self.world:get_system_by_key("pickupables")
		for ent, _ in pairs(pickupables:get_pickupables_map()) do
			local cmp = compare_ent and compare_ent.inventory:get_selected_item_of_type(ent.item:get_slot_type())
			ent:set_hud_name_display(show_pickups, cmp)
		end	
	end
end

return HudPlayerControlSystem