import { test, expect } from "@playwright/test";
import { mockApi, 登录 } from "../测试夹具";

test.describe("快速记账：新增账目", () => {
  test("01-输入金额和备注后可以完成一笔支出", async ({ page }) => {
    await mockApi(page);
    await 登录(page);
    await page.getByRole("button", { name: "记一笔" }).click();
    await expect(page.getByRole("region", { name: "快速记账" })).toBeVisible();
    await page.getByRole("button", { name: "1", exact: true }).click();
    await page.getByRole("button", { name: "2", exact: true }).click();
    await page.getByLabel("备注").fill("测试午餐");
    await page.getByLabel("备注").press("Tab");
    await page.getByRole("button", { name: "完成记账" }).click();
    await expect(page.getByText("已记入账本")).toBeVisible();
    await expect(page.getByText("测试午餐")).toBeVisible();
  });

  test("02-个人账本自动选中且不能取消，其他账本可以多选", async ({ page }) => {
    await mockApi(page);
    await 登录(page);
    await page.getByRole("button", { name: "记一笔" }).click();
    await page.getByRole("button", { name: /记入/ }).click();
    const personal = page.getByRole("button", { name: /个人账本自动记入/ });
    await expect(personal).toBeDisabled();
    await expect(personal).toContainText("自动记入");
    await page.getByRole("button", { name: /家庭账本/ }).click();
    await expect(page.getByRole("button", { name: /完成/ }).last()).toBeVisible();
  });

  test("03-切换币种后保持金额数字且可以确认", async ({ page }) => {
    await mockApi(page);
    await 登录(page);
    await page.getByRole("button", { name: "记一笔" }).click();
    await page.getByRole("button", { name: /切换币种/ }).click();
    await expect(page.getByRole("dialog", { name: "选择币种" })).toBeVisible();
    await page.getByRole("button", { name: /美元/ }).click();
    await expect(page.getByRole("button", { name: /切换币种，当前美元/ })).toBeVisible();
    await page.getByRole("button", { name: "8", exact: true }).click();
    await page.getByRole("button", { name: "完成记账" }).click();
    await expect(page.getByText("已记入账本")).toBeVisible();
  });

  test("04-大金额完整显示且不使用省略号", async ({ page }) => {
    await mockApi(page);
    await 登录(page);
    await page.getByRole("button", { name: "记一笔" }).click();
    for (let index = 0; index < 9; index += 1) {
      await page.getByRole("button", { name: "9", exact: true }).click();
    }
    const amount = page.locator(".amount-value");
    await expect(amount).toContainText("999,999,999");
    await expect(amount).not.toContainText("...");
    await expect(page.locator(".drawer-header.long-amount")).toBeVisible();
  });

  test("05-备注输入回车后恢复数字键盘并保留备注", async ({ page }) => {
    await mockApi(page);
    await 登录(page);
    await page.getByRole("button", { name: "记一笔" }).click();
    const drawer = page.getByRole("region", { name: "快速记账" });
    const note = drawer.getByLabel("备注");
    await note.fill("回车备注");
    await note.press("Enter");
    await expect(drawer.getByRole("button", { name: "完成记账" })).toBeVisible();
    await expect(note).toHaveValue("回车备注");
  });

  test("06-点击遮罩或向下拖动把手都带动画关闭抽屉", async ({ page }) => {
    await mockApi(page);
    await 登录(page);
    await page.getByRole("button", { name: "记一笔" }).click();
    const drawer = page.getByRole("region", { name: "快速记账" });
    await expect(drawer).toHaveClass(/drawer-entered/);
    const handle = page.getByRole("button", { name: "向下拖动关闭快速记账" });
    const handleBox = await handle.boundingBox();
    expect(handleBox).not.toBeNull();
    const startY = handleBox!.y + handleBox!.height / 2;
    await handle.dispatchEvent("pointerdown", { pointerId: 1, pointerType: "touch", isPrimary: true, button: 0, clientY: startY });
    await handle.dispatchEvent("pointermove", { pointerId: 1, pointerType: "touch", isPrimary: true, buttons: 1, clientY: startY + 80 });
    await handle.dispatchEvent("pointerup", { pointerId: 1, pointerType: "touch", isPrimary: true, button: 0, clientY: startY + 80 });
    await expect(drawer).not.toBeVisible();

    await page.getByRole("button", { name: "记一笔" }).click();
    await page.locator(".drawer-scrim").click({ position: { x: 8, y: 8 } });
    await expect(page.getByRole("region", { name: "快速记账" })).not.toBeVisible();
  });
});
