/*
 * 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('Filtering in the Commandbar sidebar', {retries: {openMode: 2, runMode: 2}}, () => {
  const page = new CommandbarPage();

  beforeEach(() => {
    cy.viewport(1600, 800);
    page.navigateTo();
  });

  describe('with initial setup', () => {
    it('should show the commandbar', () => {
      page.getCommandBar().should('exist');
      page.getCommandBar().should('be.visible');
    });

    it('should have 11 entries in the table', () => {
      page.getAllTableRows().should('have.length', 11);
    });
  });

  describe('with open filter sidebar', () => {
    beforeEach(() => {
      cy.viewport(480, 800);
      page.waitForFilterSidebarToBeInvisible();
      page.openFilterModalInSmallScreen();
      page.waitForFilterSidebarToBeVisible();
    });

    it('should have open sidebar on main slide', () => {
      page.waitForFilterSidebarApplyButtonToBeVisible();
      page.waitForFilterSidebarParentSlideButtonToBeInvisible();
    });

    it('should close the filter sidebar on apply button click and be able to reopen', () => {
      page.getFilterSidebarApplyButton().click();
      page.waitForFilterSidebarToBeInvisible();
      page.openFilterModalInSmallScreen();
      page.waitForFilterSidebarToBeVisible();
      page.waitForFilterSidebarApplyButtonToBeVisible();
    });

    it('should close the filter sidebar on history back and be able to reopen', () => {
      cy.go('back');
      page.waitForFilterSidebarToBeInvisible();
      page.openFilterModalInSmallScreen();
      page.waitForFilterSidebarToBeVisible();
      page.waitForFilterSidebarApplyButtonToBeVisible();
    });

    it('should filter by department', () => {
      page.getFilterSidebarFilterEntryByTitle('Departments').click();
      page.waitForFilterSidebarApplyButtonToBeInvisible();
      page.waitForFilterSidebarParentSlideButtonToBeVisible();
      page.getFilterSidebarListOptionByLabel('Sales').click();
      page.getFilterSidebarListOptionByLabel('Security').click();

      // back to main filter slide
      cy.go('back');

      // filter active and state visualized
      page.getFilterSidebarFilterEntrySubtitleByTitle('Departments').should('contain.text', 'Sales, Security');

      // close filter
      cy.go('back');
      page.waitForFilterSidebarToBeInvisible();

      // table filtered
      // only the three sales and security employees expected
      page.getAllTableRows().should('have.length', 5);

      page.openFilterModalInSmallScreen();
      page.waitForFilterSidebarApplyButtonToBeVisible();

      // filter active and state visualized and can be cleared
      page.getFilterSidebarFilterEntrySubtitleByTitle('Departments').should('contain.text', 'Sales, Security');
      page.getFilterSidebarFilterEntryClearByTitle('Departments').click();
      page.getFilterSidebarApplyButton().click();

      page.getAllTableRows().should('have.length.greaterThan', 5);
    });
  });
});
