import { expect, test } from "../../fixtures";
import { adjustScreenView } from "../../utils/adjust-screen-view";
import { awaitBootstrapTest } from "../../utils/await-bootstrap-test";
import {
  closeAdvancedOptions,
  disableInspectPanel,
  enableInspectPanel,
  openAdvancedOptions,
} from "../../utils/open-advanced-options";

test(
  "ToggleComponent",
  { tag: ["@release", "@workspace"] },
  async ({ page }) => {
    await awaitBootstrapTest(page);

    await page.waitForSelector('[data-testid="blank-flow"]', {
      timeout: 30000,
    });
    await page.getByTestId("blank-flow").click();

    // Open the sidebar options dropdown
    await page.getByTestId("sidebar-options-trigger").click();

    // Wait for and click the legacy switch
    await page
      .getByTestId("sidebar-legacy-switch")
      .waitFor({ state: "visible" });
    await page.getByTestId("sidebar-legacy-switch").click();
    expect(
      await page
        .getByTestId("sidebar-legacy-switch")
        .getAttribute("aria-checked"),
    ).toBe("true");

    await page.getByTestId("sidebar-search-input").click();
    await page.getByTestId("sidebar-search-input").fill("directory");

    await page.waitForSelector('[data-testid="files_and_knowledgeDirectory"]', {
      timeout: 30000,
    });
    await page
      .getByTestId("files_and_knowledgeDirectory")
      .dragTo(page.locator('//*[@id="react-flow-id"]'));
    await page.mouse.up();
    await page.mouse.down();

    await adjustScreenView(page);

    await page.getByTestId("div-generic-node").click();

    await openAdvancedOptions(page);

    await page.locator('//*[@id="showload_hidden"]').click();
    expect(
      await page.locator('//*[@id="showload_hidden"]').isChecked(),
    ).toBeTruthy();

    await closeAdvancedOptions(page);

    await adjustScreenView(page);

    await page.getByTestId("toggle_bool_load_hidden").click();
    expect(
      await page.getByTestId("toggle_bool_load_hidden").isChecked(),
    ).toBeTruthy();

    await page.getByTestId("toggle_bool_load_hidden").click();
    expect(
      await page.getByTestId("toggle_bool_load_hidden").isChecked(),
    ).toBeFalsy();

    await page.getByTestId("toggle_bool_load_hidden").click();
    expect(
      await page.getByTestId("toggle_bool_load_hidden").isChecked(),
    ).toBeTruthy();

    await page.getByTestId("toggle_bool_load_hidden").click();
    expect(
      await page.getByTestId("toggle_bool_load_hidden").isChecked(),
    ).toBeFalsy();

    await page.getByTestId("toggle_bool_load_hidden").click();
    expect(
      await page.getByTestId("toggle_bool_load_hidden").isChecked(),
    ).toBeTruthy();

    await page.getByTestId("div-generic-node").click();

    await adjustScreenView(page);

    await disableInspectPanel(page);

    await openAdvancedOptions(page);

    expect(
      await page.getByTestId("toggle_bool_load_hidden").isChecked(),
    ).toBeTruthy();

    await page.locator('//*[@id="showload_hidden"]').click();
    expect(
      await page.locator('//*[@id="showload_hidden"]').isChecked(),
    ).toBeFalsy();

    await page.locator('//*[@id="showmax_concurrency"]').click();
    expect(
      await page.locator('//*[@id="showmax_concurrency"]').isChecked(),
    ).toBeTruthy();

    await page.locator('//*[@id="showpath"]').click();
    expect(await page.locator('//*[@id="showpath"]').isChecked()).toBeFalsy();

    await page.locator('//*[@id="showrecursive"]').click();
    expect(
      await page.locator('//*[@id="showrecursive"]').isChecked(),
    ).toBeTruthy();

    await page.locator('//*[@id="showsilent_errors"]').click();
    expect(
      await page.locator('//*[@id="showsilent_errors"]').isChecked(),
    ).toBeTruthy();

    await page.locator('//*[@id="showuse_multithreading"]').click();
    expect(
      await page.locator('//*[@id="showuse_multithreading"]').isChecked(),
    ).toBeTruthy();

    await page.locator('//*[@id="showmax_concurrency"]').click();
    expect(
      await page.locator('//*[@id="showmax_concurrency"]').isChecked(),
    ).toBeFalsy();

    await page.locator('//*[@id="showpath"]').click();
    expect(await page.locator('//*[@id="showpath"]').isChecked()).toBeTruthy();

    await page.locator('//*[@id="showrecursive"]').click();
    expect(
      await page.locator('//*[@id="showrecursive"]').isChecked(),
    ).toBeFalsy();

    await page.locator('//*[@id="showsilent_errors"]').click();
    expect(
      await page.locator('//*[@id="showsilent_errors"]').isChecked(),
    ).toBeFalsy();

    await page.locator('//*[@id="showuse_multithreading"]').click();
    expect(
      await page.locator('//*[@id="showuse_multithreading"]').isChecked(),
    ).toBeFalsy();

    await closeAdvancedOptions(page);

    const plusButtonLocator = page.getByTestId("toggle_bool_load_hidden");
    const elementCount = await plusButtonLocator?.count();
    if (elementCount === 0) {
      expect(true).toBeTruthy();

      await page.getByTestId("div-generic-node").click();

      await openAdvancedOptions(page);

      await page.locator('//*[@id="showload_hidden"]').click();
      expect(
        await page.locator('//*[@id="showload_hidden"]').isChecked(),
      ).toBeTruthy();

      expect(
        await page.getByTestId("toggle_bool_load_hidden").isChecked(),
      ).toBeTruthy();

      await closeAdvancedOptions(page);

      await page.getByTestId("toggle_bool_load_hidden").click();
      expect(
        await page.getByTestId("toggle_bool_load_hidden").isChecked(),
      ).toBeFalsy();

      await page.getByTestId("toggle_bool_load_hidden").click();
      expect(
        await page.getByTestId("toggle_bool_load_hidden").isChecked(),
      ).toBeTruthy();

      await page.getByTestId("toggle_bool_load_hidden").click();
      expect(
        await page.getByTestId("toggle_bool_load_hidden").isChecked(),
      ).toBeFalsy();

      await page.getByTestId("toggle_bool_load_hidden").click();
      expect(
        await page.getByTestId("toggle_bool_load_hidden").isChecked(),
      ).toBeTruthy();

      await page.getByTestId("toggle_bool_load_hidden").click();
      expect(
        await page.getByTestId("toggle_bool_load_hidden").isChecked(),
      ).toBeFalsy();
    }
    await enableInspectPanel(page);
  },
);
