--      ████████╗ ██████╗ ██████╗     ██████╗  █████╗ ███╗   ██╗███████╗██╗
--      ╚══██╔══╝██╔═══██╗██╔══██╗    ██╔══██╗██╔══██╗████╗  ██║██╔════╝██║
--         ██║   ██║   ██║██████╔╝    ██████╔╝███████║██╔██╗ ██║█████╗  ██║
--         ██║   ██║   ██║██╔═══╝     ██╔═══╝ ██╔══██║██║╚██╗██║██╔══╝  ██║
--         ██║   ╚██████╔╝██║         ██║     ██║  ██║██║ ╚████║███████╗███████╗
--         ╚═╝    ╚═════╝ ╚═╝         ╚═╝     ╚═╝  ╚═╝╚═╝  ╚═══╝╚══════╝╚══════╝

-- ===================================================================
-- Initialization
-- ===================================================================


local awful = require("awful")
local beautiful = require("beautiful")
local wibox = require("wibox")
local theme = require("theme")
local lain = require("lain")
local tag_list = require("widgets.tag-list")
local battery_widget = require("widgets.battery-widget.battery")
local volume_widget = require("widgets.volume-widget.volume")
local pl = require("powerline")

-- import widgets
local task_list = require("widgets.task-list")



-- ===================================================================
-- Custom Widgets
-- ===================================================================

local top_panel = {}

local updates_indicator = wibox.widget{
    {
        layout = wibox.layout.fixed.horizontal,
        {
            {
                widget = awful.widget.watch('bash -c "checkupdates | wc -l"', 360),
            },
            top = 0, bottom = 2, left = 0, right = 0,
            widget = wibox.container.margin
        },
        {
            {
                image = "/usr/share/icons/Papirus/48x48/apps/org.kde.archUpdate.svg",
                forced_width = 18,
                widget = wibox.widget.imagebox,
            },
            top = 5, bottom = 0, left = 5, right = 0,
            widget = wibox.container.margin
        }
    },
    widget = wibox.container.background
}

local frequency_widget = wibox.widget{
    layout = wibox.layout.fixed.horizontal,
    awful.widget.watch('bash -c "/usr/bin/cat /proc/cpuinfo | grep MHz | cut -b 12- | sort -r | head -n 1 | xargs printf \'%f / 1000\n\' | bc -l | awk \'{printf \\"%.2f\\n\\", $0}\'"', 1),
    wibox.widget.textbox('GHz')
}

local memory_widget = wibox.widget{
    layout = wibox.layout.fixed.horizontal,
    lain.widget.mem({
        settings = function()
            widget:set_markup(lain.util.markup.font(theme.font, mem_now.used .. " MB"))
        end,
    }),
}

local cpu_widget = wibox.widget{
    layout = wibox.layout.fixed.horizontal,
    lain.widget.cpu({
        settings = function()
            widget:set_markup(lain.util.markup.font(theme.font, "CPU:  " .. cpu_now.usage .. "%"))
        end,
    })
}

-- ===================================================================
-- Bar Creation
-- ===================================================================

top_panel.create = function(s)
    local panel = awful.wibar({
        screen = s,
        position = "top",
        ontop = true,
        height = beautiful.top_panel_height,
        width = s.geometry.width,
        bg = theme.bg_normal .. "99"
    })

    panel:setup {
        expand = "none",
        layout = wibox.layout.align.horizontal,
        task_list.create(s),
        tag_list.create(s),
        {
            layout = wibox.layout.fixed.horizontal,
            pl(wibox.widget{}, theme.bg_normal .. "00", theme.top_panel_powerline),
            pl(wibox.container.margin(wibox.widget.systray(), 0, 0, 5, 5), theme.top_panel_powerline, theme.bg_normal),
            pl(volume_widget({widget_type='icon_and_text'}), theme.bg_normal, theme.top_panel_powerline),
            pl(awful.widget.watch('bash -c "sensors | grep Tctl | cut -f 10 -d \' \' | cut -c 2-"', 5), theme.top_panel_powerline, theme.bg_normal),
            pl(memory_widget, theme.bg_normal, theme.top_panel_powerline),
            pl(cpu_widget, theme.top_panel_powerline,  theme.bg_normal),
            pl(frequency_widget, theme.bg_normal, theme.top_panel_powerline),
            pl(updates_indicator, theme.top_panel_powerline, theme.bg_normal),
            pl(require("widgets.layout-box"), theme.bg_normal, theme.top_panel_powerline),
            pl(wibox.widget.textclock('%a %b %d, %H:%M:%S', 1), theme.top_panel_powerline, theme.top_panel_powerline),
        }
    }


    -- ===================================================================
    -- Functionality
    -- ===================================================================


    -- hide panel when client is fullscreen
    local function change_panel_visibility(client)
        if client.screen == s then
            panel.ontop = not client.fullscreen
        end
    end

    -- connect panel visibility function to relevant signals
    client.connect_signal("property::fullscreen", change_panel_visibility)
    client.connect_signal("focus", change_panel_visibility)

    updates_indicator:connect_signal("button::press", function(c, _, _, button)
        awful.spawn.with_shell('WINIT_X11_SCALE_FACTOR=1 alacritty -e bash -c "yay ; echo \'\nDone, press any key to exit...\' ; read"')
    end)

end

return top_panel