import getBrowser from '../../helpers/puppeteer';

const $ = {
    payMethod: 'vn-client-billing-data vn-autocomplete[ng-model="$ctrl.client.payMethodFk"]',
    IBAN: 'vn-client-billing-data vn-textfield[ng-model="$ctrl.client.iban"]',
    dueDay: 'vn-client-billing-data vn-input-number[ng-model="$ctrl.client.dueDay"]',
    receivedCoreLCRCheckbox: 'vn-client-billing-data vn-check[label="Received LCR"]',
    receivedCoreVNLCheckbox: 'vn-client-billing-data vn-check[label="Received core VNL"]',
    receivedB2BVNLCheckbox: 'vn-client-billing-data vn-check[label="Received B2B VNL"]',
    swiftBic: 'vn-client-billing-data vn-autocomplete[ng-model="$ctrl.client.bankEntityFk"]',
    newBankEntityButton: 'vn-client-billing-data vn-icon-button[vn-tooltip="New bank entity"] > button',
    newBankEntityName: '.vn-dialog.shown vn-textfield[ng-model="$ctrl.data.name"]',
    newBankEntityBIC: '.vn-dialog.shown vn-textfield[ng-model="$ctrl.data.bic"]',
    newBankEntityCountry: '.vn-dialog.shown vn-autocomplete[ng-model="$ctrl.data.countryFk"]',
    newBankEntityCode: '.vn-dialog.shown vn-textfield[ng-model="$ctrl.data.id"]',
    acceptBankEntityButton: '.vn-dialog.shown button[response="accept"]',
    saveButton: 'vn-client-billing-data button[type=submit]',
    watcher: 'vn-client-billing-data vn-watcher'
};

describe('Client Edit billing data path', () => {
    let browser;
    let page;
    beforeAll(async() => {
        browser = await getBrowser();
        page = browser.page;
        await page.loginAndModule('administrative', 'client');
        await page.accessToSearchResult('Bruce Banner');
        await page.accessToSection('client.card.billingData');
    });

    afterAll(async() => {
        await browser.close();
    });

    it(`should attempt to edit the billing data without an IBAN but fail`, async() => {
        await page.autocompleteSearch($.payMethod, 'PayMethod with IBAN');
        await page.autocompleteSearch($.swiftBic, 'BBKKESMMMMM');
        await page.clearInput($.dueDay);
        await page.write($.dueDay, '60');
        await page.waitForTextInField($.dueDay, '60');
        await page.waitToClick($.receivedCoreLCRCheckbox);
        await page.waitToClick($.receivedCoreVNLCheckbox);
        await page.waitToClick($.receivedB2BVNLCheckbox);
        await page.waitToClick($.saveButton);
        const message = await page.waitForSnackbar();

        expect(message.text).toContain('That payment method requires an IBAN');
    });

    it(`should create a new BIC code`, async() => {
        await page.waitToClick($.newBankEntityButton);
        await page.write($.newBankEntityName, 'Gotham City Bank');
        await page.write($.newBankEntityBIC, 'GTHMCT');
        await page.autocompleteSearch($.newBankEntityCountry, 'EspaƱa');
        await page.write($.newBankEntityCode, '9999');
        await page.waitToClick($.acceptBankEntityButton);
        const message = await page.waitForSnackbar();
        await page.waitForTextInField($.swiftBic, 'GTHMCT');
        const newcode = await page.waitToGetProperty($.swiftBic, 'value');

        expect(newcode).toEqual('GTHMCT');
        expect(message.text).toContain('Data saved!');
    });

    it(`should confirm the IBAN pay method was sucessfully saved`, async() => {
        const payMethod = await page.waitToGetProperty($.payMethod, '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() => {
        await page.write($.IBAN, 'ES9121000418450200051332');
        await page.keyboard.press('Tab');
        await page.keyboard.press('Tab');
        await page.waitForTextInField($.swiftBic, 'caixesbb');
        let automaticCode = await page.waitToGetProperty($.swiftBic, 'value');

        expect(automaticCode).toEqual('CAIXESBB');
    });

    it(`should save the form with all its new data`, async() => {
        await page.waitForWatcherData($.watcher);
        await page.waitToClick($.saveButton);
        const message = await page.waitForSnackbar();

        expect(message.text).toContain('Notification sent!');
    });

    it('should confirm the billing data have been edited', async() => {
        const dueDate = await page.waitToGetProperty($.dueDay, 'value');
        const IBAN = await page.waitToGetProperty($.IBAN, 'value');
        const swiftBic = await page.waitToGetProperty($.swiftBic, 'value');
        const receivedCoreLCR = await page.checkboxState($.receivedCoreLCRCheckbox);
        const receivedCoreVNL = await page.checkboxState($.receivedCoreVNLCheckbox);
        const receivedB2BVNL = await page.checkboxState($.receivedB2BVNLCheckbox);

        expect(dueDate).toEqual('60');
        expect(IBAN).toEqual('ES9121000418450200051332');
        expect(swiftBic).toEqual('CAIXESBB');
        expect(receivedCoreLCR).toBe('checked');
        expect(receivedCoreVNL).toBe('unchecked');
        expect(receivedB2BVNL).toBe('unchecked');
    });
});