import type { EditorWorkKind } from "./model/editorTypes";

export const EDITOR_MONTHS = ["янв", "фев", "мар", "апр", "май", "июн", "июл", "авг", "сен", "окт", "ноя", "дек"];

export const DEFAULT_EDITOR_MODES = [
  { id: "pan", label: "Панхром." },
  { id: "ms", label: "Мультиспектр." },
  { id: "stereo", label: "Стерео" },
  { id: "scan", label: "Скан" }
];

export function kindLabel(kind: EditorWorkKind): string {
  switch (kind) {
    case "shoot":
      return "Съёмка";
    case "downlink":
      return "Сброс";
  }
}

export function kindColor(kind: EditorWorkKind): string {
  return kind === "shoot" ? "var(--t-optical)" : "var(--accent)";
}

export function formatTime(ms: number): string {
  const date = new Date(ms);
  return `${pad(date.getUTCHours())}:${pad(date.getUTCMinutes())}`;
}

export function formatDate(ms: number): string {
  const date = new Date(ms);
  return `${pad(date.getUTCDate())} ${EDITOR_MONTHS[date.getUTCMonth()]}`;
}

export function formatDateTime(ms: number): string {
  return `${formatDate(ms)} ${formatTime(ms)}`;
}

export function formatDuration(ms: number): string {
  const hours = ms / 3_600_000;
  if (hours < 24) {
    return `${Number.isInteger(hours) ? hours : hours.toFixed(1)} ч`;
  }

  const days = Math.floor(hours / 24);
  const restHours = Math.round(hours - days * 24);
  return restHours > 0 ? `${days} сут ${restHours} ч` : `${days} сут`;
}

export function niceTicks(fromMs: number, toMs: number, pxPerMs: number, minPx: number): number[] {
  const hourMs = 3_600_000;
  const dayMs = 24 * hourMs;
  const steps = [hourMs, 2 * hourMs, 3 * hourMs, 6 * hourMs, 12 * hourMs, dayMs, 2 * dayMs, 7 * dayMs];
  const step = steps.find((item) => item * pxPerMs >= minPx) ?? steps[steps.length - 1];
  const ticks: number[] = [];
  const firstTick = Math.ceil(fromMs / step) * step;

  for (let timeMs = firstTick; timeMs <= toMs; timeMs += step) {
    ticks.push(timeMs);
  }

  return ticks;
}

function pad(value: number): string {
  return String(value).padStart(2, "0");
}
