local UseItemSystem = inherited("UseItemSystem", SystemMap)

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

function UseItemSystem:dynamic_wants(ent)
	return ent.aspects.use_item and ent.use_slot
end

function UseItemSystem:ent_added(ent)
end

function UseItemSystem:ent_removed(ent)
end

function UseItemSystem:step(dt)
	for ent, _ in pairs(self.ents) do
		local use_slot = ent.use_slot
		if use_slot then
			local item = use_slot:get_item()
			if item then
				local effect = item:get_effect()
				if effect then
					effect:apply_to(ent)					
				end
				if item:has_stack() then
					item:adjust_stack(-1)
					if item:get_stack() <= 0 then
						use_slot:clear_item()	
						if ent.inventory_changed then
							ent:inventory_changed()
						end
					end
				else
					use_slot:clear_item()
					if ent.inventory_changed then
						ent:inventory_changed()
					end
				end
			end
			ent:set_use_slot_intent(nil)
		end
	end
end

return UseItemSystem