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

const $ = {
    company: 'vn-client-balance-index vn-autocomplete[ng-model="$ctrl.companyId"]',
    newPaymentButton: `vn-float-button`,
    newPayment: '.vn-dialog.shown',
    refundAmount: '.vn-dialog.shown [vn-name="amountToReturn"]',
    saveButton: '.vn-dialog.shown [response="accept"]',
    firstLineBalance: 'vn-client-balance-index vn-tbody > vn-tr:nth-child(1) > vn-td:nth-child(8)',
    firstLineReference: 'vn-client-balance-index vn-tbody > vn-tr:nth-child(1) > vn-td-editable',
    firstLineReferenceInput: 'vn-client-balance-index vn-tbody > vn-tr:nth-child(1) > vn-td-editable vn-textfield',
};

describe('Client balance path', () => {
    let browser;
    let page;
    beforeAll(async() => {
        browser = await getBrowser();
        page = browser.page;
        await page.loginAndModule('administrative', 'client');
        await page.accessToSearchResult('Petter Parker');
    });

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

    it('should now edit the local user config data', async() => {
        await page.waitToClick(selectors.globalItems.userMenuButton);
        await page.autocompleteSearch(selectors.globalItems.userLocalCompany, 'CCs');
        const companyMessage = await page.waitForSnackbar();

        await page.accessToSection('client.card.balance.index');
        const company = await page.getValue($.company);

        await page.waitToClick(selectors.globalItems.userMenuButton);
        await page.clearInput(selectors.globalItems.userConfigThirdAutocomplete);
        const message = await page.waitForSnackbar();

        await page.closePopup();
        await page.reloadSection('client.card.balance.index');

        expect(companyMessage.isSuccess).toBeTrue();
        expect(company).toEqual('CCs');
        expect(message.isSuccess).toBeTrue();
    });

    it('should create a new payment that clears the debt', async() => {
        await page.waitToClick($.newPaymentButton);
        await page.fillForm($.newPayment, {
            bank: 'Cash',
            description: 'Description',
            viewReceipt: false
        });
        await page.respondToDialog('accept');
        const message = await page.waitForSnackbar();

        expect(message.isSuccess).toBeTrue();
    });

    it('should edit the 1st line reference and check data', async() => {
        await page.waitToClick($.firstLineReference);
        await page.write($.firstLineReferenceInput, 'Miscellaneous payment');
        await page.keyboard.press('Enter');
        const message = await page.waitForSnackbar();

        await page.waitForSpinnerLoad();
        let company = await page.getValue($.company);
        let reference = await page.innerText($.firstLineReference);
        let firstBalanceLine = await page.innerText($.firstLineBalance);

        expect(message.isSuccess).toBeTrue();
        expect(company).toEqual('VNL');
        expect(reference).toEqual('Miscellaneous payment');
        expect(firstBalanceLine).toContain('0.00');
    });

    it('should create a new payment, check the cash comparison works correctly and balance value is -100', async() => {
        await page.waitToClick($.newPaymentButton);
        await page.fillForm($.newPayment, {
            amountPaid: 100,
            description: 'Payment',
            deliveredAmount: 500,
            viewReceipt: false
        });
        const refund = await page.getValue($.refundAmount);
        await page.respondToDialog('accept');
        const message = await page.waitForSnackbar();

        const result = await page.innerText($.firstLineBalance);

        expect(refund).toEqual('400');
        expect(message.isSuccess).toBeTrue();
        expect(result).toContain('-€100.00');
    });

    it('should create a new payment and check the cash exceeded the maximum', async() => {
        await page.waitToClick($.newPaymentButton);
        await page.fillForm($.newPayment, {
            bank: 'Cash',
            amountPaid: 1001,
            description: 'Payment'
        });
        await page.waitToClick($.saveButton);
        const message = await page.waitForSnackbar();

        expect(message.text).toContain('Amount exceeded');
    });

    it('should create a new payment that sets the balance back to negative value and check it', async() => {
        await page.closePopup();
        await page.waitToClick($.newPaymentButton);

        await page.fillForm($.newPayment, {
            bank: 'Pay on receipt',
            amountPaid: -150,
            description: 'Description'
        });
        await page.respondToDialog('accept');
        const message = await page.waitForSnackbar();

        const result = await page.innerText($.firstLineBalance);

        expect(message.isSuccess).toBeTrue();
        expect(result).toEqual('€50.00');
    });

    it('should now click on the Clients button of the top bar menu', async() => {
        await page.login('employee');
        await page.waitToClick(selectors.globalItems.applicationsMenuButton);
        await page.waitForSelector(selectors.globalItems.applicationsMenuVisible);
        await page.waitToClick(selectors.globalItems.clientsButton);
        await page.waitForSelector(selectors.clientsIndex.createClientButton);
        await page.waitForState('client.index');
    });

    it('should now search for the user Petter Parker not check the payment button is not present', async() => {
        await page.accessToSearchResult('Petter Parker');
        await page.accessToSection('client.card.balance.index');
        await page.waitForSelector($.newPaymentButton, {hidden: true});
    });
});