import { execFileSync } from "node:child_process";
import { expect } from "e2e";
import { IOS, test } from "./t3-mobile.ts";

// Count the flat blue fill in the actual native screenshot, away from antialiased edges.
function bluePixels(path: string, hex: string) {
  const histogram = execFileSync("convert", [path, "-depth", "8", "-format", "%c", "histogram:info:-"], { encoding: "utf8" });
  return histogram.split("\n").filter(line => line.toUpperCase().includes(hex.toUpperCase())).reduce((sum, line) => sum + Number(line.trim().split(":")[0]), 0);
}

test("Adaptive project SVG palettes follow iOS appearance", IOS, async ({ t3, app, screen, device }) => {
  await t3.project("adaptive-proj", {
    "t3.json": JSON.stringify({ iconPath: "adaptive-icon.svg" }),
    "adaptive-icon.svg": '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 48 48"><style>:root{color:#111827}@media(prefers-color-scheme:dark){:root{color:#F8FAFC}}</style><path d="M24 4 44 24 24 44 4 24Z" fill="none" stroke="currentColor" stroke-width="4"/></svg>',
  });
  await t3.project("twotone-proj", {
    "t3.json": JSON.stringify({ iconPath: "twotone-icon.svg" }),
    "twotone-icon.svg": '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 48 48"><style>.a{fill:#1D4ED8}.b{fill:#B91C1C}@media(prefers-color-scheme:dark){.a{fill:#93C5FD}.b{fill:#FCA5A5}}</style><rect class="a" width="48" height="24"/><rect class="b" y="24" width="48" height="24"/></svg>',
  });
  await device.setAppearance("light");
  await t3.open();
  await screen.getByRole("button", "New chat").or(screen.getByRole("button", "New thread")).tap();
  await device.openLink("t3code-dev://new", { app: "com.t3tools.t3code.dev" });
  await screen.getByRole("button", "twotone-proj").first().boundingBox();
  await app.screenshot("system-light");
  await device.setAppearance("dark");
  const dark = await app.screenshot("system-dark");
  await device.openApp("com.t3tools.t3code.dev", { relaunch: true });
  await device.openLink("t3code-dev://new", { app: "com.t3tools.t3code.dev" });
  await screen.getByRole("button", "twotone-proj").first().boundingBox();
  const relaunched = await app.screenshot("dark-after-relaunch");
  await device.openLink("t3code-dev://settings/appearance", { app: "com.t3tools.t3code.dev" });
  await screen.getByRole("radio", "Light appearance").first().tap();
  await app.screenshot("light-override-setting-system-dark");
  await device.openLink("t3code-dev://new", { app: "com.t3tools.t3code.dev" });
  await screen.getByRole("button", "twotone-proj").first().boundingBox();
  const lightOverride = await app.screenshot("app-light-system-dark");
  await device.setAppearance("light");
  await device.openLink("t3code-dev://settings/appearance", { app: "com.t3tools.t3code.dev" });
  await screen.getByRole("radio", "Dark appearance").first().tap();
  await app.screenshot("dark-override-setting-system-light");
  await device.openLink("t3code-dev://new", { app: "com.t3tools.t3code.dev" });
  await screen.getByRole("button", "twotone-proj").first().boundingBox();
  const darkOverride = await app.screenshot("app-dark-system-light");
  await expect(bluePixels(dark, "#93C5FD")).toBeGreaterThan(50);
  await expect(bluePixels(relaunched, "#93C5FD")).toBeGreaterThan(50);
  await expect(bluePixels(lightOverride, "#1D4ED8")).toBeGreaterThan(50);
  await expect(bluePixels(darkOverride, "#93C5FD")).toBeGreaterThan(50);
});
