alert = {
    texts = {},
    activeTexts = 0,
}

whitelisted_fns_map = {
    alert = {
        SetText = true,
        ClearText = true,
    }
}

function alert:Init()
    RegisterListenerToFiltered(EVENTS.UI_ALERT_SHOW, 'CallWithArgs', whitelisted_fns_map)
    RegisterListenerToFiltered(EVENTS.UI_ALERT_HIDE, 'CallWithArgs', whitelisted_fns_map)
end

local ico = {}
ico.__index = function(_, key)
	local ico = Ui.ActionIcons[key]
	ico.style = "white"
    ico.iW = 50
    ico.iH = 50
	return ico
end
setmetatable(ico, ico)

function alert:Draw(dt)
    if self.activeTexts == 0 then return end
    UiPush()

    UiTranslate(UiCenter(), UiMiddle()/2)
    if GetBool("level.show_err") then
        UiTranslate(0, 88)
    end

    local is_gamepad = LastInputDevice() == UI_DEVICE_GAMEPAD
    UiFont("regular.ttf", 50)
    local r, g, b, a = SetColorFromHEX("#F2E040")
    for k, text in pairs(self.texts) do
        local text1, text2, text3
        if type(text) == 'table' then
            text1 = GetTranslatedStringByKey(text[1])
            local dur = text[3]
            if dur and k ~= "loc@NITRO_HINT" then
                text[2] = tonumber(text[2]) - dt
                if text[2] <= 0 then
                    self:ClearText(k)
                    UiPop()
                    return
                end
            end
        else
            text1 = GetTranslatedStringByKey(text)
        end
        local left, right = text1:find"{", text1:find"}"
        if left and right then
            text1, text2, text3 = text1:sub(1, left-1), text1:sub(left+1, right-1), text1:sub(right+1)
            if text2 == "NITROKEY" then
                text2 = is_gamepad and ico["driving:interact"] or ico["key:" .. GetString("savegame.mod.input.nitro")]
            end
        end
        local total_width = UIUtils.UiTextSizeAfterWrap({text1, text2, text3})
        local ico_w, ico_h
        if type(text2) == "table" then
            ico_w, ico_h = text2.w, text2.h
        end
        UiPush()
            UiAlign('center middle')
            PAGE_COMPONENTS:drawRoundedColorRect(total_width + 48, 76, 12, 0, 0, 0, 0.5)
        UiPop()
        UiPush()
            UiTranslate(-(total_width+48)/2 + 24, 12)
            UiTranslate((UiText(text1)))
            if text2 then
                UiPush()
                    UiColorFilter(r, g, b, a)
                    UiTranslate(0, -ico_h + 6)
                    text2:draw()
                UiPop()
                UiTranslate(ico_w or text2 and UiGetTextSize(text2))
            end
            if text3 then UiText(text3) end
        UiPop()
        UiTranslate(0, 88)
    end

    UiPop()
end

function alert:SetText(text, subtext, subtextIsDuration)
    if text and text ~= '' and not self.texts[text] then
        if subtext then
            self.texts[text] = {text, subtext, subtextIsDuration:as_bool()}
        else
            self.texts[text] = text
        end
        self.activeTexts = self.activeTexts+1
    end
end

function alert:ClearText(text)
    if text and text ~= '' and self.texts[text] then
        self.texts[text] = nil
        self.activeTexts = self.activeTexts-1
    end
end
