
local function search_content(state, search)
    local out = {}
    
    for i = 1, #state.content do
        local pkg = state.content[i]
        if pkg.name:find(search, 1, true) then
            out[#out + 1] = pkg
        end
    end
    
    return out
end

return function(state)
    imfs.begin(size.x, size.y)
        :padding(0, 0)
        :bgcolor(theme.styles.bg_color, true)
    
    if not state.content then
        state.content = package.all()
        state.content_shown._val = state.content
    end
    
    local current_package = state.current_package()
    
    imfs.box(0, 0, "100%", current_package and 1 or 2, theme.styles.container.bg_color)
    imfs.row(0.2, 0.125, "100% - 0.4", 0.75)
        :gap(0.1)
        theme.field(0, 0, "1x", "100%", state.search)
            :onchange(function(value)
                state.search = value
            end)
            :onenter(function()
                state.page(1)
                state.content_shown(search_content(state, state.search))
            end)
        imfs.button(0, 0, 0.75, 0.75)
            :image(theme.icon("search"))
            :style({
                bgimg = theme.get_background_image("button"),
                bgimg_middle = theme.styles.button.border_width,
            })
            :tooltip("Search content")
            :onclick(function()
                state.page(1)
                state.content_shown(search_content(state, state.search))
            end)
        
         imfs.button(0, 0, 0.75, 0.75)
             :image(theme.icon("cancel"))
             :style({
                 bgimg = theme.get_background_image("button"),
                 bgimg_middle = theme.styles.button.border_width,
             })
             :tooltip("Clear search")
             :onclick(function()
                state.search = ""
                state.page(1)
                state.content_shown(state.content)
             end)
         imfs.button(0, 0, 3.5, 0.75, "Check for updates")
             :onclick(function()
                 
             end)
        imfs.button(0, 0, 2, 0.75, "Back")
            :onclick(function()
                if current_package then
                    state.current_package(false)
                else
                    show_meta_menu()
                end
            end)
    imfs.row_end()
    imfs.box(0, 0.95, "100%", 0.1, theme.styles.container.border_color)
    
    if current_package then
        imfs.hypertext(0.25, 1.05, "100% - 0.5", "100% - 1.05", "\
            <global color="..theme.styles.text_color..">\
            <tag name=action color="..theme.styles.link_color.." hovercolor="..theme.styles.link_color_hovered..">\
            "..markdown_to_hypertext(read_file(current_package.path.."/README.md") or ""))
    else
        imfs.row(0.2, 1.125, "100% - 0.4", 0.75)
            :gap(0.1)
            
            imfs.group(0, 0, "1x", 0.75)
                imfs.image(0, 0, "100%", "100%", theme.get_background_image("button"), "8,8")
                imfs.row(0.15, 0.05, "100% - 0.3", "100% - 0.13")
                    local show_type = state.show_type()
                    imfs.button(0, 0, "1x", "100%", "All")
                        :style(show_type == "all" and style_borderless_alt or style_borderless)
                        :style("hovered", style_borderless_hovered)
                        :style("pressed", style_borderless_hovered)
                        :onclick(function()
                            if show_type ~= "all" then
                                state.show_type("all")
                                state.page(1)
                                state.content = package.all()
                                if state.search ~= "" then
                                    state.content_shown(search_content(state, state.search))
                                else
                                    state.content_shown(state.content)
                                end
                            end
                        end)
                    imfs.box(0, 0, 0.1, "100%", theme.styles.container.border_color)
                    imfs.button(0, 0, "1x", "100%", "Games")
                        :style(show_type == "games" and style_borderless_alt or style_borderless)
                        :style("hovered", style_borderless_hovered)
                        :style("pressed", style_borderless_hovered)
                        :onclick(function()
                            if show_type ~= "games" then
                                state.show_type("games")
                                state.page(1)
                                state.content = package.games()
                                if state.search ~= "" then
                                    state.content_shown(search_content(state, state.search))
                                else
                                    state.content_shown(state.content)
                                end
                            end
                        end)
                    imfs.box(0, 0, 0.1, "100%", theme.styles.container.border_color)
                    imfs.button(0, 0, "1x", "100%", "Mods")
                        :style(show_type == "mods" and style_borderless_alt or style_borderless)
                        :style("hovered", style_borderless_hovered)
                        :style("pressed", style_borderless_hovered)
                        :onclick(function()
                            if show_type ~= "mods" then
                                state.show_type("mods")
                                state.page(1)
                                state.content = package.mods()
                                if state.search ~= "" then
                                    state.content_shown(search_content(state, state.search))
                                else
                                    state.content_shown(state.content)
                                end
                            end
                        end)
                    imfs.box(0, 0, 0.1, "100%", theme.styles.container.border_color)
                    imfs.button(0, 0, "1x", "100%", "Texture Packs")
                        :style(show_type == "texturepacks" and style_borderless_alt or style_borderless)
                        :style("hovered", style_borderless_hovered)
                        :style("pressed", style_borderless_hovered)
                        :onclick(function()
                            if show_type ~= "texturepacks" then
                                state.show_type("texturepacks")
                                state.page(1)
                                state.content = package.texturepacks()
                                if state.search ~= "" then
                                    state.content_shown(search_content(state, state.search))
                                else
                                    state.content_shown(state.content)
                                end
                            end
                        end)
                imfs.row_end()
            imfs.group_end()
            
            imfs.button(0, 0, 3.5, 0.75, "Browse online content...")
                :onclick(function()
                    show_online_content_menu()
                end)
            imfs.button(0, 0, 2.5, 0.75, "New package...")
                :onclick(function()
                    
                end)
        imfs.row_end()
            
        imfs.box(0, 1.95, "100%", 0.1, theme.styles.container.border_color)

        local content = state.content_shown()
        if #content == 0 then
            imfs.hypertext(0, 2.05, "100%", 2, [[
                <global halign=center valign=middle color=#444>
                <style size=36><b>No results.</b></style>
            ]])
        else
            local num_per_page = math.floor(size.x / 3) * math.floor((size.y - 2) / 3)
            local pages = math.ceil(#content / num_per_page)
            imfs.scroll_container(0, 2.05, "100%", "100% - 2.55", "horizontal", 0, "")
                :scrollbar(function()
                    imfs.scrollbar(-800, -800, 0, 0, "horizontal", state.page)
                        :options({
                            min = 1,
                            max = pages,
                            smallstep = 1
                        })
                        :onchange(function(action, value)
                            if action == "CHG" then
                                state.page(value)
                            end
                        end)
                end)
                local x = 0
                local y = 0
                local start = (state.page() - 1) * num_per_page
                for i = start + 1, start + num_per_page do
                    local pkg = content[i]
                    if not pkg then break end
                    if not pkg.icon then
                        pkg.icon = pkg.type == "game" and pkg.path.."/menu/icon.png" or pkg.path.."/icon.png"
                        if not file_exists(pkg.icon) then
                            pkg.icon = theme.icon("menu_content")
                        end
                    end
                    
                    imfs.button(x + 0.125, y, 2.5, 2.5)
                        :image(pkg.icon)
                        :tooltip(pkg.title ~= "" and pkg.title or pkg.name)
                        :onclick(function()
                            state.current_package(pkg)
                        end)
                    x = x + 3
                    if x + 2.5 > size.x then
                        x = 0
                        y = y + 3
                    end
                end
            imfs.scroll_container_end()
    
            imfs.row(0, "100% - 0.375", "100%", 0.25)
                :align("center")
                for i = 1, pages do
                    imfs.button(0, 0, 0.25, 0.25)
                        :image(theme.icon(i == state.page() and "circle_active" or "circle"))
                        :onclick(function()
                            state.page(i)
                        end)
                end
            imfs.row_end()
        end
    end
    
    return imfs.end_()
end
