import Jet from "~/assets/Jet.txt?ansi";
import { frostedBox } from "~/components/frosted-box";

export function homePage(outlet: HTMLElement) {
  outlet.classList.remove("qa-outlet");
  outlet.innerHTML = `
    <div class="flex flex-col items-center justify-start">
      ${frostedBox(`
        <div class="flex flex-col items-center justify-center gap-[1.25ch] md:gap-[2ch] md:flex-row">
          <div class="order-1 flex flex-col items-center md:order-2">
            <h1 class="sr-only">Jet Pham</h1>
            <div aria-hidden="true" data-emitter-ansi>${Jet}</div>
            <p class="mt-[2ch]">Software Extremist</p>
          </div>
          <div class="order-2 shrink-0 md:order-1">
            <img
              data-emitter-image
              src="/jet.svg"
              alt="A picture of Jet wearing a beanie in purple and blue lighting"
              width="250"
              height="250"
              class="aspect-square w-full max-w-[220px] object-cover md:h-[263px] md:w-[175px] md:max-w-none"
              style="background-color: #a80055; color: transparent"
            />
          </div>
        </div>
        <fieldset class="section-block mt-[2ch] border-2 px-[calc(1.5ch-0.5px)] pb-[1ch] pt-0" style="border-color: var(--white)">
          <legend class="-mx-[0.5ch] px-[0.5ch]" style="color: var(--white)">Contact</legend>
          <button type="button" id="copy-email" class="qa-inline-action">jet@extremist.software</button>
          <span id="copy-email-status" class="qa-meta ml-[1ch]" aria-live="polite"></span>
        </fieldset>
        <fieldset class="section-block mt-[2ch] border-2 px-[calc(1.5ch-0.5px)] pb-[1ch] pt-0" style="border-color: var(--white)">
          <legend class="-mx-[0.5ch] px-[0.5ch]" style="color: var(--white)">Links</legend>
          <ol>
            <li><a href="https://git.extremist.software" class="inline-flex items-center">Forgejo</a></li>
            <li><a href="https://github.com/jetpham" class="inline-flex items-center">GitHub</a></li>
            <li><a href="https://x.com/exmistsoftware" class="inline-flex items-center">X</a></li>
            <li><a href="https://bsky.app/profile/extremist.software" class="inline-flex items-center">Bluesky</a></li>
          </ol>
        </fieldset>
      `)}
    </div>`;

  const copyButton = document.getElementById("copy-email") as HTMLButtonElement;
  const copyStatus = document.getElementById(
    "copy-email-status",
  ) as HTMLSpanElement;
  let resetTimer: number | null = null;

  copyButton.addEventListener("click", () => {
    void (async () => {
      try {
        await navigator.clipboard.writeText("jet@extremist.software");
        copyStatus.textContent = "copied";
        copyStatus.style.color = "var(--light-green)";
      } catch {
        copyStatus.textContent = "copy failed";
        copyStatus.style.color = "var(--light-red)";
      }

      if (resetTimer !== null) window.clearTimeout(resetTimer);
      resetTimer = window.setTimeout(() => {
        copyStatus.textContent = "";
        resetTimer = null;
      }, 1400);
    })();
  });
}
