import { kbVar } from '@/config/kb';
import { navigateToBia } from '@/utils/kbUtils';
import { test } from '@core/fixtures';
import { expect } from 'playwright/test';
import { users } from '../../const';
import { Entity } from '@/core/api/types';
import { kbEntityManager } from '@/test-data/KB';
import { faker } from '@faker-js/faker';
import { roleManager } from '@/core/api/classes';

let company = {} as Entity;
let bia = {} as Entity;
let capability = {} as Entity;
let agenda = {} as Entity;

const change = {
  kod: faker.internet.domainName(),
  nameEn: 'Bia Changed Name EN',
  nameCz: 'Bia Changed Name CZ',
};

test.beforeAll(async ({ api: { login } }) => {
  await login();
  await roleManager.makeUserSuperuser(users.authorized.username);

  const biaEntity = await kbEntityManager.createBia();
  const capabilityEntity = await kbEntityManager.createCapability();

  bia = biaEntity.entity;
  company = biaEntity.entities.company;
  capability = capabilityEntity.entity;

  await kbEntityManager.create('KatalogAgend', {
    agendy: [],
    capabilita: capability.gid,
  });
  const agendaEntity = await kbEntityManager.createAgenda({
    capabilita: capability.gid,
  });

  agenda = agendaEntity.entity;
});
test.afterAll(async () => await kbEntityManager.flush());

test('Undo and Redo', async ({
  page,
  widgetHelper: w,
  layoutHeper: l,
  actions: { loginAsUser },
}) => {
  const { authorized: user } = users;
  await page.goto('/');
  await loginAsUser(user.username, user.password);
  await navigateToBia(w);
  await w.dataTable().singleAction({ icon: 'arrows-rotate' }).click(); // update cache just in case
  await w.dataTable().cell({ text: bia.nameCz, exact: true }).click();
  const engNameInput = w.textInput({ fieldName: 'nameCz' });
  await engNameInput.change(change.nameCz);
  await l.header.undo();
  await engNameInput.hasNotValue(change.nameCz);
  await l.header.redo();
  await engNameInput.hasValue(change.nameCz);
});

test.fixme(
  'Authorized user edits all details in all tabs',
  async ({
    page,
    widgetHelper: w,
    layoutHeper: l,
    actions: { loginAsUser },
  }) => {
    const { authorized: user } = users;
    await page.goto('/');
    await loginAsUser(user.username, user.password);
    await navigateToBia(w);
    await w.dataTable().cell({ text: bia.nameCz }).click();

    // Edit detail tab
    await w.tabBar().hasActiveTab('Detail');
    await w.textInput({ fieldName: 'kod' }).change(change.kod);
    await w.textInput({ fieldName: 'nameCz' }).change(change.nameCz);
    await w.textInput({ fieldName: 'nameEn' }).change(change.nameEn);

    const perimetrInput = w.selectInput({ label: 'Perimetr' });
    await perimetrInput.clear(true);
    await perimetrInput.selectFirstOption();

    const agendyInput = w.multiSearchInput({
      label: 'Agendy zařazené do společné BIA',
    });
    await agendyInput.clear(true);
    await agendyInput.selectFirstBySearch('AG');

    const managerInput = w.multiSearchInput({ label: 'BCM manažer' });
    await managerInput.clear(true);
    await managerInput.selectFirstBySearch(kbVar.val.person3);

    // Edit evaluations tab
    await w.tabBar().navigateToTab('Ohodnocení agendy');

    const firstTableRow = w.table({ index: 1 }).body.row(1);
    await firstTableRow
      .cell(4)
      .child.selectInput({ fieldName: 'biaKriticnost' })
      .selectFirstOption();
    await firstTableRow
      .cell(8)
      .child.selectInput({ fieldName: 'urcenoKObnove' })
      .selectFirstOption();
    await firstTableRow
      .cell(11)
      .child.selectInput({ fieldName: 'rto' })
      .select(kbVar.val.RTO);
    await firstTableRow
      .cell(12)
      .child.selectInput({ fieldName: 'disasterRto' })
      .select(kbVar.val.RPO);

    await firstTableRow.openDetail();
    await expect(w.table({ widgetLid: 'table-1' }).l).toBeVisible();
    await firstTableRow.closeDetail();
    await expect(w.table({ widgetLid: 'table-1' }).l).not.toBeVisible();

    // Edit Applications
    await w.tabBar().navigateToTab('Aplikace');
    await w.dataTable().modalAction({ label: 'Přidat' }).click();
    const modal = w.modal();
    await modal.isVisible();
    await modal.child.selectInput({ label: 'Agenda' }).selectFirstOption();
    await modal.child
      .searchInput({ label: 'Aplikace' })
      .selectByLabel(kbVar.val.app1);
    await modal.child.selectInput({ label: 'Stav' }).selectFirstOption();
    await modal.child.selectInput({ label: 'RTO ApI' }).selectFirstOption();
    await modal.child.selectInput({ label: 'RPO ApI' }).selectFirstOption();
    await modal.child.button({ label: 'Vytvořit' }).click();
    await w.dataTable().body.row(1).isVisible();

    // Edit Channels
    await w.tabBar().navigateToTab('Kanály');
    await w.dataTable().modalAction({ label: 'Přidat' }).click();
    await modal.isVisible();
    await modal.child.selectInput({ label: 'Agenda' }).selectFirstOption();
    await modal.child
      .searchInput({ label: 'Kanál' })
      .selectByLabel(kbVar.val.channel1);
    await modal.child.selectInput({ label: 'Stav' }).selectFirstOption();
    await modal.child
      .selectInput({ label: 'Požadované RTO' })
      .selectFirstOption();
    await modal.child.button({ label: 'Vytvořit' }).click();
    await w.dataTable().body.row(1).isVisible();

    // Edit Persons
    await w.tabBar().navigateToTab('Kritické osoby');
    await w.dataTable().modalAction({ label: 'Přidat' }).click();
    await modal.isVisible();
    await modal.child.selectInput({ label: 'Agenda' }).selectFirstOption();
    await modal.child
      .searchInput({ label: 'Osoba' })
      .selectByLabel(kbVar.val.person1);
    await modal.child.button({ label: 'Vytvořit' }).click();
    await w.dataTable().body.row(1).isVisible();
  }
);
