--   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/>. 
-- Module that adds default keybindings
local awful = require("awful")
local gears = require("gears")
global.modkey = global.modkey or "Mod4"

local function get_keycomb(name)
    local modifiers = {}
    name = name:gsub("[^%s%+]+%+",function(v)
        if v == "modkey+" then v = global.modkey.."+" end
        table.insert(modifiers,v:sub(1,-2))
        return ""
    end)
    return modifiers,name
end

local keymap = keybindings

local function k(name,callback,description)
    if not keymap[name] then
        return {}
    end
    local modifiers,key = get_keycomb(keymap[name])
    return awful.key(modifiers,key,
        callback,
    description)
end

local custom_keys = {}

for comm,bind in pairs(keymap) do
    if not comm:match("^:.*") then
        table.insert(custom_keys,k(comm,function()
            awful.spawn(comm)
        end,{description = bind, group = "custom"}))
    end
end

local keys = gears.table.join(
    k(':root.client_next',
        function()
            awful.client.focus.byidx(1)
        end,
    {description = "switch to next client", group = "client"}),
    k(":root.client_previous",
        function()
            awful.client.focus.byidx(-1)
        end,
    {description = "switch to previous client", group = "client"}),
    k(":root.screen_next",
        function()
            awful.screen.focus_relative(1)
        end,
    {description = "switch to next screen", group = "screen"}),
    k(":root.screen_previous",
        function()
            awful.screen.focus_relative(-1)
        end,
    {description = "switch to previous screen", group = "screen"}),
    k(":root.client_swap",
        function()
            awful.client.focus.history.previous()
            if client.focus then
                client.focus:raise()
            end
        end,
    {description = "go back", group = "client"}),
    k(":root.spawn_terminal",
        function()
            awful.spawn(global.terminal)
        end,
    {description = "open terminal", group = "launcher"}),
    k(":root.spawn_browser",
        function()
            awful.spawn(global.browser)
        end,
    {description = "open browser", group = "launcher"}),
    table.unpack(custom_keys))
root.keys(keys)

local buttons = gears.table.join(
    awful.button({}, 3, function() end),
    awful.button({}, 4, awful.tag.viewnext),
    awful.button({}, 5, awful.tag.viewprev)
)
root.buttons(buttons)

local clientkeys = gears.table.join(
    k(":client.kill",
        function(c)
            c:kill()
        end,
    {description = "close client", group = "client"}),
    k(":client.cycle_screen",
        function(c)
            c:move_to_screen()
        end,
    {description = "move to screen", group = "client"}),
    k(":client.ontop",
        function(c)
            c.ontop = not c.ontop
        end,
    {description = "toggle ontop", group = "client"}),
    k(":client.below",
        function(c)
            c.below = not c.below
        end,
    {description = "toggle below", group = "client"}),
    k(":client.fullscreen",
        function(c)
            c.fullscreen = not c.fullscreen
            c:raise()
        end,
    {description = "toggle fullscreen", group = "client"}),
     k(":client.minimize",
        function (c)
            c.minimized = true
        end ,
    {description = "minimize", group = "client"}),
    k(":client.maximize",
        function (c)
            c.maximized = not c.maximized
            c:raise()
        end ,
    {description = "(un)maximize", group = "client"}))
awful.rules.rules[1].properties.keys = clientkeys

local clientbuttons = gears.table.join(
    awful.button({ }, 1, function (c)
        c:emit_signal("request::activate", "mouse_click", {raise = true})
    end),
    awful.button({ global.modkey }, 1, function (c)
        c:emit_signal("request::activate", "mouse_click", {raise = true})
        awful.mouse.client.move(c)
    end),
    awful.button({ global.modkey }, 3, function (c)
        c:emit_signal("request::activate", "mouse_click", {raise = true})
        awful.mouse.client.resize(c)
    end))
awful.rules.rules[1].properties.buttons = clientbuttons

