-- Module that adds default keybindings
local awful = require("awful")
local gears = require("gears")

global.modkey = global.modkey or "Mod4"
local keys = gears.table.join(
    awful.key({global.modkey}, "Up",
        function()
            awful.client.focus.byidx(1)
        end,
    {description = "switch to next client", group = "client"}),
    awful.key({global.modkey}, "Down",
        function()
            awful.client.focus.byidx(-1)
        end,
    {description = "switch to previous client", group = "client"}),
    awful.key({global.modkey, "Control"}, "Up",
        function()
            awful.screen.focus_relative(1)
        end,
    {description = "switch to next screen", group = "screen"}),
    awful.key({global.modkey, "Control"}, "Down",
        function()
            awful.screen.focus_relative(-1)
        end,
    {description = "switch to previous screen", group = "screen"}),
    awful.key({global.modkey}, "Tab",
        function()
            awful.client.focus.history.previous()
            if client.focus then
                client.focus:raise()
            end
        end,
    {description = "go back", group = "client"}),
    awful.key({global.modkey}, "Return",
        function()
            awful.spawn(global.terminal)
        end,
    {description = "Open terminal", group = "launcher"}),
    awful.key({global.modkey, "Shift"}, "Return",
        function()
            awful.spawn(global.browser)
        end,
    {description = "Open browser", group = "launcher"}))
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(
    awful.key({global.modkey, "Shift"},"c",
        function(c)
            c:kill()
        end,
    {description = "close client", group = "client"}),
    awful.key({global.modkey}, "o",
        function(c)
            c:move_to_screen()
        end,
    {description = "move to screen", group = "client"}),
    awful.key({global.modkey}, "t",
        function(c)
            c.ontop = not c.ontop
        end,
    {description = "toggle ontop", group = "client"}),
    awful.key({global.modkey}, "b",
        function(c)
            c.below = not c.below
        end,
    {description = "toggle below", group = "client"}),
    awful.key({global.modkey}, "f",
        function(c)
            c.fullscreen = not c.fullscreen
            c:raise()
        end,
    {description = "toggle fullscreen", group = "client"}),
     awful.key({ global.modkey }, "n",
        function (c)
            c.minimized = true
        end ,
    {description = "minimize", group = "client"}),
    awful.key({ global.modkey }, "m",
        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