-- vim:fileencoding=utf-8:foldmethod=marker:foldmarker={{{,}}}

-- {{{ Libraries

local awful = require("awful")
local wibox = require("wibox")
local watch = require("awful.widget.watch")
local naughty = require("naughty")

-- }}}


-- {{{ Variables

local cpu_temp_widget = {}
-- uses for notify
local last_id

-- }}}


-- {{{ Worker function

local function worker(user_args)

	-- {{{ Variables
	
	local args = user_args or {}
	local widget_dir = os.getenv("HOME") .. '/.config/awesome/widgets/temperature/'

    local font = args.font or 'FontAwesome bold 9'
	local refresh_rate = args.refresh_rate or 5
	--local GET_TEMP_COMMAND = args.GET_TEMP_COMMAND or "sensors | awk '/^CPU/ {t=substr($2,2,4); print t}'"
	local GET_TEMP_COMMAND = "/bin/bash -c \"sensors | awk '/^CPU/ {t=substr($2,2,4); print t}'\""
	--naughty.notify({text = GET_TEMP_COMMAND})
	local low_temp_color = args.low_temp_color or "#669900"
	local medium_temp_color = args.medium_temp_color or "#F5F549"
	local high_temp_color = args.high_temp_color or "#FF8000"
	local critical_temp_color = args.critical_temp_color or "#FF0000"
	local medium_temp_value = args.medium_temp_value or 60.0
	local high_temp_value = args.high_temp_value or 70.0
	local critical_temp_value = args.critical_temp_value or 80.0
	local critical_temp_icon = args.critical_temp_icon or widget_dir.."hot.png"

	-- End variables}}}


	-- {{{ Work functions
	
--	function get_cpu_temp()
--		local cpu_temp = -1
--		awful.spawn.easy_async_with_shell(command, function (out)
--			cpu_temp = out
--		end)
--		return cpu_temp
--	end

-- End work functions }}}


	-- {{{ Widget settings
	cpu_temp_widget = wibox.widget {
		{
			id = 'tempw',
			font = font,
			widget = wibox.widget.textbox,
			text = "init text",
		},
		layout = wibox.layout.align.horizontal,
		set_temp = function (self, cpu_temp)
			local set_color = "<span color='"
			local output_temp
			--local color = "#ff44ff"
			if tonumber(cpu_temp) > tonumber(critical_temp_value) then
				set_color = set_color..critical_temp_color.."'>"
				last_id = naughty.notify ({
					title = "CPU temp is critical",
					text = "CPU temperature is dangerously hot, turn it off to prevent damage.",
					preset = naughty.config.presets.critical,
					replaces_id = last_id,
					icon = critical_temp_icon,
				}).id
				--cpu_temp = cpu_temp .. " "
				output_temp = cpu_temp:match('%d+.%d+') .. " "
				--color = critical_temp_color
			elseif tonumber(cpu_temp) > tonumber(high_temp_value) then
				set_color = set_color..high_temp_color.."'>"
				--cpu_temp = tostring(cpu_temp) .. " "
				output_temp = cpu_temp:match('(%d+.%d+)') .. " "
				--color = high_temp_color
			elseif tonumber(cpu_temp) > tonumber(medium_temp_value) then
				set_color = set_color..medium_temp_color.."'>"
				--cpu_temp = tostring(cpu_temp) .. " "
				output_temp = cpu_temp:match('(%d+.%d+)') .. " "
				--color = medium_temp_color
			else
				set_color = set_color..low_temp_color.."'>"
				output_temp = cpu_temp:match('(%d+.%d+)') .. " "
				--color = low_temp_color
			end
			self:get_children_by_id('tempw')[1]:set_markup(set_color..output_temp.."</span>")
			--self:get_children_by_id('tempw')[1]:set_markup("<span color='"..color.."'>"..cpu_temp.."</span>")
		end
	}
	-- End widget settings }}}


	local update_widget_text = function (widget, stdout, _, _, _)
		widget:set_temp(stdout)
	end

	watch(GET_TEMP_COMMAND, refresh_rate, update_widget_text, cpu_temp_widget)

	return cpu_temp_widget

end


return setmetatable(cpu_temp_widget, {
	__call = function (_, ...)
		return worker(...)
	end
})

-- End worker function}}}
