/// <reference lib="dom" />
import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

test("A selected red swatch recolors the active label and saved image", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("demo");
  const thread = await t3.thread({ project, title: "Image label color", messages: ["Start"] });
  await t3.open(thread);
  const picker = '[data-chat-composer-form] input[type="file"]';
  await browser.locator(picker).waitFor({ state: "attached" });
  await browser.evaluate(async (selector) => {
    const canvas = document.createElement("canvas");
    canvas.width = 600; canvas.height = 400;
    const ctx = canvas.getContext("2d")!;
    ctx.fillStyle = "white"; ctx.fillRect(0, 0, 600, 400);
    const blob = await new Promise<Blob>((resolve) => canvas.toBlob((b) => resolve(b!), "image/png"));
    const files = new DataTransfer(); files.items.add(new File([blob], "white.png", { type: "image/png" }));
    const input = document.querySelector<HTMLInputElement>(selector)!;
    input.files = files.files; input.dispatchEvent(new Event("change", { bubbles: true }));
    return null;
  }, picker);
  await screen.getByRole("button", "Preview white.png").tap();
  await screen.getByRole("button", "Draw on image").tap();
  await screen.getByRole("button", "Add a text label (T)").tap();
  const drawing = 'svg[aria-label="Draw on white.png"]';
  await browser.locator(drawing).waitFor();
  const point = await browser.evaluate((selector) => {
    const r = document.querySelector(selector)!.getBoundingClientRect();
    return { x: r.x + r.width * 100 / 600, y: r.y + r.height * 114 / 400 };
  }, drawing);
  await browser.mouse.move(point.x, point.y); await browser.mouse.down(); await browser.mouse.up();
  const input = screen.getByRole("textbox", "Text label", { exact: true });
  await input.fill("Bug");
  await screen.getByRole("radio", "Red", { exact: true }).tap();
  await input.fill("Bug here");
  const preview = await browser.evaluate((selector) => {
    const text = document.querySelector(selector)!.querySelector("text")!;
    return { text: text.textContent, fill: text.getAttribute("fill"), stroke: text.getAttribute("stroke"), x: text.getAttribute("x"), y: text.getAttribute("y") };
  }, drawing);
  await app.screenshot("active-label-after-red-swatch");
  await screen.getByRole("button", "Done", { exact: true }).tap();
  await browser.locator('[role="dialog"] img').waitFor();
  const pixels = await browser.evaluate(async () => {
    const image = document.querySelector<HTMLImageElement>('[role="dialog"] img')!;
    await image.decode();
    const canvas = document.createElement("canvas"); canvas.width = image.naturalWidth; canvas.height = image.naturalHeight;
    const ctx = canvas.getContext("2d")!; ctx.drawImage(image, 0, 0);
    const { data } = ctx.getImageData(0, 0, canvas.width, canvas.height);
    let red = 0, blue = 0;
    for (let i = 0; i < data.length; i += 4) {
      if (data[i]! > 180 && data[i+1]! < 120 && data[i+2]! < 120) red++;
      else if (data[i+2]! > 150 && data[i]! < 120) blue++;
    }
    return { red, blue, width: canvas.width, height: canvas.height };
  });
  await app.screenshot("saved-label-image");
  console.log(JSON.stringify({ preview, pixels }));
  await expect(preview.fill).toBe("#ef4444");
  await expect(preview.stroke).toBe("rgba(0,0,0,0.6)");
  await expect(preview.text).toBe("Bug here");
  await expect(pixels.red).toBeGreaterThan(0);
  await expect(pixels.blue).toBe(0);
});
