-- Standard awesome libraries
local gears = require("gears")
local awful = require("awful")
local beautiful = require("beautiful")
-- Local imports
local grid = require("components.layout.grid")

-- Import theme
beautiful.init(gears.filesystem.get_configuration_dir() .. "theme.lua")

-- Load components
require("components.notifications")
require("components.screen")
require("utils.signals")

-- Autofocus a new client when previously focused one is closed
require("awful.autofocus")

-- Define layouts
awful.layout.append_default_layouts({
    awful.layout.suit.fair.horizontal,
    awful.layout.suit.tile,
    awful.layout.suit.floating,
    grid.horizontal,
    awful.layout.suit.tile.left,
    awful.layout.suit.tile.bottom,
    awful.layout.suit.tile.top,
    grid,
})


-- List of apps to run on start-up
local run_on_start_up = {
    "numlockx on",
    "xcape -e \"Super_L=Super_L|XF86Launch5\" -t 5000",
    "xmodmap /home/user/.Xmodmap",
    "feh --no-fehbg --bg-fill " .. gears.filesystem.get_configuration_dir() .. "/images/wallpaper.png",
    "sudo wg-quick up wg0"
    -- "/opt/power/powersave.sh",
    -- os.getenv("XDG_CONFIG_HOME") .. "/awesome/scripts/setup_display.sh"
}

-- Run all the apps listed in run_on_start_up
for _, app in ipairs(run_on_start_up) do
    local findme = app
    local firstspace = app:find(" ")
    if firstspace then
        findme = app:sub(0, firstspace - 1)
    end
    -- Run commands only if it's not already running.
    awful.spawn.with_shell(string.format("echo 'pgrep -f -u $USER -x %s > /dev/null || (%s)' | bash -", findme, app), false)
end

-- Start timers
gears.timer {
    timeout   = 60,
    call_now  = true,
    autostart = true,
    callback  = function()
        awful.spawn({"xmodmap", "/home/user/.Xmodmap"}) -- If not repeated it sometimes just stops working
    end
}

-- Import Keybinds
local keys = require("keys")
root.keys(keys.globalkeys)
root.buttons(keys.desktopbuttons)

-- Import rules
local create_rules = require("rules").create
awful.rules.rules = create_rules(keys.clientkeys, keys.clientbuttons)
