import { test, expect } from '../../fixtures/index';
import { LoginPage } from '../../pom/login.page';
import { SharedRequestPage } from '../../pom/share-request.page';

test.describe('UI case for client tests', async () => {
  let loginPage: LoginPage;
  let sharedRequestPage: SharedRequestPage;

  test.beforeEach(async ({ page, conf }) => {
    loginPage = new LoginPage(page);
    sharedRequestPage = new SharedRequestPage(page);

    await loginPage.open();
    await loginPage.login(conf.data.username, conf.data.password);
    await expect(loginPage.baseLoc.dashboardContainer).toBeVisible();

    await sharedRequestPage.open();
  });

  test('TC_SWR_001 - Verify UI when empty state', {
    tag: ['@TC_SWR_001', '@share-request', '@ui']
  }, async ({ page }) => {
    await expect(page.locator('#root')).toMatchAriaSnapshot(`
          - heading "Manage" [level=2]:
            - img
          - img
          - text: Show
          - textbox: /\\d+/
          - text: entries
          - button "Filter":
            - img
          - img
          - textbox "Search..."
          - text: No matching results found. Try again.
          `);
  });

  test('TC_SWR_002 - Verify UI when have shared request', {
    tag: ['@TC_SWR_002', '@share-request', '@ui']
  }, async ({ page }) => {
    await expect(page.locator('#root')).toMatchAriaSnapshot(`
         - heading "Manage" [level=2]:
           - img
         - img
         - text: Show
         - textbox: /\\d+/
         - text: entries
         - button "Filter":
           - img
         - img
         - textbox "Search..."
         `);

    await expect(async () => {
      const rowCount = await sharedRequestPage.dashboardLoc.table.container.locator('tbody').locator('tr').count();
      expect(rowCount).toBeGreaterThan(1);
    }).toPass();

    // Verify default columns visible
    const defaultColumns = ['Pin', 'Request #', 'Request Type', 'Client Name', 'From', 'Directed To', 'Status', 'Status Details'];
    for (let i = 0; i < defaultColumns.length; i++) {
      await expect(sharedRequestPage.dashboardLoc.table.headingColumn(defaultColumns[i])).toBeVisible();
    }
  });

  test("SWR_013 - Verify UI when turn off all/ turn on all Notification", {
    tag: ['@SWR_013', '@share-request', '@ui']
  }, async ({conf}) => {
    await test.step("Turn off all notifications", async () => {
      await sharedRequestPage.getDetailCase(conf.data.case);
      await sharedRequestPage.shareRequestLoc.notification.btnOpenModalNoti.nth(1).click();
      await sharedRequestPage.waitForSecond(3);
      await sharedRequestPage.shareRequestLoc.notification.btnTurnOffAll.click();
      await sharedRequestPage.dashboardLoc.common.popUpConfirm.click();
      await sharedRequestPage.waitForSecond(1); // Wait for update notification

      //Verify UI turn off all in app
      await sharedRequestPage.shareRequestLoc.notification.btnToggleSettingNoti.first().click();
      await expect(sharedRequestPage.dashboardLoc.root).toMatchAriaSnapshot(sharedRequestPage.shareRequestSnapshot.turnOffAllNotificationInApp);

      //Verify UI turn off all in mail
      await sharedRequestPage.shareRequestLoc.notification.btnToggleSettingNoti.first().click();
      await expect(sharedRequestPage.dashboardLoc.root).toMatchAriaSnapshot(sharedRequestPage.shareRequestSnapshot.turnOffAllNotificationEmail);
    });

    await test.step("Turn on all notifications", async () => {
      await sharedRequestPage.shareRequestLoc.notification.btnTurnOnAll.click();
      await expect(sharedRequestPage.dashboardLoc.notificationUpdateSuccess).toBeVisible();
      await sharedRequestPage.waitForSecond(1);

      //Verify UI turn on all in app
      await sharedRequestPage.shareRequestLoc.notification.btnToggleSettingNoti.first().click();
      await expect(sharedRequestPage.dashboardLoc.root).toMatchAriaSnapshot(sharedRequestPage.shareRequestSnapshot.turnOnAllNotificationInApp);

      //Verify UI turn on all in mail
      await sharedRequestPage.shareRequestLoc.notification.btnToggleSettingNoti.first().click();
      await expect(sharedRequestPage.dashboardLoc.root).toMatchAriaSnapshot(sharedRequestPage.shareRequestSnapshot.turnOnAllNotificationEmail);
    })
  });
  // Waiting for link case feature complete
  // test("SWC_003 - Verify when click manually and not manually in case had exist", {
  //   tag: ['@SWC_003', '@share-request', '@function']
  // }, async ({ conf }) => {
  //   await test.step("Search and open modal Organize Now", async () => {
  //     await sharedRequestPage.getDetailCase(conf.data.case);
  //     await expect(sharedRequestPage.dashboardLoc.common.spanText("Organize Now")).toBeVisible();
  //     await sharedRequestPage.dashboardLoc.common.spanText("Organize Now").click();
  //     await expect(sharedRequestPage.dashboardLoc.modal.headerModalh2("You already have this case")).toBeVisible();
  //   });

  //   await test.step("Click not manually", async () => {
  //     await sharedRequestPage.shareRequestLoc.linkCase.btnNext.click();
  //     await expect(sharedRequestPage.dashboardLoc.modal.headerModalh2("Compare Request to Case")).toBeVisible();
  //     await expect(sharedRequestPage.dashboardLoc.root).toMatchAriaSnapshot(sharedRequestPage.shareRequestSnapshot.dialogCompareRequest);
  //     await sharedRequestPage.dashboardLoc.buttonByText("Close").last().click();
  //   });

  //   await test.step("Verify UI when click Manually", async() => {
  //     await sharedRequestPage.dashboardLoc.common.spanText("Organize Now").click();
  //     await sharedRequestPage.dashboardLoc.common.spanText("I want to do Manually").click();
  //     await expect(sharedRequestPage.dashboardLoc.root).toMatchAriaSnapshot(sharedRequestPage.shareRequestSnapshot.modalManuallyLinkCase)
  //   })
  // });
});