--[[
  @package    eg4-crank-ui-2
  @file       machine-stats.lua
  @brief      Collects information about the local machine. Primarily used to populate controls on the UI.
]]

---
--- @brief  Fetch a string indicating the size that is scaled.
--- @param  size_in_kb - value to scale in KB
--- @return string scaled and appended with KB, MB, GB or TB
local function scale_value(size_in_kb)
  size_in_kb = tonumber(size_in_kb) or 0
  local storage_scale
  if size_in_kb > 999 then
    if size_in_kb > 999 * 1024 then
      if size_in_kb > 999 * 1024 * 1024 then
        size_in_kb = size_in_kb / (1024 * 1024 * 1024)
        storage_scale = "TB"
      else
        size_in_kb = size_in_kb / (1024 * 1024)
        storage_scale = "GB"
      end
    else
      size_in_kb = size_in_kb / 1024
      storage_scale = "MB"
    end
  else
    storage_scale = "KB"
  end
  return string.format("%.1f%s", size_in_kb, storage_scale)
end

---
--- @brief  Data source for the memory usage temperature dial
--- @return Percent used, used string, total string
function ds_memory_usage(control)
  local total, free = sbsettings.get_sysinfo_memory()
  local used = total - free
  return {
    value = math.floor((used / total) * 100 + 0.5) / 100,
    used = scale_value(used),
    total = string.format(gre.get_value(control..".total_format"), scale_value(total))
  }
end

---
--- @brief  Data source for the disk usage temperature dial
--- @return Percent used, used string, total string
function ds_disk_usage(control)
  local total, used = sbsettings.get_sysinfo_storage()
  return {
    value = math.floor((used / total) * 100 + 0.5) / 100,
    used = scale_value(used),
    total = string.format(gre.get_value(control..".total_format"), scale_value(total))
  }
end

---
--- @brief  Data source for the CPU temperature dial
--- @return Percent used, No. of cores, blank
function ds_cpu_load(control)
  local data = sbsettings.get_sysinfo_cpu_usage()
  return {
    value = data[1],
    used = string.format(gre.get_value(control..".used_format"), #data - 1),
    total = ""
  }
end

---
--- @brief  Data source for the CPU up time
--- @return Percent used, No. of cores, blank
function ds_up_time(control)
  local v = sbsettings.get_sysinfo_uptime()
  local h = math.floor(v / 3600)
  local m = math.floor((v % 3600) / 60)
  local s = v % 60
  return { value = string.format('%d:%02d:%02d', h, m, s) }
end

---
--- @brief Data source for the CPU temperature dial
--- @return temperature in degrees celsius
local cpu_temp_file_path
function ds_cpu_temp(control)
  if not cpu_temp_file_path then
    local folder_list = fileinfo:execute("ls /sys/class/thermal/ | grep thermal_zone")
    if #folder_list == 1 then
      cpu_temp_file_path = "/sys/class/thermal/"..folder_list[1].."/temp"
    else
      for _,folder in ipairs(folder_list) do
        local folder = "/sys/class/thermal/"..folder
        local file = io.open(folder.."/type", "r")
        local type = file:read("*l")
        file:close()
        if type == "x86_pkg_temp" or type == "imx_thermal_zone" then
          cpu_temp_file_path = folder.."/temp"
          break
        end
      end
    end
  end
  if cpu_temp_file_path then
    local file = io.open(cpu_temp_file_path, "r")
    if file then
      local temp = math.floor(file:read("*n") / 1000 + 0.5)
      file:close()
      return { value = gre.get_value(control..".format"):format(temp) }
    end
  end
  return { value = "---" }
end
