--   This file is part of Reno desktop.
--
--  Reno desktop is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
--
--  Reno desktop is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
--
--  You should have received a copy of the GNU General Public License along with Reno desktop. If not, see <https://www.gnu.org/licenses/>. 
-- XDG application menu for the global context menu
local awmtk2 = require("awmtk2")
local wibox = require("wibox")
local gears = require("gears")
local menugen = require("context_menu")
local menuutils = require("menubar").utils

local count = function(t)
    local n = 0
    for _,_ in pairs(t) do
        n = n + 1
    end
    return n
end

return function(args)
    local style = awmtk2.create_style("xdg_menu",
        awmtk2.generic.menu,args.style,args.vertical)
    local templates = awmtk2.create_template_lib("xdg_menu",awmtk2.templates,args.templates)
    local t = awmtk2.build_templates(templates,style,args.vertical)
    -- Add a "loading" indicator while XDG is still parsing data
    local widget = wibox.widget({
        t.container({
            markup = "Loading XDG menu...",
            widget = wibox.widget.textbox
        }),
        layout = wibox.layout.fixed.vertical,
        spacing = 0,
        id = "xdg_menu_root"
    })
    local function exclude(name)
        for _,v in pairs(args.exclude or {}) do
            if name:match(v) then
                return true
            end
        end
        return false
    end
    awesome.connect_signal("xdg::all_finished",function()
        local items = {}
        for k,v in pairs(xdg.categories) do
            local noprocess = false
            for _,v2 in pairs(args.exclude_category or {}) do
                if k == v2 then
                    noprocess = true
                end
            end
            if (not noprocess) and (count(v.apps) > 0) then
                local category = {k,{},menuutils.lookup_icon_uncached(v.icon)}
                for _,item in pairs(v.apps) do
                    if not exclude(item.name) then
                        table.insert(category[2], {
                            item.name,
                            item.exec:gsub("%%%w","") or "",
                            gears.filesystem.file_readable(item.icon or "") and item.icon
                        })
                    end
                end
                table.insert(items,category)
            end
        end
        -- uhhh there's a lot of things about async, some of which i can't explain
        local xdg_menu_root = widget:get_children_by_id("xdg_menu_root")[1]
        xdg_menu_root:reset()
        menugen({
            items = items,
            parent = args.menu_parent
        })
    end)
    return widget
end
