
local function get_credits()
    local f = assert(io.open(core.get_mainmenu_path() .. "/credits.json"))
    local json = core.parse_json(f:read("*all"))
    f:close()
    return json
end

local function get_renderer_info()
    local out = {}

    local renderer = core.get_active_renderer()
    if renderer:sub(1, 7) == "OpenGL " then
        renderer = renderer:sub(8)
    end
    local m = renderer:match("^[%d.]+")
    if not m then
        m = renderer:match("^ES [%d.]+")
    end
    out[#out + 1] = m or renderer
    out[#out + 1] = core.get_active_driver():lower()
    out[#out + 1] = core.get_active_irrlicht_device():upper()

    return table.concat(out, " / ")
end

return function(state)
    imfs.begin(size.x, size.y)
        :padding(0, 0)
        :bgcolor(theme.styles.bg_color, true)
    
    if not state.credits then
        local credits = get_credits()
        state.credits = table.concat({
            "<global halign=center color=", theme.styles.text_color, ">",
            "<tag name=muted color=", theme.styles.text_color_muted, ">",
            "<tag name=link color=", theme.styles.link_color, ">",
            "<tag name=heading color=", theme.styles.heading_color, " size=24>",
            "<heading>", fgettext_ne("Core Developers"), "</heading>\n",
            core.hypertext_escape(table.concat(credits.core_developers, "\n")),
            "\n\n<heading>", fgettext_ne("Core Team"), "</heading>\n",
            core.hypertext_escape(table.concat(credits.core_team, "\n")),
            "\n\n<heading>", fgettext_ne("Active Contributors"), "</heading>\n",
            core.hypertext_escape(table.concat(credits.contributors, "\n")),
            "\n\n<heading>", fgettext_ne("Previous Core Developers"), "</heading>\n",
            core.hypertext_escape(table.concat(credits.previous_core_developers, "\n")),
            "\n\n<heading>", fgettext_ne("Previous Contributors"), "</heading>\n",
            core.hypertext_escape(table.concat(credits.previous_contributors, "\n")),
            "\n\n",
        }):gsub("%[.-%]", "<muted>%1</muted>"):gsub("\\<.-\\>", "<link>%1</link>")
    end
    
    local header_height = size.x * (72/672)
    imfs.image(0, 0.2, size.x, header_height, default_textures.."menu_header.png")
    
    imfs.hypertext(0, header_height + 0.3, "100%", 0.5, "<global halign=center valign=middle color="..theme.styles.text_color.."><b>Version "..version.string.."</b>")
    
    imfs.hypertext(0, header_height + 0.9, "100%", "100% - "..(header_height + 0.9), state.credits)
    
    imfs.button("100% - 2.2", 0.2, 2, 0.75, "Back")
        :onclick(function()
            show_meta_menu()
        end)
    
    imfs.button(0.2, 0.2, 3, 0.75, "API reference")
        :onclick(function()
            
        end)

    imfs.button(0.2, 1.05, 3, 0.75, "Website")
        :onclick(function()
            core.open_url("https://luanti.org")
        end)
    
    imfs.button(0.2, 1.9, 3, 0.75, "Open data directory")
        :onclick(function()
            core.open_dir(core.get_user_path())
        end)
    
    imfs.box(0, "100% - 0.45", 6, 0.45, theme.styles.bg_color)
    imfs.label(0.2, "100% - 0.225", fgettext_ne("Active renderer:").." "..get_renderer_info())
    
    local offset = math.abs(size.x - size.y)
    imfs.image(size.x > size.y and offset / 2 or 0, size.x < size.y and offset / 2 or 0, math.min(size.x, size.y), math.min(size.x, size.y), default_textures.."logo.png^[opacity:32")
    
    return imfs.end_()
end
