local beautiful = require("beautiful")
local gears = require("gears")
local naughty = require("naughty")
local wibox = require("wibox")

local config = require("config")

local inspect = require("simple/debug/inspect")

local bar = {
    {
        {
            max_value = 1,
            value = 0,
            widget = wibox.widget.progressbar,
            color = beautiful.system_resources_widget_bar_color,
            background_color = beautiful.system_resources_widget_bar_bg,
            shape = beautiful.system_resources_widget_bar_shape,
            border_color = beautiful.system_resources_widget_border_color,
            border_width = beautiful.system_resources_widget_border_width,
        },
        forced_width = beautiful.system_resources_widget_bar_width,
        direction = "east",
        layout = wibox.container.rotate,
    },
    margins = beautiful.system_resources_widget_bar_margin,
    widget = wibox.container.margin,
}

local widget = wibox.widget {
    bar,
    bar,
    bar,
    layout = wibox.layout.flex.horizontal
}


function readCommand(command)
    local handle = io.popen(command, "r")
    local r = handle:read("*a")
    handle:close()
    return r
end


local cpuBar = widget:get_children()[1]:get_children()[1]:get_children()[1]
function getCpuData()
    local raw = readCommand("cat /proc/stat | head -n 1")
    local data = {raw:match((raw:gsub("[^ ]* ", "([^ ]*) ")))}
    return {
        user = data[3],
        nice = data[4],
        system = data[5],
        idle = data[6],
        iowait = data[7],
        irq = data[8],
        softirq = data[9],
        steal = data[10],
    }
end

local lastCpuTime = -1
local lastCpuIdle = -1

gears.timer {
    timeout = config.system_resources_widget_refresh_interval,
    call_now = true,
    autostart = true,
    callback = function()

        local data = getCpuData()

        local currentCpuTime = data.user + data.nice + data.system + data.idle + data.iowait + data.irq + data.softirq + data.steal
        local currentCpuIdle = data.idle + data.iowait

        if(lastCpuTime >= 0) then
            cpuBar.value = 1 - (currentCpuIdle - lastCpuIdle) / (currentCpuTime - lastCpuTime)
        end

        lastCpuTime = currentCpuTime
        lastCpuIdle = currentCpuIdle
    end
}


local memoryBar = widget:get_children()[2]:get_children()[1]:get_children()[1]
function getTotalMemory()
    return readCommand("cat /proc/meminfo | grep MemTotal | awk '{print $2}'")
end
function getAvailableMemory()
    return readCommand("cat /proc/meminfo | grep MemAvailable | awk '{print $2}'")
end
function getFreeMemory()
    return readCommand("cat /proc/meminfo | grep MemFree | awk '{print $2}'")
end

gears.timer {
    timeout = config.system_resources_widget_refresh_interval,
    call_now = true,
    autostart = true,
    callback = function()
        memoryBar.value = 1 - getAvailableMemory() / getTotalMemory()
    end
}


local swapBar = widget:get_children()[3]:get_children()[1]:get_children()[1]
function getTotalSwap()
    return readCommand("cat /proc/meminfo | grep SwapTotal | awk '{print $2}'")
end
function getFreeSwap()
    return readCommand("cat /proc/meminfo | grep SwapFree | awk '{print $2}'")
end

gears.timer {
    timeout = config.system_resources_widget_refresh_interval,
    call_now = true,
    autostart = true,
    callback = function()
        swapBar.value = 1 - getFreeSwap() / getTotalSwap()
    end
}



return {
    widget = widget
}