local gears = require("gears")
local beautiful = require("beautiful")
local awful = require("awful")
local wibox = require("wibox")

function set_wallpaper(s)
    if beautiful.wallpaper then
        local wallpaper = beautiful.wallpaper
        if type(wallpaper) == "function" then
            wallpaper = wallpaper(s)
        end
        gears.wallpaper.maximized(wallpaper, s, true)
    end
end

local function set_transparency(t)
    -- Percentage xx/100
    awful.spawn("sed -i 's/[0-9]*:class_g/" .. t .. ":class_g/g' /home/nick/.config/picom/picom.conf", false)
    -- Integer 0.xx/1.00
    awful.spawn("sed -i 's/opacity: [0-9]*.[0-9]*/opacity: " .. t/100 .. "/g' /home/nick/.config/alacritty/alacritty.yml", false)
    -- Binary xx/255
    awful.spawn("sed -i 's/\"glassit.alpha\": [0-9]*/\"glassit.alpha\": " .. math.floor((t/100)*255 + 0.5) .. "/g' /home/nick/.config/Code/User/settings.json", false)
end

local theme = "abstract_office"

-- Cleanup running theme applications
awful.spawn("pkill conky", false)

-- Per-theme settings
if theme == "nordic_legion" then
    beautiful.init(gears.filesystem.get_configuration_dir() .. "theme/nordic_legion/theme.lua")
    beautiful.font = "Ubuntu Sans 11"
    beautiful.useless_gap = 7
    beautiful.gap_single_client = true

    beautiful.wallpaper = "/home/nick/Pictures/wallpapers/nordic_legion/wallpaper.png"

    -- Launch Conky
    awful.spawn.with_shell("killall conky; conky -c /home/nick/.config/conky/conky_left.conf &>/dev/null & disown; conky -c /home/nick/.config/conky/conky_right.conf &>/dev/null & disown", false)

    -- Configure Glava
    awful.spawn("sed -i 's/#request mod .*/#request mod bars/g' /home/nick/.config/glava/rc.glsl", false)
    awful.spawn("sed -i 's/#request setgeometry .*/#request setgeometry 0 0 2560 1600/g' /home/nick/.config/glava/rc.glsl", false)

elseif theme == "abstract_office" then
    beautiful.init(gears.filesystem.get_configuration_dir() .. "theme/abstract_office/theme.lua")
    beautiful.wallpaper = "/home/nick/Pictures/wallpapers/abstract_office/Cage.jpg"

    -- Configure Glava
    awful.spawn("sed -i 's/#request mod .*/#request mod bars/g' /home/nick/.config/glava/rc.glsl", false)
    awful.spawn("sed -i 's/#request setgeometry .*/#request setgeometry 0 0 2560 1600/g' /home/nick/.config/glava/rc.glsl", false)

end

-- Update external configs
awful.spawn("sed -i 's/corner-radius = [0-9]*/corner-radius = " .. beautiful.corner_radius .. "/g' /home/nick/.config/picom/picom.conf", false)
set_transparency(beautiful.transparency)

-- Restart Glava if it's running
awful.spawn.with_shell("if pgrep glava; then killall glava; glava --desktop &>/dev/null & disown; fi", false)

local bling = require("extras.bling")

awful.layout.layouts = {
    awful.layout.suit.tile,
    awful.layout.suit.fair,
    awful.layout.suit.tile.bottom,
    bling.layout.centered,
    awful.layout.suit.spiral.dwindle,
    bling.layout.equalarea,
    awful.layout.suit.corner.nw,
    awful.layout.suit.floating
}
