/* eslint-disable @typescript-eslint/ban-types */

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

export class MasterDetailViewPage {
  navigateToList() {
    return cy.visit('/master-detail/md-list-demo');
  }

  navigateToTable() {
    return cy.visit('/master-detail/md-table-demo');
  }

  navigateToNestedTabs() {
    return cy.visit('/master-detail/md-nested-tabs-demo');
  }

  getMasterDetailView() {
    return cy.get('bci-master-detail-view');
  }

  getMasterView() {
    return cy.get('bci-master-view');
  }

  getDetailView() {
    return cy.get('bci-detail-view');
  }

  getListRows() {
    return cy.get('bci-master-view .mat-mdc-list .mat-mdc-list-item');
  }

  getListRowContaining(text: string) {
    return cy.contains('bci-master-view .mat-mdc-list .mat-mdc-list-item', text);
  }

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

  getTableRowContaining(text: string) {
    return cy.contains('.mat-mdc-table .mat-mdc-row', text);
  }

  getNameField() {
    return cy.get('.mat-mdc-input-element').first();
  }

  getCloseButton() {
    return cy.contains('button', 'Cancel');
  }

  getSaveButton() {
    return cy.contains('button', 'Save');
  }

  switchToDialog(callback: Function) {
    this.isDialogUsed().then((used) => {
      if (!used) {
        this.toggleUseDialogSlide(used, true, callback);
      } else {
        callback();
      }
    });
  }

  switchToSidebar(callback: Function) {
    this.isDialogUsed().then((used) => {
      if (used) {
        this.toggleUseDialogSlide(used, false, callback);
      } else {
        callback();
      }
    });
  }

  expectCloseButtonToBeEnabled() {
    this.getCloseButton().should('exist');
    this.getCloseButton().should('be.enabled');
  }

  expectNameFieldToBeEnabled() {
    this.getNameField().should('exist');
    this.getNameField().should('be.enabled');
  }

  expectSidebarFullSize() {
    cy.get('.sidebar-full-size').should('exist');
  }

  expectSidebarSplit() {
    cy.get('.sidebar-full-size').should('not.exist');
  }

  waitDialogToBeClosed() {
    this.getMatCardFromDialog().should('not.exist');
  }

  waitDialogToBeVisible() {
    this.getMatCardFromDialog().should('exist');
  }

  waitSidebarToBeClosed() {
    this.getDetailContainer().should('not.be.visible');
  }

  waitSidebarToBeVisible() {
    this.getDetailContainer().should('be.visible');
  }

  getResizeHandle() {
    return cy.get('.resize-handle');
  }

  expectResizeHandleToBeEnabled() {
    this.getResizeHandle().should('exist');
  }

  toggleResizeHandleSlide(callback: Function) {
    const resizeHandleToggle = this.getResizeHandleToggle();
    resizeHandleToggle.should('exist');

    resizeHandleToggle.click().then(() => {
      callback();
    });
  }

  prepareResizeHandle() {
    this.getListRows().should('have.length.greaterThan', 0);
    this.switchToSidebar(() => {
      this.getListRows().first().click();
      this.waitSidebarToBeVisible();

      this.toggleResizeHandleSlide(() => {
        this.expectResizeHandleToBeEnabled();
      });
    });
  }

  getDetailContainer() {
    return cy.get('#detailContainer');
  }

  private getMatCardFromDialog() {
    return cy.get('mat-card-content');
  }

  private getList() {
    return cy.get('mat-list');
  }

  private isDialogUsed() {
    return cy.get('.testDialogToggle').then(($toggle) => {
      return $toggle.hasClass('.mat-checked');
    });
  }

  private getSlideToggle() {
    return cy.get('.testDialogToggle');
  }

  private toggleUseDialogSlide(currentValue: boolean, newValue: boolean, callback: Function) {
    const slideToggle = this.getSlideToggle();
    slideToggle.should('exist');

    const mustClick = currentValue !== newValue;
    if (mustClick) {
      slideToggle
        .find('.mdc-switch')
        .click()
        .then(() => {
          callback();
        });
    } else {
      callback();
    }
  }

  private getResizeHandleToggle() {
    return cy.get('.testResizeHandleToggle');
  }
}
