/* ───────────────────────────────────────────────────────────────────────────
   Wivexx timer-completion alert — a centered, always-on-top popup shown when a
   timer finishes (Python beeps until dismissed). Python pushes the fired timers
   via `window.wivexxTimerAlert.setAlert`; dismiss clears them all.
   ─────────────────────────────────────────────────────────────────────────── */
(function () {
  "use strict";
  let _pyReady = false;
  const _queue = [];
  function py(method) {
    const args = Array.prototype.slice.call(arguments, 1);
    const run = () => { try { return window.pywebview.api[method].apply(null, args); } catch (e) {} };
    if (_pyReady && window.pywebview && window.pywebview.api) return run();
    _queue.push(run);
  }
  function flush() { _pyReady = true; while (_queue.length) { try { _queue.shift()(); } catch (e) {} } }

  function fmtDur(sec) {
    sec = Math.max(0, Math.round(sec || 0));
    const h = Math.floor(sec / 3600), m = Math.floor((sec % 3600) / 60), s = sec % 60;
    const parts = [];
    if (h) parts.push(h + "h");
    if (m) parts.push(m + "m");
    if (s || !parts.length) parts.push(s + "s");
    return parts.join(" ");
  }

  function render(list) {
    list = list || [];
    const msg = document.getElementById("msg");
    const sub = document.getElementById("sub");
    const lbl = document.getElementById("lbl");
    if (lbl) lbl.textContent = (list.length && list.every((t) => t.kind === "goal")) ? "REMINDER" : "TIMER";
    if (!list.length) { msg.textContent = "Your timer has finished, sir."; sub.textContent = ""; return; }
    if (list.length === 1) {
      const t = list[0];
      if (t.kind === "goal") {
        msg.textContent = "Goal reminder, sir.";
        sub.textContent = (t.label || "").trim();
      } else {
        msg.textContent = "Your " + fmtDur(t.seconds) + " timer has finished, sir.";
        const lbl = (t.label || "").trim();
        sub.textContent = (lbl && lbl !== fmtDur(t.seconds)) ? lbl : "";
      }
    } else {
      const anyGoal = list.some((t) => t.kind === "goal");
      const anyTimer = list.some((t) => t.kind !== "goal");
      msg.textContent = list.length + (anyGoal && !anyTimer ? " goal reminders, sir."
        : anyTimer && !anyGoal ? " timers have finished, sir." : " reminders, sir.");
      sub.textContent = list.map((t) => t.label || fmtDur(t.seconds)).join("  ·  ");
    }
  }

  window.wivexxTimerAlert = { setAlert: render };

  function wireDrag() {
    let dx = 0, dy = 0, dragging = false;
    document.body.addEventListener("mousedown", (e) => {
      if (e.button !== 0 || e.target.id === "dismiss") return;
      dx = e.screenX; dy = e.screenY; dragging = false; e.preventDefault();
    });
    document.addEventListener("mousemove", (e) => {
      if (e.buttons === 0 || dragging) return;
      if (Math.abs(e.screenX - dx) > 4 || Math.abs(e.screenY - dy) > 4) { dragging = true; py("start_drag"); }
    });
    document.addEventListener("mouseup", () => { dragging = false; });
  }

  function boot() {
    document.getElementById("dismiss").addEventListener("click", () => py("dismiss"));
    document.addEventListener("keydown", (e) => { if (e.key === "Enter" || e.key === "Escape") py("dismiss"); });
    wireDrag();
    py("ready");
  }
  if (window.pywebview && window.pywebview.api) { _pyReady = true; }
  window.addEventListener("pywebviewready", flush);
  if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", boot);
  else boot();
})();
