/*
 * Copyright (C) 2019 Robert Bosch GmbH Copyright (C) 2019 Robert Bosch Manufacturing Solutions GmbH, Germany. All rights reserved.
 */

import { CommandbarPage } from '../../support/commandbar.po';

describe('Column Selector in the Commandbar', {retries: {openMode: 2, runMode: 2}}, () => {
  let page: CommandbarPage;

  before(() => {
    page = new CommandbarPage();
    page.navigateTo();
    cy.viewport(1920, 1080);
  });

  it('should hide commandbar column selector button', () => {
    page.getShowHideColumnsButton().should('not.exist');
  });

  it('should show commandbar column selector button', () => {
    cy.viewport(1920, 1080);
    page.getColumnSelectorButton().should('exist');
  });

  it('should open column selector dialog', () => {
    page
      .getCommandBarOptionsButton()
      .eq(3)
      .click()
      .then(() => page.getShowHideColumnsButton().click())
      .then(() => page.getColumnSelectorDialogCloseButton().should('be.visible').click())
      .then(() => page.getColumnSelector().should('not.exist'));

    page.getColumnSelector().should('not.exist');
  });

  describe('Show Hide columns', () => {
    beforeEach(() => {
      // show column selection dialog
      page
        .getCommandBarOptionsButton()
        .eq(3)
        .click()
        .then(() => page.getShowHideColumnsButton().click());
    });

    it('should show 0 available columns', () => {
      page.getColumnSelectorDialogSourceListOptions().should('have.length', 0);
      page.getColumnSelectorDialogCloseButton().click();
      page.getColumnSelector().should('not.exist');
    });

    it('should show 6 selected columns', () => {
      page.getColumnSelectorDialogTargetListOptions().should('have.length', 6);
      page.getColumnSelectorDialogCloseButton().click();
      page.getColumnSelector().should('not.exist');
    });

    it('should show all columns if dialog is cancelled', () => {
      page.getTableFirstRowColumns().should('have.length', 6);
      page.getColumnSelectorDialogCancelButton().click();
      page.getColumnSelector().should('not.exist');
    });

    it('should hide one column', () => {
      page.getTableFirstRowColumns().should('have.length', 6);
      page.getColumnSelectorDialogTargetListOptions().first().click();
      page.getColumnSelectorDialogRemoveSelectionButton().first().click();
      page.getColumnSelectorDialogSaveButton().click();
      page.getColumnSelector().should('not.exist');
      page.getTableFirstRowColumns().should('have.length', 5);
    });

    it('should restore hidden column', () => {
      page.getTableFirstRowColumns().should('have.length', 5);
      page.getColumnSelectorDialogSourceListOptions().first().click();
      page.getColumnSelectorDialogAddSelectionButton().first().click();
      page.getColumnSelectorDialogSaveButton().click();
      page.getColumnSelector().should('not.exist');
      page.getTableFirstRowColumns().should('have.length', 6);
    });
  });
});
