--@name Session timer
--@version 1.0.0
--@author wolfhack docs
--@api 1.0
--@description A draggable widget with the clock, the time since the script started, and the time spent in a match. The total across sessions is saved.
-- No permissions needed. Drag the widget by its top strip while the menu is open.

local W, H = 176, 64

local session_start = time.now()   -- the session "starts" when the script loads (or when you press Reset on the tab)
local match_total = 0              -- seconds spent in matches this session
local match_since = nil            -- time.now() when the current match began
local lifetime = store.get("lifetime", 0)   -- seconds across all sessions, saved
local last_save = time.now()

local function fmt(sec)
  sec = math.max(0, math.floor(sec))
  local h, m, s = sec // 3600, (sec % 3600) // 60, sec % 60
  if h > 0 then return string.format("%d:%02d:%02d", h, m, s) end
  return string.format("%02d:%02d", m, s)
end

local function match_seconds()
  return match_total + (match_since and (time.now() - match_since) or 0)
end

-- The ingame event fires when a match starts or ends, not at load, so the first frame reads the current state once.
events.once("frame", function()
  if data.snapshot().in_game then match_since = time.now() end
end)

events.on("ingame", function(inside)
  if inside then
    match_since = time.now()
  elseif match_since then
    match_total = match_total + (time.now() - match_since)
    match_since = nil
  end
end)

-- Save the running total now and then, and when the script goes away.
local function save()
  lifetime = lifetime + (time.now() - last_save)
  last_save = time.now()
  store.set("lifetime", lifetime)
end
timer.every(30, save)
events.on("unload", save)

ui.widget{
  id = "timer", name = "Session timer", size = { W, H }, pos = { 0.02, 0.4 },
  draw = function(w)
    draw.rect(0, 0, w.w, w.h, color.alpha(0x101418FF, store.get("opacity", 0.86)), 6)
    draw.rect(0, 6, 3, w.h - 12, store.get("accent", 0x750851FF))
    local pattern = store.get("h24", true) and "%H:%M:%S" or "%I:%M:%S %p"
    draw.text(14, 10, 0xF0F0F0FF, os.date(pattern, time.wall()), { size = 18 })
    draw.text(14, 34, 0xA8B0B8FF, "session   " .. fmt(time.now() - session_start), { size = 12 })
    draw.text(14, 48, 0xA8B0B8FF, "in match  " .. fmt(match_seconds()), { size = 12 })
  end,
}

ui.tab("Session timer", function()
  ui.card("Widget", function()
    ui.toggle("h24", "24-hour clock", true)
    ui.slider("opacity", "Background opacity", 0, 1, 0.86)
    ui.color("accent", "Accent line", 0x750851FF)
  end, { width = 0.5 })

  ui.card("Time", function()
    ui.note("Total time with this script on: " .. fmt(lifetime + (time.now() - last_save)))
    if ui.button("Reset this session") then
      session_start = time.now()
      match_total = 0
      if match_since then match_since = time.now() end
    end
    if ui.button("Clear the saved total", { variant = "danger" }) then
      lifetime = 0
      last_save = time.now()
      store.set("lifetime", 0)
    end
  end, { width = 0.5 })
end)
