local beautiful = require("beautiful")
local awful = require("awful")
local gears = require("gears")
local wibox = require("wibox")
-- {{{ Wibar

-- Create a wibox for each screen and add it

local function set_wallpaper(s)
    -- Wallpaper
    if beautiful.wallpaper then
        local wallpaper = beautiful.wallpaper
        -- If wallpaper is a function, call it with the screen
        if type(wallpaper) == "function" then
            wallpaper = wallpaper(s)
        end
        gears.wallpaper.maximized(wallpaper, s, true)
    end
end

-- Re-set wallpaper when a screen's geometry changes (e.g. different resolution)
screen.connect_signal("property::geometry", set_wallpaper)

-- {{{ Non-widget modules
-- The following modules are not exactly widgets, however they are part of the ui.

-- Load the keybindings list
require("core.widgets.hotkeys_popup")

-- Load the menubar
require("core.widgets.menubar")
-- }}}

awful.screen.connect_for_each_screen(function(s)
    -- Wallpaper
    set_wallpaper(s)

    -- Each screen has its own tag table.
    awful.tag({ "1", "2", "3", "4", "5", "6", "7", "8", "9" }, s, awful.layout.layouts[1])

    -- Create a promptbox for each screen
    s.mypromptbox = require("core.widgets.prompt")()
    -- Create an imagebox widget which will contain an icon indicating which layout we're using.
    -- Create the taglist
    s.mytaglist = require("core.widgets.taglist")(s)
    -- We need one layoutbox per screen.
    s.mylayoutbox = require("core.widgets.layout_box")(s)
    -- Create a tasklist widget
    s.mytasklist = require("core.widgets.tasklist")(s)
    -- Create the wibox
    s.mywibox = awful.wibar({
        position = "top",
        screen = s,
        bg = beautiful.topbar_bg,
    })
    -- Add screen lock
    require("widgets.lock")({screen = s, obscure = true})
    require("widgets.unitybar")(s,{
        top_widgets = {
            require("widgets.polytasklist")({
                vertical = true,
                stretch = false,
                --constraint = 180,
                names = false,
                margins = 5
            })(s),
        },
        bottom_widgets = {
            require("widgets.polylauncher")({vertical = true})
        },
        position="left"
    })
    -- Menu
    require("core.widgets.menu")
    -- Start button
    local start_button = wibox.widget {
        {
            {
                image = beautiful.awesome_icon,
                widget = wibox.widget.imagebox
            },
            widget = wibox.container.place,
            halign = "center",
            id = "menu"
        },
        bg = "#26262633",
        widget = wibox.container.background,
        forced_width = require("beautiful.xresources").apply_dpi(61),
    }
    start_button:connect_signal("button::press",function(self,x,y,button)
        if button == 1 then
            menu:show()
        end
    end)
    -- Add widgets to the wibox
    s.mywibox:setup {
        expand = "outside",
        layout = wibox.layout.align.horizontal,
        { -- Left widgets
            start_button,
            layout = wibox.layout.fixed.horizontal,
            --require("widgets.polylauncher")({vertical = false}),
            --s.mytaglist,
            s.mypromptbox,
            spacing = 3
        },
        {
            layout = wibox.layout.fixed.horizontal
        },
        { -- Right widgets
            {
                layout = wibox.layout.fixed.horizontal,
                require("widgets.wallpapers")({
                    screen = s,
                    path = os.getenv("HOME").."/Pictures/Wallpapers/",
                }),
                require("widgets.mailbox")({
                    screen = s,
                    style = {
                        rounding = 1,
                    }
                }),
                require("widgets.volume")({}),
                require("widgets.battery")({
                    percentage = true,
                }),
                --wibox.widget.systray(),
                awful.widget.keyboardlayout(),
                wibox.widget.textclock(),
                s.mylayoutbox,
                spacing = 4,
            },
            widget = wibox.container.place,
            halign = "right",
            fill_horizontal = false
        },
    }
end)
-- }}}
