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

export class AccessControl3AppPo {
  constructor(private baseHref: string = '/') {}

  navigateTo() {
    cy.visit(this.baseHref);
  }

  navigateToAccessControl3() {
    cy.visit(this.baseHref + '?auth-provider=accessControl3');
  }

  getTitle() {
    return cy.get('.sidenav-header h1');
  }

  getSidebarLoginLogoutButton() {
    return cy.get('#sidenav-login-logout');
  }

  getSidebarLogoutButton() {
    return cy.get('#sidenav-login-logout .Bosch-Ic-user');
  }

  getSidebarLoginButton() {
    return cy.get('#sidenav-login-logout .Bosch-Ic-log-in');
  }

  getBoschLogo() {
    return cy.get('.header-logo img');
  }

  getSidebar() {
    return cy.get('bci-sidebar');
  }

  getSidebarItemByText(menuItemText: string) {
    return cy.contains('p.mat-line', menuItemText);
  }

  getOverlayBackdrop() {
    return cy.get('.cdk-overlay-backdrop');
  }

  getLogoutOverlay() {
    return cy.get('bci-logout');
  }

  getLogoutOverlayCloseButton() {
    return cy.get('bci-logout button.close-button');
  }

  getLogoutOverlayLogoutButton() {
    return cy.contains('bci-logout button', 'Logout');
  }

  isApplicationLoaded() {
    return this.getTitle().should('exist').should('be.visible');
  }

  makeLoggedOut() {
    this.isApplicationLoaded();
    this.getSidebarLoginLogoutButton().should('be.visible');
    this.getSidebarLoginButton().should('exist').should('be.visible');
    this.logout();

    this.isLoggedOut();
  }

  logout() {
    console.log('Logging out...');
    this.getSidebarLoginLogoutButton().click();
    this.getSidebarLoginLogoutButton().should('be.visible');
    this.isLoggedOut();
  }

  login(username: string, password: string) {
    this.getSidebarLoginLogoutButton().click();

    cy.get('#Username').type(username);
    cy.get('#Password').type(password);
    cy.get('button[value="login"]').click();
  }

  isLoggedIn() {
    return this.getSidebarLogoutButton().should('exist').should('be.visible');
  }

  isLoggedOut() {
    return this.getSidebarLoginButton().should('exist').should('be.visible');
  }
}
