pName = GetStringParam("name", "img")
pShowTime = GetFloatParam("showTime", 10)

function init()
    t = 0
end

function tick()
    screen = UiGetScreen()
    pName = GetTagValue(screen, "name")
    t = t + GetTimeStep()
end

local MaxPageHeight = 290
local MaxPageHeightNoWarning = 390
local Spacing = 20

function copy(obj, seen)
    if type(obj) ~= 'table' then return obj end
    if seen and seen[obj] then return seen[obj] end
    local s = seen or {}
    local res = setmetatable({}, getmetatable(obj))
    s[obj] = res
    for k, v in pairs(obj) do res[copy(k, s)] = copy(v, s) end
    return res
end

-- Caclculate how many pages do we need to show and put all strings there
function splitToPages(baseKey, maxHeight)
    local base = string.upper(baseKey)
    local pages = {}
    local currentPage = {}
    local hasKey = true
    local index = 1
    local pageHeight = 0
    local lastOffset = 0
    while hasKey do
        local key = "EDITOR_SHOWCASE_" .. base .. "_" .. tostring(index)
        if HasTranslationByKey(key) then
            local str = GetTranslatedStringByKey(key)
            local _, h = UiGetTextSize(str)
            -- Can't find next string to current page, except if it's first line to prevent infinite loop
            if pageHeight + h > maxHeight and #currentPage > 0 then
                pages[#pages + 1] = copy(currentPage)
                currentPage = {}
                pageHeight = 0
                lastOffset = 0
            end
            currentPage[#currentPage + 1] = { text = str, offset = lastOffset }
            lastOffset = h + Spacing
            pageHeight = pageHeight + lastOffset
            index = index + 1
        else
            hasKey = false
            if #currentPage > 0 then
                -- Add last page if we have anything there
                pages[#pages + 1] = copy(currentPage)
            end
        end
    end

    return pages
end

function drawPageContent(page)
    UiPush()
        UiColor(0.965, 0.957, 0.047)
        UiFont("regular.ttf", 26)
        UiWordWrap(586)
        UiAlign("left top")
        for _, val in pairs(page) do
            UiTranslate(0, val.offset)
            UiText(val.text)
        end
    UiPop()
end

function drawTutorial(baseKey)
    -- Split to pages every frame because language can change but we can't tell when
    local base = string.upper(baseKey)
    local warnignKey = "EDITOR_SHOWCASE_" .. base .. "_WARNING"
    local hasWarning = HasTranslationByKey(warnignKey)
    UiPush()
        UiFont("regular.ttf", 26)
        UiWordWrap(586)
        UiAlign("left top")
        local maxHeight = MaxPageHeight
        if not hasWarning then
            maxHeight = MaxPageHeightNoWarning
        end
        local cachedPages = splitToPages(baseKey, maxHeight)
    UiPop()

    local pageIndex = math.floor(math.fmod(t / pShowTime, #cachedPages) + 1)

    UiTranslate(30, 28)

    UiPush()
        local title = GetTranslatedStringByKey("EDITOR_SHOWCASE_" .. base .. "_TITLE")
        UiColor(1, 1, 1)
        UiPush()
            UiColor(0.082, 0.043, 0.969)
            UiRect(586, 47)
            UiTranslate(284, 24)
            UiColor(0.965, 0.957, 0.047)
            UiAlign("center middle")
            UiFont("bold.ttf", 30)
            UiText(title)
        UiPop()
        if cachedPages[pageIndex] ~= nil then
            UiPush()
                UiTranslate(0, 60)
                drawPageContent(cachedPages[pageIndex])
            UiPop()
        end
        if hasWarning then
            UiPush()
                UiColor(0.957, 0.455, 0.451)
                UiTranslate(0, 380)
                UiRect(586, 47)
                UiColor(1, 1, 1)
                UiFont("bold.ttf", 24)
                local text = GetTranslatedStringByKey(warnignKey)
                local w, _ = UiGetTextSize(text)
                UiTranslate(294, 24)
                if w > 570 then -- Consider some padding
                    UiScale(570/w)
                end
                UiAlign("center middle")
                UiText(text)
            UiPop()
        end
    UiPop()
end

function draw()
    if screen == 0 then
        return
    end

    UiPush()
        drawTutorial(pName)
    UiPop()
end
