/**
 * Dag/Nacht panel chrome — theme op login + alle pagina's.
 */
var PanelModeUI = (function () {
  "use strict";

  function titleClass(sleep) {
    return (
      "text-3xl font-bold text-white tracking-wider brand panel-main-title uppercase " +
      (sleep ? "glow-text-purple" : "glow-cyan")
    );
  }

  function subtitleClass(sleep) {
    return (
      "text-sm mt-2 font-mono panel-main-subtitle " +
      (sleep ? "text-violet-300/70" : "text-gray-400")
    );
  }

  function apply(mode) {
    var sleep = mode === "sleep";
    document.body.classList.toggle("panel-mode-sleep", sleep);
    document.body.classList.toggle("panel-mode-awake", !sleep);

    var label = document.getElementById("system-status-label");
    var ping = document.getElementById("system-status-ping");
    var dot = document.getElementById("system-status-dot");

    if (label) {
      label.textContent = sleep ? "NACHTMODUS" : "ONLINE";
      label.className =
        "font-mono tracking-wider text-xs " +
        (sleep ? "glow-text-purple" : "glow-text-green");
    }
    if (ping) {
      ping.style.backgroundColor = sleep ? "var(--accent-neon)" : "var(--atomic-green)";
    }
    if (dot) {
      dot.style.backgroundColor = sleep ? "var(--accent-neon)" : "var(--atomic-green)";
      dot.className =
        "relative inline-flex rounded-full h-2 w-2 " +
        (sleep ? "glow-purple" : "glow-green");
    }

    document.querySelectorAll(".panel-main-title").forEach(function (el) {
      el.className = titleClass(sleep);
    });
    document.querySelectorAll(".panel-main-subtitle").forEach(function (el) {
      el.className = subtitleClass(sleep);
    });

    if (typeof window.VisitorsPage !== "undefined" && VisitorsPage.refreshFilterUI) {
      VisitorsPage.refreshFilterUI();
    }
  }

  function load() {
    return fetch("/api/panel-mode", { credentials: "same-origin" })
      .then(function (res) {
        return res.json();
      })
      .then(function (data) {
        apply(data.mode === "sleep" ? "sleep" : "awake");
        return data.mode;
      })
      .catch(function () {
        apply("awake");
        return "awake";
      });
  }

  return { apply: apply, load: load };
})();
