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

import { MacmaAppPo } from '../support/macma-app.po';

xdescribe('App', () => {
  const application = new MacmaAppPo();

  afterEach(() => {
    application.makeLoggedOut();
  });

  describe('on landing page with default realm', () => {
    beforeEach(() => {
      application.navigateToMacma();
      application.makeLoggedOut();
    });

    it('should load app', () => {
      application.isApplicationLoaded();
    });

    describe('as admin', () => {
      beforeEach(() => {
        application.login(
          application.getMacmaTestParams().adminUsername,
          application.getMacmaTestParams().adminPassword
        );
      });

      it('should be logged in', () => {
        application.isApplicationLoaded();
        application.isLoggedIn();
      });

      it('should stay logged in on refresh', () => {
        application.isLoggedIn();

        cy.reload();

        application.isApplicationLoaded();
        application.isLoggedIn();
      });

      // xit('should be logged in in new tab', () => {
      //     application.isLoggedIn();
      //
      //     browser.executeScript('window.open()');
      //
      //     let initialWindow = null, additionalWindow = null;
      //     browser.getAllWindowHandles().then((handles) => {
      //         initialWindow = handles[handles.length - 2];
      //         additionalWindow = handles[handles.length - 1];
      //     });
      //
      //     browser.switchTo().window(additionalWindow);
      //     application.navigateToMacma();
      //     expect(application.isApplicationLoaded()).toBeTruthy();
      //     expect(application.isLoggedIn()).toBeTruthy();
      //     browser.driver.close();
      //
      //     // old tab is still unchanged
      //     browser.switchTo().window(initialWindow);
      //     expect(application.isApplicationLoaded()).toBeTruthy();
      //     expect(application.isLoggedIn()).toBeTruthy();
      // });

      it('should be logged out when switching realm', () => {
        application.navigateToMacmaTenant(application.getMacmaTestParams().testRealm);

        application.isApplicationLoaded();
        application.isLoggedOut();
      });
    });
  });

  describe('on landing page with test realm', () => {
    beforeEach(() => {
      application.navigateToMacmaTenant(application.getMacmaTestParams().testRealm);
      application.makeLoggedOut();
      // expect(application.loginLogoutButton.getText()).toContain(`(${conf.testRealm})`);
    });

    it('should display the app title and be in test realm', () => {
      application.getTitle().should('contain.text', `@bci-web-core/auth Demo App`);
    });

    describe("as a test-tenant's user", () => {
      beforeEach(() => {
        application.login(
          application.getMacmaTestParams().testAccessManagerUsername,
          application.getMacmaTestParams().testAccessManagerPassword
        );
      });

      it('should login', () => {
        application.isApplicationLoaded();
        application.isLoggedIn();
      });

      it('should keep realm after logout', () => {
        application.isLoggedIn();
        application.logout();
        application.isLoggedOut();
      });
    });
  });
});
