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

export class CommandbarPage {
  navigateTo() {
    return cy.visit('/data-table-demo/commandbar-demo'); 
  }

  getCommandBar() {
    return cy.get('.mat-toolbar-row:last-of-type');
  }

  getTableInteractionCommandBar() {
    return cy.get('[data-test="table-interaction-demo-command-bar"]');
  }

  toggleCommandBarDemoFilterToggle() {
    return cy.get('[data-test="commandbar-demo-filter-toggle"]').click();
  }

  getSearchInput() {
    return cy.get('[data-test="table-interaction-demo-command-bar"]').find('[data-test="commandbar-search-input"]').eq(-1);
  }

  getItemCount() {
    return cy.get('[data-test="table-interaction-demo-command-bar"]').find('[data-test="commandbar-item-count"]').eq(-1);
  }

  getDepartmentFilterChip() {
    return cy.get('bci-chip');
  }

  getAllFilterChips() {
    return cy.get('bci-commandbar-active-filters').first().get('bci-chip');
  }

  getAllTableRows() {
    return cy.get('.mat-mdc-row');
  }

  getTableFirstRowColumns() {
    return this.getAllTableRows().eq(0).find('.mat-mdc-cell');
  }

  getMoreActionsButton() {
    return cy.get('bci-commandbar-button[icon="Bosch-Ic-show-more-vertical"]').eq(7);
  }

  getFilterButton() {
    return cy.get(this.getFilterButtonSelector());
  }

  getFilterButtonFromMoreActionsMenu() {
    return cy.get('bci-commandbar-optionsmenu mat-icon.Bosch-Ic-filter');
  }

  getFilterModal() {
    return cy.get(this.getFilterModalSelector());
  }

  getFilterSidebar() {
    return cy.get(this.getFilterSidebarSelector());
  }

  getFilterSidebarApplyButton() {
    return cy.get(`${this.getFilterSidebarSelector()} button.sidebar-apply-button`);
  }

  waitForFilterSidebarApplyButtonToBeVisible() {
    this.getFilterSidebarApplyButton().should('be.visible');
  }

  waitForFilterSidebarApplyButtonToBeInvisible() {
    this.getFilterSidebarApplyButton().should('not.be.visible');
  }

  getFilterSidebarParentSlideButton() {
    return cy.get(`${this.getFilterSidebarSelector()} button.sidebar-back-button`);
  }

  waitForFilterSidebarParentSlideButtonToBeVisible() {
    this.getFilterSidebarParentSlideButton().should('be.visible');
  }

  waitForFilterSidebarParentSlideButtonToBeInvisible() {
    this.getFilterSidebarParentSlideButton().should('not.exist');
  }

  waitForFilterSidebarToBeInvisible() {
    this.getFilterSidebar().should('not.be.visible');
  }

  waitForFilterSidebarToBeVisible() {
    this.getFilterSidebar().should('be.visible');
  }

  getFilterSidebarFilterEntryByTitle(filterTitle: string) {
    return cy.contains(`${this.getFilterSidebarSelector()} .mat-mdc-list-item`, filterTitle);
  }

  getFilterSidebarFilterEntrySubtitleByTitle(filterTitle: string) {
    return cy.contains(`${this.getFilterSidebarSelector()} .mdc-list-item__content`, filterTitle);
  }

  getFilterSidebarFilterEntryClearByTitle(filterTitle: string) {
    cy.contains(`${this.getFilterSidebarSelector()} bci-filter-entry`, filterTitle);
    return cy.contains(`${this.getFilterSidebarSelector()} bci-filter-entry`, filterTitle).find('.mat-mdc-button-touch-target');
  }

  getFilterSidebarListOptionByLabel(label: string) {
    return cy.contains(`${this.getFilterSidebarSelector()} .mdc-list-item__content`, label);
  }

  clickOnBackdrop() {
    // TODO
    cy.get('.cdk-overlay-backdrop').click(40, 40, { force: true });
    // browser.actions().mouseMove(webElement, { x: 40, y: 40 }).click().perform();
  }

  openFilterModalInSmallScreen() {
    const moreActionsButton = this.getMoreActionsButton();
    moreActionsButton.click();
    const filterButton = this.getFilterButtonFromMoreActionsMenu();
    filterButton.click();
  }

  openFilterModal() {
    this.getFilterButton().scrollIntoView();
    this.getFilterButton().should('be.visible');
    this.getFilterButton().click();
    cy.viewport(1200, 700);
    this.getAllDialogFilterEntries().should('be.visible');
  }

  getCommandBarButtonWithAriaLabel() {
    return cy.get(`${this.getCommandBarOptions()} button.magicClass`);
  }

  getFilterModalCloseButton() {
    return cy.get(`${this.getFilterModalSelector()} button.close-button`);
  }

  getFilterModalApplyButton() {
    return cy.get('.mat-mdc-dialog-actions button.button-primary');
  }

  getFilterModalCancelButton() {
    return cy.get('.mat-mdc-dialog-actions button.button-secondary');
  }

  // filter entries
  getAllDialogFilterEntries() {
    cy.get(this.getDialogFilterEntrySelector()).should('be.visible');
    return cy.get(this.getDialogFilterEntrySelector());
  }

  getDialogFilterEntryAt(index: number) {
    return cy.get(this.getDialogFilterEntrySelector() + ':nth-of-type(' + (index + 1) + ')');
  }

  getActiveDialogFilterEntry() {
    return cy.get('mat-dialog-container bci-filter-entry mat-list-item.active');
  }

  getTextElementInFilterEntry(listElement: Cypress.Chainable<JQuery<HTMLElement>>) {
    return listElement.find('.mdc-list-item__content:first-child');
  }

  getActiveIconInFilterEntry(entry: Cypress.Chainable<JQuery<HTMLElement>>) {
    return entry.find('mat-icon.Bosch-Ic-checkmark');
  }

  getDeleteIconInFilterEntry(entry: Cypress.Chainable<JQuery<HTMLElement>>) {
    return entry.find('.mat-mdc-button-touch-target');
  }

  // filter content
  getSelectedFilterView() {
    return cy.get(this.getSelectedFilterViewSelector());
  }

  getSelectedFilterContent() {
    return cy.get(`${this.getSelectedFilterViewSelector()} .mat-mdc-checkbox .mdc-label`);
  }

  getFilterCheckboxes() {
    return cy.get(`${this.getSelectedFilterViewSelector()} mat-checkbox`);
  }

  getOptionsMenuItems() {
    return cy.get('bci-commandbar-optionsmenu mat-list mat-list-item');
  }


  getColumnSelectorButton() {
    return cy.get('[data-test="show-hide-columns-button"]');
  }

  getCommandBarOptionsButton() {
    return cy.get('[data-test="commandbar-options-button"]');
  }

  getCommandBarSortButton() {
    return cy.get('bci-commandbar-optionsmenu [data-test="commandbar-sort-button"]');
  }

  getBadgeElement() {
    return cy.get('[data-test="commandbar-options-button"] .mat-icon .mat-badge-content-37');
  }

  getStandardAddButton() {
    return cy.get('[data-test="commandbar-add-button"]').last();
  }

  getDisabledAddButton() {
    return cy.get('[data-test="commandbar-add-button"] div button').first();
  }

  getOverriddenAddButton() {
    return cy.get('bci-commandbar.overridden-add-btn-commandbar bci-commandbar-button.commandbar-action-add').last();
  }

  getAddMenuFirstItem() {
    return cy.get('bci-commandbar-addmenu .mdc-list-item').first();
  }

  resizeToADesktopLandscapeSize() {
    this.resize(1281);
  }

  resizeToATabletLandscapeSize() {
    this.resize(993);
  }

  resizeToAMobileLandscapeSize() {
    this.resize(480);
  }

  expectCommandbarIconVisible(icon: string) {
    this.getCommandbarIcon(icon).should('exist');
    this.getCommandbarIcon(icon).should('be.visible');
  }

  expectCommandbarIconNotVisible(icon: string) {
    this.getCommandbarIcon(icon).should('not.exist');
  }

  getShowHideColumnsButton() {
    return cy.get('[data-test="show-hide-columns-button"]');
  }

  getColumnSelector() {
    return cy.get('bci-column-selector');
  }

  getColumnSelectorDialogCloseButton() {
    return cy.get('bci-column-selector [data-test="modal-window-close-button"]');
  }

  getColumnSelectorDialogSaveButton() {
    return cy.get('bci-column-selector [data-test="column-selector-save-button"]');
  }

  getColumnSelectorDialogCancelButton() {
    return cy.get('bci-column-selector [data-test="column-selector-cancel-button"]');
  }

  getColumnSelectorDialogSourceListOptions() {
    return cy.get('bci-column-selector bci-drag-drop-list[responsibility="source"] mat-list-option');
  }

  getColumnSelectorDialogTargetListOptions() {
    return cy.get('bci-column-selector bci-drag-drop-list[responsibility="target"] mat-list-option');
  }

  getColumnSelectorDialogAddSelectionButton() {
    return cy.get('bci-column-selector [data-test="picklist-add-selection-button"]');
  }

  getColumnSelectorDialogRemoveSelectionButton() {
    return cy.get('bci-column-selector [data-test="picklist-remove-selection-button"]');
  }

  private getDialogFilterEntrySelector() {
    return 'mat-dialog-container bci-filter-entry';
  }

  private getFilterButtonSelector() {
    return 'bci-commandbar[data-test="table-interaction-demo-command-bar"] button[data-test="commandbar-filter-button"]';
  }

  getFilterModalSelector() {
    return 'bci-complex-filter';
  }

  private getCommandBarOptions() {
    return 'bci-commandbar-actions';
  }

  private getFilterSidebarSelector() {
    return '.commandbar-mobile-sidenav';
  }

  private getSelectedFilterViewSelector() {
    return '.mat-mdc-dialog-container .filter-view';
  }

  private resize(windowWidth: number) {
    const windowHeight = 800;
    cy.viewport(windowWidth, windowHeight);
  }

  private getCommandbarIcon(icon: string) {
    return cy.get(`mat-icon.Bosch-Ic-${icon}`);
  }
}
