--   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/>. 
-- Global context menu base
local awmtk2 = require("awmtk2")
local wibox = require("wibox")
local gears = require("gears")
local awful = require("awful")
local builder = require("builder")
local digger = require("digger")

return function(args)
    local style = awmtk2.create_style("root_menu",
        awmtk2.generic.composite_widget,{})
    local templates = awmtk2.create_template_lib("root_menu",awmtk2.templates,{})
    local t = awmtk2.build_templates(templates,style,args.vertical)
    -- Layout configuration
    local config_file = io.open(root_path.."/themes/"..global.theme.."/config/root_menu.json","r")
    local config
    if config_file then
        config = config_file:read("*a")
        config_file:close()
    else
        config = [[{"list": [{"widget": "widgets.rootcontrols"}],"vertical": true}]]
    end
    -- Not yet loaded menu popup (TODO: maybe make it more obvious that it's a global?)
    root_menu = awful.popup(t.popup({
        markup = "brainhurt the game",
        widget = wibox.widget.textbox
    }))
    -- Close popup on right click
    root_menu:connect_signal("button::press",function(_,_,_,b)
        if b == 3 then
            root_menu.visible = false
        end
    end)
    -- Build the menu based on the json config
    root_menu.widget = wibox.widget(t.popup(builder(
        config,
        {
            style = style.base,
            screen = mouse.screen,
        }
    )).widget)
    -- Generate a list of all existing menu_root objects
    local menus = digger(root_menu.widget,"menu_root")
    local root_menu_size = root_menu.width * root_menu.height
    root_menu:connect_signal("widget::size_changed",function()
        -- Attach callbacks to close the root_menu when new buttons appear
        local already_managed = {}
        for _,v in pairs(menus) do
            already_managed[v] = true
        end
        menus = digger(root_menu.widget,"menu_root")
        for _,v in pairs(menus) do
            if not already_managed[v] then
                v:connect_signal("cascade::kill",function()
                    root_menu.visible = false
                end)
            end
        end
    end)
    -- Close the prime_menu if our menu becomes invisible
    root_menu:connect_signal("property::visible",function()
        local current_root_menu_size = root_menu.width * root_menu.height
        if current_root_menu_size ~= root_menu_size then
            root_menu:emit_signal("widget::size_changed")
            root_menu_size = current_root_menu_size
        end
        if not root_menu.visible then
            for _,v in pairs(menus) do
                v:emit_signal("cascade::kill")
            end
        end
    end)
    -- If any of the menus emits cascade::kill, close the menu 
    for _,v in pairs(menus) do
        v:connect_signal("cascade::kill",function()
            root_menu.visible = false
        end)
    end
    -- Make the root_menu pop up on the desktop on right click
    local buttons = root.buttons()
    root.buttons(gears.table.join(buttons,
        awful.button({}, 3, function()
            root_menu.visible = (not root_menu.visible)
            if root_menu.visible then
                root_menu:move_next_to(gears.table.join(mouse.coords(),{
                    width = 0,
                    height = 0
                }))
            end
        end)
    ))
    return root_menu
end
