import { getMotivationalMessage } from './message.js';

export let startTime = null;
let breakActiveUntil = 0; // bis wann Break-Text sichtbar bleibt (ms)

export function setBreakActiveUntil(timestamp) {
  breakActiveUntil = timestamp;
}

export function getBreakActiveUntil() {
  return breakActiveUntil;
}

export function loadStatus() {
  const stored = localStorage.getItem("victorytimer_start");
  startTime = stored ? new Date(stored) : new Date(Math.floor(Date.now() / 1000) * 1000);
  if (!stored) {
    localStorage.setItem("victorytimer_start", startTime.toISOString());
  }
}

export function updateTimer() {
  if (!startTime) return;
  const now = new Date();
  const diff = Math.floor((now - startTime) / 1000);

  const h = Math.floor(diff / 3600);
  const m = Math.floor((diff % 3600) / 60);
  const s = diff % 60;

  document.getElementById("timer").textContent = `${h}h ${m}m ${s}s`;

  const messageEl = document.getElementById("message");
  if (!messageEl) return;

  // Falls Break-Phase aktiv -> Text nicht überschreiben
  if (Date.now() < getBreakActiveUntil()) {
    return;
  }

  // Ansonsten normale Motivationsmeldung
  messageEl.textContent = getMotivationalMessage(diff);
}

