/// <reference lib="dom" />
import { expect } from "e2e";
import { SECOND_HOME, startSecondServer } from "./second-server.ts";
import { makeState, test, WEB } from "./t3.ts";

test("Connected thread can be dragged between disconnected unarranged threads", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("Local work");
  const first = await t3.thread({ project, title: "A-old-1", messages: ["hello"] });
  await t3.thread({ project, title: "A-old-2", messages: ["hello"] });
  const server = startSecondServer(app.baseUrl ?? "");
  const remote = makeState(SECOND_HOME).state;
  let stopped = false;
  try {
    const { host, token } = await server.ready;
    const otherProject = await remote.project("Remote work");
    for (let i = 1; i <= 4; i++) await remote.thread({ project: otherProject, title: `B-new-${i}`, messages: ["hello"] });
    await t3.open("/settings/general");
    await screen.getByRole("switch", "Working section", { exact: true }).tap();
    await t3.open("/settings/connections");
    await screen.getByRole("button", "Add environment").tap();
    const dialog = screen.getByRole("dialog", "Add Environment", { exact: true });
    await dialog.getByPlaceholder("backend.example.com").fill(host);
    await dialog.getByPlaceholder("PAIRCODE").fill(token);
    await dialog.getByRole("button", "Add environment").tap();
    await dialog.waitFor({ state: "hidden" });
    await t3.open(first);
    const row = (title: string) => screen.getByRole("button", title, { exact: false }).first();
    await row("B-new-4").waitFor();
    const point = async (title: string) => {
      const box = await row(title).boundingBox();
      if (!box) throw new Error(`Missing row ${title}`);
      return { x: box.x + box.width / 2, y: box.y + box.height / 2 };
    };
    const dragAbove = async (source: string, target: string) => {
      const from = await point(source);
      const to = await point(target);
      await browser.mouse.move(from.x, from.y);
      await browser.mouse.down();
      await browser.mouse.move(from.x + 10, from.y);
      for (let i = 1; i <= 20; i++) await browser.mouse.move(from.x, from.y + (to.y - 7 - from.y) * i / 20);
      await browser.mouse.up();
    };
    await dragAbove("A-old-1", "A-old-2");
    for (let i = 5; i <= 6; i++) await remote.thread({ project: otherProject, title: `B-new-${i}`, messages: ["hello"] });
    await row("B-new-6").waitFor();
    await server.stop();
    stopped = true;
    await app.screenshot("offline-before-drag");
    await dragAbove("A-old-1", "B-new-5");
    await app.screenshot("offline-after-drag");
    const local = await point("A-old-1");
    const offline = await point("B-new-5");
    expect(local.y).toBeLessThan(offline.y);
    expect(local.y).toBeGreaterThan((await point("B-new-6")).y);
    await expect(screen.getByRole("button", "Unpin thread", { exact: true })).toBeHidden();
    await t3.open(first);
    await row("B-new-6").waitFor();
    await app.screenshot("offline-after-reload");
    expect((await point("A-old-1")).y).toBeLessThan((await point("B-new-5")).y);
    expect((await point("A-old-1")).y).toBeGreaterThan((await point("B-new-6")).y);
  } finally {
    if (!stopped) await server.stop();
  }
});
