#include "ui_extensions.lua"

function init()
end


function draw()
	local a = GetFloat("loading.alpha")
	local showText = GetBool("loading.text")

	UiMute(1)

	if a < 0 then a = 0 end
	if a > 1 then a = 1 end
	UiColor(0,0,0, a)
	UiRect(UiWidth(), UiHeight())
	
	UiColorFilter(1,1,1, a*a)
	if showText then
		UiFont("regular.ttf", 32)
		UiAlign("center middle")
		UiTranslate(UiCenter(), UiMiddle())
		UiColor(0.96, 0.96, 0.96)
		local scale = 1+(1-a)
		scale = scale * (1.0 - a * 0.05 * math.cos(GetTime() * math.pi)) -- awesome pulsation
		UiPush()
			local showLargeUI = GetBool("loading.largeui")
			if showLargeUI then
				UiScale(scale*1.3)
			else 
				UiScale(scale)
			end
			UiText("loc@UI_TEXT_LOADING")
		UiPop()
		if not GetBool("loading.showmods") then
			return
		end
		
		local mods = ListKeys("mods.available")
		local activeMods = {}
		local more = 0
		for i=1, #mods do
			local active = GetBool("mods.available."..mods[i]..".active")
			if active then
			    if #activeMods > 6 and showLargeUI then
					more = more +1
				elseif #activeMods > 10 then
					more = more + 1
				else
					local name = GetString("mods.available."..mods[i]..".name")
					if string.len(name) > 128 then
						activeMods[#activeMods+1] = string.sub(name, 1, 128) .. "..."
					else
						activeMods[#activeMods+1] = name
					end
				end
			end
		end
		if more > 0 then
			activeMods[#activeMods+1] =  "loc@UI_MOD_AND_MORE,"..tostring(more) -- "...and "..more.." more"
		end
		if #activeMods > 0 then
			UiPush()
				UiFont("regular.ttf", 22)
				local activeModsText = GetTranslatedStringByKey("UI_TEXT_ACTIVE_MODS")
				local textW, textH = UiGetTextSize(activeModsText)
				local width = textW + 100
				local modPadding = 5
				local height = 0
				UiTranslate(0, 250)
				for i=1, #activeMods do
					local w,h = UiGetTextSize(activeMods[i])
					w = w + 60
					height = height + h + modPadding
					if w > width then
						width = w
					end
				end
				height = height + 35
				showLargeUI = GetBool("loading.largeui")
				if showLargeUI then
					UiScale(1.3)
				end
				UiImageBox("common/box-outline-6.png", width, height, 6, 6)
				UiWindow(width, height)
				UiAlign("left")
				UiPush()
					UiAlign("center middle")
					UiTranslateToScreenCenter()
					UiPush()
						UiColor(0,0,0)
						UiRect(textW + 14, textH + 8)
					UiPop()
					UiPush()
						UiColor(0.96, 0.96, 0.96)
						UiText(activeModsText)
					UiPop()
				UiPop()
				UiTranslateToScreenCenter(0, 35)
				UiColor(1,1,0.7)
				for i=1, #activeMods do
					UiAlign("center middle")
					UiText(activeMods[i], true)
					UiTranslate(0, modPadding)
				end
			UiPop()
		end
	end
end

