local awful = require("awful")
local wibox = require("wibox")
local beautiful = require("beautiful")
local vicious = require("vicious")
local naughty = require("naughty")

-- Spacers
volspace = wibox.widget.textbox()
volspace:set_text(" ")

-- {{{ BATTERY
-- Battery attributes
local bat_state  = ""
local bat_charge = 0
local bat_time   = 0
local blink      = true

-- Icon
baticon = wibox.widget.imagebox()
baticon:set_image(beautiful.widget_batfull)

-- Charge %
batpct = wibox.widget.textbox()
vicious.register(batpct, vicious.widgets.bat, function(widget, args)
    bat_state  = args[1]
    bat_charge = args[2]
    bat_time   = args[3]

    if args[1] == "-" then
        if bat_charge > 70 then
            baticon:set_image(beautiful.widget_batfull)
        elseif bat_charge > 30 then
            baticon:set_image(beautiful.widget_batmed)
        elseif bat_charge > 10 then
            baticon:set_image(beautiful.widget_batlow)
        else
            baticon:set_image(beautiful.widget_batempty)
        end
    else
        baticon:set_image(beautiful.widget_ac)
        if args[1] == "+" then
            blink = not blink
            if blink then
                baticon:set_image(beautiful.widget_acblink)
            end
        end
    end

    return args[2] .. "%"
end, nil, "BAT1")

-- Buttons
function popup_bat()
    local state = ""
    if bat_state == "↯" then
        state = "Full"
    elseif bat_state == "↯" then
        state = "Charged"
    elseif bat_state == "+" then
        state = "Charging"
    elseif bat_state == "-" then
        state = "Discharging"
    elseif bat_state == "⌁" then
        state = "Not charging"
    else
        state = "Unknown"
    end

    naughty.notify { text = "Charge : " .. bat_charge .. "%\nState  : " .. state ..
    " (" .. bat_time .. ")", timeout = 5, hover_timeout = 0.5 }
end
batpct:buttons(awful.util.table.join(awful.button({ }, 1, popup_bat)))
baticon:buttons(batpct:buttons())
-- End Battery}}}
--
-- {{{ PACMAN
-- Icon
pacicon = wibox.widget.imagebox()
pacicon:set_image(beautiful.widget_pac)
--
-- Upgrades
pacwidget = wibox.widget.textbox()
vicious.register(pacwidget, vicious.widgets.pkg, function(widget, args)
    if args[1] > 0 then
        pacicon:set_image(beautiful.widget_pacnew)
    else
        pacicon:set_image(beautiful.widget_pac)
    end

    return args[1]
end, 1801, "Arch S") -- Arch S for ignorepkg
--
-- Buttons
function popup_pac()
    local pac_updates = ""
    local f = io.popen("pacman -Sup --dbpath /tmp/pacsync")
    if f then
        pac_updates = f:read("*a"):match(".*/(.*)-.*\n$")
    end
    f:close()
    if not pac_updates then
        pac_updates = "System is up to date"
    end
    naughty.notify { text = pac_updates }
end
pacwidget:buttons(awful.util.table.join(awful.button({ }, 1, popup_pac)))
pacicon:buttons(pacwidget:buttons())
-- End Pacman }}}
--
-- {{{ VOLUME
-- Cache
vicious.cache(vicious.widgets.volume)
--
-- Icon
volicon = wibox.widget.imagebox()
volicon:set_image(beautiful.widget_vol)
--
-- Volume %
volpct = wibox.widget.textbox()
vicious.register(volpct, vicious.widgets.volume, "$1%", nil, "Master")
--
-- Buttons
volicon:buttons(awful.util.table.join(
awful.button({ }, 1,
function() awful.util.spawn_with_shell("pamixer -t") end),
awful.button({ }, 4,
function() awful.util.spawn_with_shell("pamixer -i 3") end),
awful.button({ }, 5,
function() awful.util.spawn_with_shell("pamixer -d 3") end)
))
volpct:buttons(volicon:buttons())
volspace:buttons(volicon:buttons())
-- End Volume }}}
--
-- {{{ Start CPU
cpuicon = wibox.widget.imagebox()
cpuicon:set_image(beautiful.widget_cpu)
--
cpu = wibox.widget.textbox()
vicious.register(cpu, vicious.widgets.cpu, "All: $1% 1: $2% 2: $3% 3: $4% 4: $5%", 2)
-- End CPU }}}
--
-- {{{ Start Mem
memicon = wibox.widget.imagebox()
memicon:set_image(beautiful.widget_ram)
--
mem = wibox.widget.textbox()
vicious.register(mem, vicious.widgets.mem, "Mem: $1% Use: $2MB Total: $3MB Free: $4MB Swap: $5%", 2)
-- End Mem }}}
--
-- {{{ Start Gmail
--mailicon = wibox.widget.imagebox(beautiful.widget_mail)
--mailwidget = wibox.widget.textbox()
--gmail_t = awful.tooltip({ objects = { mailwidget },})
--vicious.register(mailwidget, vicious.widgets.gmail,
--        function (widget, args)
--        gmail_t:set_text(args["{subject}"])
--        gmail_t:add_to_object(mailicon)
--            return args["{count}"]
--                 end, 120)
--
--     mailicon:buttons(awful.util.table.join(
--         awful.button({ }, 1, function () awful.util.spawn("urxvt -e mutt", false) end)
--     ))
-- End Gmail }}}
--
-- {{{ Start Wifi
wifiicon = wibox.widget.imagebox()
wifiicon:set_image(beautiful.widget_wifi)
--
wifi = wibox.widget.textbox()
vicious.register(wifi, vicious.widgets.wifi, "${ssid} Rate: ${rate}MB/s Link: ${link}%", 3, "wlp3s0")
-- End Wifi }}}
