import selectors from '../../helpers/selectors.js';
import createNightmare from '../../helpers/nightmare';

describe('Client Edit pay method path', () => {
    const nightmare = createNightmare();

    beforeAll(() => {
        nightmare
            .loginAndModule('administrative', 'client')
            .accessToSearchResult('Bruce Banner')
            .accessToSection('client.card.billingData');
    });

    it(`should attempt to edit the Pay method without an IBAN but fail`, async() => {
        const snackbarMessage = await nightmare
            .autocompleteSearch(selectors.clientPayMethod.payMethodAutocomplete, 'PayMethod with IBAN')
            .autocompleteSearch(selectors.clientPayMethod.swiftBicAutocomplete, 'BBKKESMMMMM')
            .clearInput(selectors.clientPayMethod.dueDayInput)
            .write(selectors.clientPayMethod.dueDayInput, '60')
            .waitForTextInInput(selectors.clientPayMethod.dueDayInput, '60')
            .waitToClick(selectors.clientPayMethod.receivedCoreLCRCheckbox)
            .waitToClick(selectors.clientPayMethod.receivedCoreVNLCheckbox)
            .waitToClick(selectors.clientPayMethod.receivedB2BVNLCheckbox)
            .waitToClick(selectors.clientPayMethod.saveButton)
            .waitForLastSnackbar();

        expect(snackbarMessage).toEqual('That payment method requires an IBAN');
    });

    it(`should add the IBAN but fail as it requires a BIC code`, async() => {
        const snackbarMessage = await nightmare
            .waitToClick(selectors.clientPayMethod.clearswiftBicButton)
            .clearInput(selectors.clientPayMethod.IBANInput)
            .write(selectors.clientPayMethod.IBANInput, 'FR9121000418450200051332')
            .waitForTextInInput(selectors.clientPayMethod.IBANInput, 'FR9121000418450200051332')
            .wait(1000)
            .waitToClick(selectors.clientPayMethod.saveButton)
            .waitForLastSnackbar();

        expect(snackbarMessage).toEqual('That payment method requires a BIC');
    });

    it(`should create a new BIC code`, async() => {
        const newcode = await nightmare
            .waitToClick(selectors.clientPayMethod.newBankEntityButton)
            .write(selectors.clientPayMethod.newBankEntityName, 'Gotham City Bank')
            .write(selectors.clientPayMethod.newBankEntityBIC, 'GTHMCT')
            .waitToClick(selectors.clientPayMethod.acceptBankEntityButton)
            .waitToGetProperty(`${selectors.clientPayMethod.swiftBicAutocomplete} input`, 'value');

        expect(newcode).toEqual('GTHMCT Gotham City Bank');
    });

    it(`should confirm the IBAN pay method is sucessfully saved`, async() => {
        const payMethod = await nightmare
            .waitToGetProperty(`${selectors.clientPayMethod.payMethodAutocomplete} input`, 'value');

        expect(payMethod).toEqual('PayMethod with IBAN');
    });

    it(`should clear the BIC code field, update the IBAN to see how he BIC code autocompletes`, async() => {
        const AutomaticCode = await nightmare
            .clearInput(selectors.clientPayMethod.IBANInput)
            .waitToClick(selectors.clientPayMethod.clearswiftBicButton)
            .write(selectors.clientPayMethod.IBANInput, 'ES9121000418450200051332')
            .waitToGetProperty(`${selectors.clientPayMethod.swiftBicAutocomplete} input`, 'value');

        expect(AutomaticCode).toEqual('CAIXESBB Caixa Bank');
    });

    it(`should save the form with all its new data`, async() => {
        const snackbarMessages = await nightmare
            .waitToClick(selectors.clientPayMethod.saveButton)
            .waitForSnackbar();

        expect(snackbarMessages).toEqual(jasmine.arrayContaining(['Data saved!']));
    });

    it('should confirm the due day have been edited', async() => {
        const dueDate = await nightmare
            .waitToGetProperty(selectors.clientPayMethod.dueDayInput, 'value');

        expect(dueDate).toEqual('60');
    });

    it('should confirm the IBAN was saved', async() => {
        const IBAN = await nightmare
            .waitToGetProperty(selectors.clientPayMethod.IBANInput, 'value');

        expect(IBAN).toEqual('ES9121000418450200051332');
    });

    it('should confirm the swift / BIC code was saved', async() => {
        const code = await nightmare
            .waitToGetProperty(`${selectors.clientPayMethod.swiftBicAutocomplete} input`, 'value');

        expect(code).toEqual('CAIXESBB Caixa Bank');
    });

    it('should confirm Received LCR checkbox is checked', async() => {
        const checkedBox = await nightmare
            .evaluate(selector => {
                return document.querySelector(selector).checked;
            }, selectors.clientPayMethod.receivedCoreLCRCheckbox);

        expect(checkedBox).toBeTruthy();
    });

    it('should confirm Received core VNL checkbox is unchecked', async() => {
        const checkedBox = await nightmare
            .evaluate(selector => {
                return document.querySelector(selector).checked;
            }, selectors.clientPayMethod.receivedCoreVNLCheckbox);

        expect(checkedBox).toBeFalsy();
    });

    it('should confirm Received B2B VNL checkbox is unchecked', async() => {
        const checkedBox = await nightmare
            .evaluate(selector => {
                return document.querySelector(selector).checked;
            }, selectors.clientPayMethod.receivedB2BVNLCheckbox);

        expect(checkedBox).toBeFalsy();
    });
});