import { increlogLogoSvgString } from "@/components/IncrelogLogo";

/* Shared plumbing for every printable document (certificates/admit cards/
   ID cards/marksheets/receipts on both the portal and the member side).
   Keeping the builders in one place guarantees the member-facing prints are
   pixel-identical to what the institution portal produces. */

export const DEFAULT_PRIMARY = "#24695c"; // teal
export const DEFAULT_ACCENT = "#c9a227"; // gold

/** preset combinations shown as split swatches under the tabs */
export const COLOR_PRESETS: { primary: string; accent: string }[] = [
  { primary: "#24695c", accent: "#c9a227" }, // teal & gold
  { primary: "#1e5a8a", accent: "#e39a1e" }, // blue & amber
  { primary: "#7a1f2b", accent: "#c9a227" }, // maroon & gold
  { primary: "#2e6b3e", accent: "#b87333" }, // green & copper
  { primary: "#3f3d8a", accent: "#b0446e" }, // indigo & rose
];

export function hexToRgb(hex: string): [number, number, number] {
  const h = hex.replace("#", "");
  const v = h.length === 3 ? h.split("").map((c) => c + c).join("") : h;
  return [parseInt(v.slice(0, 2), 16), parseInt(v.slice(2, 4), 16), parseInt(v.slice(4, 6), 16)];
}

/** blend color `a` toward `b`; `amount` is the portion of `b` (0–1) */
export function mix(a: string, b: string, amount: number): string {
  const [r1, g1, b1] = hexToRgb(a);
  const [r2, g2, b2] = hexToRgb(b);
  const f = (x: number, y: number) => Math.max(0, Math.min(255, Math.round(x + (y - x) * amount)));
  return "#" + [f(r1, r2), f(g1, g2), f(b1, b2)].map((x) => x.toString(16).padStart(2, "0")).join("");
}

/** heading text / light band background shades derived from the theme */
export const themeShades = (primary: string, accent: string) => ({
  dark: mix(primary, "#000000", 0.34),
  tint: mix(primary, "#ffffff", 0.88),
  accentDark: mix(accent, "#000000", 0.42),
  accentTint: mix(accent, "#ffffff", 0.88),
});

/** basic HTML escaping for admin-entered names */
export function esc(v: string | null | undefined): string {
  return String(v ?? "")
    .replace(/&/g, "&amp;")
    .replace(/</g, "&lt;")
    .replace(/>/g, "&gt;")
    .replace(/"/g, "&quot;");
}

/** institution logo chip, sized to match the surrounding header (px = outer box size) */
export function instLogoChip(logo: string | null | undefined, px: number): string {
  return logo
    ? `<img src="${esc(logo)}" alt="" style="height:${px}px;width:${px}px;object-fit:contain;background:#fff;border-radius:${Math.round(px / 4)}px;padding:${Math.max(2, Math.round(px / 12))}px;box-sizing:border-box"/>`
    : "";
}

/** branding footer appended to every printable document — static (non-animated)
    Increlog logo rendered right before the credit line */
export function brandFooter(poweredBy: string): string {
  return `<div style="text-align:center;font-size:9px;color:#9aa5b1;letter-spacing:.3px;margin-top:10px">${increlogLogoSvgString(12)}&nbsp;${esc(poweredBy)}</div>`;
}

/** wrap a document so each one lands on its own printed page (except the last) */
export function pageWrap(inner: string, i: number, total: number): string {
  return `<div${i < total - 1 ? ' style="page-break-after:always"' : ""}>${inner}</div>`;
}

/** shared print window — opens the generated document and triggers the print dialog */
export function openPrintWindow(title: string, locale: string, bodyHtml: string) {
  const html = `<!DOCTYPE html>
<html lang="${locale}">
<head>
<meta charset="utf-8"/>
<title>${esc(title)}</title>
<style>
  @page { margin: 14mm; }
  body { font-family: 'Montserrat', 'Noto Sans Bengali', sans-serif; color: #2c323f; margin: 0; }
  .doc { width: 720px; margin: 0 auto; }
  table { border-collapse: collapse; }
</style>
</head>
<body>${bodyHtml}</body>
</html>`;
  const w = window.open("", "_blank", "width=900,height=760");
  if (!w) return;
  w.document.write(html);
  w.document.close();
  w.focus();
  setTimeout(() => w.print(), 350);
}

/** institution letterhead used by printable documents (from the API) */
export interface InstBranding {
  name: string;
  address: string;
  phone: string;
  logo: string | null;
}
