local json_tips_path = ModPath .. "/tips/"

function TipsTweakData:init()
	self.category_totals = {}
	self.tips = {}

	for _, filename in pairs(FileIO:GetFiles(json_tips_path)) do
		local full_path = json_tips_path .. filename
		local category = filename:sub(1, -6)

		local file = io.open(full_path, "r")
		local category_data = json.decode(file:read("*all"))
		file:close()

		for category_index, tip_data in pairs(category_data) do
			table.insert(self.tips, {
				cat_index = category_index,

				title_id = tip_data.title_id or "loading_" .. category .. "_title",
				text_id = tip_data.text_id or "loading_" .. category .. "_" .. category_index,
				image = tip_data.image or "contact_bain",
				category = category
			})
		end

		self.category_totals[category] = #category_data
    end
end

function TipsTweakData:get_tip(index)
	local tip = self.tips[index]
	local image = tip.image
	local image_exists = DB:has(Idstring("texture"), "guis/textures/loading/hints/" .. image)

	if not image_exists then
		image = "contact_bain"
	end

	return {
		image = image,
		index = tip.cat_index,
		total = self.category_totals[tip.category],
		title = managers.localization:to_upper_text(tip.title_id),
		text = managers.localization:text(tip.text_id)
	}
end

function TipsTweakData:get_a_tip()
	return self:get_tip(math.random(#self.tips))
end

function TipsTweakData:get_all_tips()
	local all_tips = {}

	for index, tip in ipairs(self.tips) do
		table.insert(all_tips, self:get_tip(index))
	end

	return all_tips
end