local gputemp = require("gputemp")
local hubic = require("hubic")
local dpmsstatus = require("dpmsstatus")
local lain = require("lain")
local vicious = require("vicious") -- Custom
local beautiful = require("beautiful") -- CPU temp
local wibox = require("wibox")
local gears = require("gears")

-- Variables
local valuecolor = "white"

icons_dir = os.getenv("HOME") .. "/.config/awesome/themes/lanxu/icons/"
cpuicon = wibox.widget.imagebox()
cpuicon.image  = icons_dir .. "cpu.png"
cpuicon.resize = false

-- Helpers
function file_exists(name)
  local f = io.open(name,"r")
  if f ~= nil then io.close(f) return true else return false end
end

function create_markup(key, value, postfix, color)
    return key .. " <span foreground='" .. color .. "'><b>" .. value .. postfix .."</b></span>"
end

-- Spacer
spacer = wibox.widget.textbox()
--spacer.text = "-"
spacer.forced_width = 8;

-- CPU
mycpu = lain.widget.cpu({
  settings = function()
    widget.markup = create_markup("CPU", cpu_now.usage, " %", valuecolor)
  end
})

-- MEM
mymem  = lain.widget.mem({
  settings = function()
    widget.markup = create_markup("MEM", mem_now.used, " MB", valuecolor)
  end
})

-- GPU
mygputemp_text = wibox.widget.textbox()
mygputemp_text.align = "center"
vicious.register(mygputemp_text, gputemp, 
function(widget, args)
  temp = string.format("%.0f", tonumber(args[0]))
  return create_markup("", temp, "℃", valuecolor)
end, 15, "AMD")
mygputemp = wibox.container.background()
mygputemp.forced_width = 70
mygputemp:set_widget(mygputemp_text)
--mytextclock:set_shape(gears.shape.hexagon)
mygputemp:set_bg('#5e35b1')

-- Hubic
myhubic = wibox.widget.textbox()
vicious.register(myhubic, hubic, create_markup("CLOUD", "$0", "", valuecolor), 15, "HUBIC")

-- Volume
volume = lain.widget.pulse({
  scallback = function()
    return "pacmd list-sinks | grep -Paoz \"(?s)(\\* index.*$(pactl info | grep -e 'ink' | cut -d' ' -f3).*(index)?)\" | tr -d '\\000' | grep -e 'index' -e 'device.string' -e 'name:' -e 'volume: front' -e 'muted'"
  end,
  settings = function()
    volstr = volume_now.left .. " "
    volcolor = valuecolor
    if volume_now.muted == "yes" then
      volcolor = "red"
    end
    widget.markup = create_markup("VOL", volstr, "%", volcolor)
  end
})

-- Text clock
mytextclock_text = wibox.widget.textclock(create_markup("","%Y-%m-%d %H:%M","", valuecolor))
mytextclock_text.align = "center"
lain.widget.cal({
  followtag = true,
  attach_to = {mytextclock_text},
  notification_preset = {
    font = "Monospace 11", fg = "#FFFFFF", bg = "#000000"
  }
})

mytextclock = wibox.container.background()
mytextclock.forced_width = 130
mytextclock:set_widget(mytextclock_text)
mytextclock:set_bg('#512da8')

-- CPU temperature
local tempfile = "/sys/devices/pci0000:00/0000:00:18.3/hwmon/hwmon1/temp1_input"
if file_exists(tempfile) == false then
  tempfile = "/sys/class/thermal/thermal_zone0/temp"
end

mycputemp_text = lain.widget.temp({
  tempfile = tempfile, 
  settings = function()
    temp = string.format("%.0f", tonumber(coretemp_now))
    widget.markup = create_markup("", temp, "℃", valuecolor)
  end
})

mycputemp_text.widget.align = "center"
mycputemp = wibox.container.background()
mycputemp.forced_width = 70
mycputemp:set_widget(mycputemp_text.widget)
mycputemp:set_bg('#9575cd')
-- Weather Widget
myweather = lain.widget.weather({
  city_id = 634963,
  lang = "fi",
  followtag = true,
  settings = function()
    widget.markup = create_markup("Tampere", weather_now.main.temp, "℃", valuecolor)
  end,
  notification_preset = {
    font = "Monospace 11", fg = "#FFFFFF", bg = "#000000"
  }
})

-- DPMS
mydpmsstatus = wibox.widget.textbox()
-- mydpmsstatus.container.background.bg = '#ff0000'
--mydpmsstatus = {
--  widget = wibox.widget.textbox
--}

vicious.register(mydpmsstatus, dpmsstatus, function(widget, args)
  color = valuecolor
  if args[0] == "Enabled" then
    return create_markup("DPMS", "ON", "", color)
  else
    color = "red"
    return create_markup("DPMS", "OFF", "", color)
  end
end)

local notification = {
  font = "Monospace 10"
}
-- Servers
--local mcstatus = require("lanxu/mcstatus")
--myserverstatus = mcstatus({
  --notification_preset = notification,
  --settings = function()
    --widget.markup = create_markup("PLAYERS", totalplayers, "", valuecolor)
  --end
--})

--local cloudstatus = require("lanxu/cloudstatus")
--mycloudstatus = cloudstatus({
  --notification_preset = notification,
  --settings = function()
    --widget.markup = create_markup("CLOUD", currentstatus, "", valuecolor)
  --end
--})

local ip = require("lanxu/ip")
ipaddress = ip({
  homeaddress = "91.157.104.247",
  settings = function()
    widget.markup = create_markup("IP", address, "", valuecolor)
  end
})

