import { environment } from './../../../src/environments/environment';
import { LoginHelper } from '../../helpers/login';
import { cypressConfig, mockApiResponse } from './../../helpers/common';

const loginHelper = new LoginHelper();
const path = '/smart-sync';
let elements: SmartSyncElements;

describe('Smart sync page', () => {

  beforeEach(() => {
    loginHelper.visitPageAndSubmitForm(
      'login/success-eula-accepted.xml',
      {
        status: 200,
        response: '@successResponse',
      }
    );
    cy.url().should('include', '/smart-sync');
    elements = {
      actions: () => cy.get('.actions'),
      categories: () => cy.get('.categories'),
      continueWithSyncButton: () => cy.get('.continue-with-sync'),
      continueWithoutSyncButton: () => cy.get('.continue-without-sync'),
      downloadImages: () => cy.get('.download-images'),
      modules: () => cy.get('.modules'),
      properties: () => cy.get('.properties'),
      severities: () => cy.get('.severities'),
    };
    const mockResponseAlias = 'successResponse'
    cy.fixture('smart-sync/success.xml').as(mockResponseAlias);
    mockApiResponse({
      status: 200,
      response: `@${mockResponseAlias}`,
      method: 'POST',
      url: `${environment.apiUrl}/get_synchronize`,
    });
  });

  const selectOptionsInSelect = (
    element: any,
    indicies: number[],
  ) => {
    element.click();
    cy.get('ion-alert', { timeout: 10000 }).should('be.visible');
    for(let index of indicies) {
      cy.get('.alert-checkbox-group button').eq(index).click();
    }
    cy.get('.alert-button:not(.alert-button-role-cancel)').click();
    cy.wait(2000);
  }

  xit('Pre-loads options for modules & properties correctly', () => {
    elements.modules().find('ion-select-option').eq(0).should('contain', 'Risk & Compliance');
    elements.properties().find('ion-select-option').eq(0).should('contain', '1 Avenue Business Park');
  });

  xit('Updates downstream <select> elements based on upstream choices', () => {

    selectOptionsInSelect(elements.modules(), [0, 1]);

    // Expect actions to be set from modules
    elements.actions().should('not.have.attr', 'disabled');
    elements.actions().find('ion-select-option').eq(0).should('contain', 'Risk & Compliance Actions');
    elements.actions().find('ion-select-option').eq(1).should('contain', 'Environmental Management Actions');

    selectOptionsInSelect(elements.actions(), [0, 1]);

    // Expect categories to be set from actions
    elements.categories().should('not.have.attr', 'disabled');
    elements.categories().find('ion-select-option').eq(0).should('contain', 'Risk & Compliance / Asbestos');
    elements.categories().find('ion-select-option').eq(4).should('contain', 'Environmental Management / Environmental');

    // Expect severities to be set from actions
    elements.severities().should('not.have.attr', 'disabled');
    elements.severities().find('ion-select-option').eq(0).should('contain', 'Risk & Compliance / High');
    elements.severities().find('ion-select-option').eq(4).should('contain', 'Environmental Management / High Environmental');

  });
/* temp disabled
  it('Prevents submission until both modules & properties are selected', () => {

    elements.continueWithSyncButton().click();
    cy.url().should('include', '/smart-sync');

    selectOptionsInSelect(elements.modules(), [0, 1]);
    elements.continueWithSyncButton().click();
    cy.url().should('include', '/smart-sync');

    selectOptionsInSelect(elements.properties(), [0]);
    elements.continueWithSyncButton().click();
    cy.url().should('include', '/tabs/property-selection');

  });

  it('Preselects previous selections when present', () => {
    selectOptionsInSelect(elements.properties(), [0]);
    selectOptionsInSelect(elements.modules(), [0, 1]);
    selectOptionsInSelect(elements.actions(), [0, 1]);
    selectOptionsInSelect(elements.categories(), [0]);
    selectOptionsInSelect(elements.severities(), [0]);
    elements.continueWithSyncButton().click();
    cy.url().should('include', '/tabs/property-selection');
    cy.visit(path); // TODO change with back button once implemented
    cy.wait(0);
    elements.properties().find('input[type=hidden]').invoke('val')
      .then(sometext => expect(sometext).to.equal('19'));
    elements.modules().find('input[type=hidden]').invoke('val')
      .then(sometext => expect(sometext).to.equal('1,3'));
    elements.actions().find('input[type=hidden]').invoke('val')
      .then(sometext => expect(sometext).to.equal('4,9'));
    elements.categories().find('input[type=hidden]').invoke('val')
      .then(sometext => expect(sometext).to.equal('296'));
    elements.severities().find('input[type=hidden]').invoke('val')
      .then(sometext => expect(sometext).to.equal('16'));
  });*/

});


interface SmartSyncElements {
  actions: any;
  categories: any;
  continueWithSyncButton: any;
  continueWithoutSyncButton: any;
  downloadImages: any;
  modules: any;
  properties: any;
  severities: any;
}
