/*
 ************************************************************************************
 * Copyright (C) 2022-2023 Openbravo S.L.U.
 * Licensed under the Openbravo Commercial License version 1.0
 * You may obtain a copy of the License at http://www.openbravo.com/legal/obcl.html
 * or in the legal folder of this module distribution.
 ************************************************************************************
 */

import '../../functions';

describe('Key Map Editor tests', () => {
  beforeEach(() => {
    cy.logInPOSWithSession();
    cy.setFixture('KeyMapEditor').as('testData');
  });

  describe('Basic Key Map Editor flows', () => {
    it('Creates a new key map category, links products to it, asserts its existence in Layout Editor and finally removes it', function keyMapEditorT1() {
      cy.goToKeyMapEditor();
      cy.window().should(win => {
        expect(
          win.OB.App.State.getState().Configuration.configurationSet
            .OBC2_CurrentWindow
        ).to.be.equal('OBPOS2_KeyMapEditorWindow');
      });
      cy.selectKeyMap(this.testData.keyMap);

      cy.get('[data-testid="obpos2KeymapEditorSearchBar"]').type('v9');
      cy.get(
        '[data-testid="obpos2KeyMapEditorGrid-dataGrid-rowContent-0"]'
      ).should('not.exist');
      cy.get('[data-testid="obpos2KeymapEditorSearchBar-clearText"]').click();
      cy.get('[data-testid="obpos2KeymapEditorSearchBar"]').type('v1');
      cy.get(
        '[data-testid="obpos2KeyMapEditorGrid-dataGrid-rowContent-0"]'
      ).should('contain', 'v1');
      cy.get(
        '[data-testid="obpos2KeyMapEditorGrid-actionButton-gridEvents-0"]'
      ).click();

      cy.createNewCategory({
        categoryName: this.testData.categoryName,
        categoryDescription: this.testData.categoryDescription,
        categoryProdVis: this.testData.categoryProdVisualization,
        categoryColorHexCode: this.testData.categoryColorHexCode
      });
      cy.intercept({
        method: 'POST',
        url: Cypress.env('baseUrl').concat(
          '/org.openbravo.mobile.core.service.jsonrest/org.openbravo.pos2.KeymapLoader?$timeout=15000'
        )
      }).as('keyMapSync');

      // cy.get('[data-testid="obpos2KeyMapEditorGrid-dataGrid-rowContent-2"]')
      //   .should('contain', this.testData.categoryName)
      //   .and('contain', this.testData.categoryDescription)
      //   .contains('View Products')
      //   .click();
      cy.get('[data-testid="obpos2KeymapEditorSaveButton"]')
        .should('be.enabled')
        .click();
      cy.wait('@keyMapSync')
        .its('response.statusCode')
        .should('eq', 200);
      cy.logOut();
      // cy.get('[data-testid="obc2MultiOptionDialog-buttonBar-2"]').click();

      // cy.linkProdCategory(
      //   this.testData.categoryLinked,
      //   this.testData.linkedCategoryColorHexCode
      // );
      // cy.linkProduct(
      //   this.testData.productLinked,
      //   this.testData.linkedProductColorHexCode
      // );

      // cy.saveKeyMapChanges();
      // cy.wait('@keyMapSync')
      //   .its('response.statusCode')
      //   .should('eq', 200);

      // cy.openLayoutEditor();
      // cy.unfoldCategoriesAccordion();
      // cy.get(
      //   '[data-testid="obpos2ProductCategoryButton-productCategory-name"]',
      //   { timeout: Cypress.env('timeoutCommand') }
      // ).should('contain', this.testData.categoryName);

      // cy.unfoldProductsAccordionAndDisplayCategoriesList();
      // // Created issue about data-testid: https://issues.openbravo.com/view.php?id=49599
      // cy.get(
      //   '[class="MuiButtonBase-root MuiListItem-root MuiMenuItem-root MuiMenuItem-gutters MuiListItem-gutters MuiListItem-button"]'
      // )
      //   .contains(this.testData.categoryName)
      //   .click();

      // cy.get('[data-testid="obc2MultiStepProcessHeaderTitle"]')
      //   .contains('Versions')
      //   .click();

      // cy.get(
      //   '[data-testid="obpos2KeyMapEditorGrid-actionButton-gridEvents-0"]'
      // ).click();

      // cy.get('[data-testid="obpos2KeyMapEditorGrid-dataGrid-rowContent-2"]')
      //   .should('contain', this.testData.categoryName)
      //   .find('[data-testid="obpos2KeymapEditorDeleteButton"]')
      //   .click();

      // cy.saveKeyMapChanges();
      // cy.wait('@keyMapSync')
      //   .its('response.statusCode')
      //   .should('eq', 200);

      // // Assertion will be skipped due async issue: https://issues.openbravo.com/view.php?id=49718

      // // Asserts that Cypress test category has been deleted
      // cy.openLayoutEditor();
      // cy.unfoldCategoriesAccordion();

      // // Following command (118 - 122) is a workaround to avoid unstabilities when reloading keymap from main screen
      // // Delete it once assetion skip is undone
      // cy.get(
      //   '[data-testid="obpos2ProductCategoryButton-productCategory-name"]',
      //   { timeout: Cypress.env('timeoutCommand') }
      // ).should('contain', 'Best Sellers');

      /*
      cy.get(
        '[data-testid="obpos2ProductCategoryButton-productCategory-name"]',
        { timeout: Cypress.env('timeoutCommand') }
      ).should('not.contain', this.testData.categoryName);

      cy.unfoldProductsAccordionAndDisplayCategoriesList();
      cy.get(
        '[class="MuiButtonBase-root MuiListItem-root MuiMenuItem-root MuiMenuItem-gutters MuiListItem-gutters MuiListItem-button"]'
      ).should('not.contain', this.testData.categoryName);
      */
    });
  });
});
