local theme = require('theme.base_theme')
local lain = require('lain')
local wibox = require('wibox')
local awful = require('awful')

local volumeicon = wibox.widget.imagebox(nil)
local volume = lain.widget.alsa({
	settings = function()
		-- luacheck: globals widget volume_now
		if volume_now.status == 'off' then
			volumeicon:set_image(theme.widget_vol_mute)
			volume_now.level = 'M ' .. volume_now.level
		elseif volume_now.level == 0 then
			volumeicon:set_image(theme.widget_vol_mute)
		elseif volume_now.level <= 50 then
			volumeicon:set_image(theme.widget_vol_low)
		else
			volumeicon:set_image(theme.widget_vol_high)
		end

		widget:set_text(' ' .. volume_now.level .. '%')
	end,
})

local volumebuttons = awful.util.table.join(
	awful.button({}, 1, function() -- left click
		os.execute(string.format('%s set %s toggle', volume.cmd, volume.togglechannel or volume.channel))
		volume.update()
	end),
	awful.button({}, 4, function()
		os.execute(string.format('%s set %s 1%%+', volume.cmd, volume.channel))
		volume.update()
	end),
	awful.button({}, 5, function()
		os.execute(string.format('%s set %s 1%%-', volume.cmd, volume.channel))
		volume.update()
	end)
)

volume.widget:buttons(volumebuttons)
volumeicon:buttons(volumebuttons)

return { {
	widget = wibox.layout.fixed.horizontal(volumeicon, volume.widget),
	bg = '#7493d2',
} }
