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

describe('InvoiceOut descriptor path', () => {
    let browser;
    let page;

    beforeAll(async() => {
        browser = await getBrowser();
        page = browser.page;
        await page.loginAndModule('administrative', 'ticket');
    });

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

    describe('as Administrative', () => {
        it('should search for tickets with an specific invoiceOut', async() => {
            await page.waitToClick(selectors.ticketsIndex.openAdvancedSearchButton);
            await page.clearInput(selectors.ticketsIndex.advancedSearchDaysOnward);
            await page.write(selectors.ticketsIndex.advancedSearchInvoiceOut, 'T2222222');
            await page.waitToClick(selectors.ticketsIndex.advancedSearchButton);
            await page.waitForState('ticket.card.summary');
        });

        it('should navigate to the invoiceOut index', async() => {
            await page.waitToClick(selectors.globalItems.applicationsMenuButton);
            await page.waitForSelector(selectors.globalItems.applicationsMenuVisible);
            await page.waitToClick(selectors.globalItems.invoiceOutButton);
            await page.waitForSelector(selectors.invoiceOutIndex.topbarSearch);
            await page.waitForState('invoiceOut.index');
        });

        it(`should click on the search result to access to the invoiceOut summary`, async() => {
            await page.accessToSearchResult('T2222222');
            await page.waitForState('invoiceOut.card.summary');
        });

        it('should delete the invoiceOut using the descriptor more menu', async() => {
            await page.waitToClick(selectors.invoiceOutDescriptor.moreMenu);
            await page.waitToClick(selectors.invoiceOutDescriptor.moreMenuDeleteInvoiceOut);
            await page.waitToClick(selectors.invoiceOutDescriptor.acceptDeleteButton);
            const message = await page.waitForSnackbar();

            expect(message.text).toContain('InvoiceOut deleted');
        });

        it('should have been relocated to the invoiceOut index', async() => {
            await page.waitForState('invoiceOut.index');
        });

        it(`should search for the deleted invouceOut to find no results`, async() => {
            await page.doSearch('T2222222');
            const nResults = await page.countElement(selectors.invoiceOutIndex.searchResult);

            expect(nResults).toEqual(0);
        });

        it('should navigate to the ticket index', async() => {
            await page.waitToClick(selectors.globalItems.applicationsMenuButton);
            await page.waitForSelector(selectors.globalItems.applicationsMenuVisible);
            await page.waitToClick(selectors.globalItems.ticketsButton);
            await page.waitForState('ticket.index');
        });

        it('should search now for tickets with an specific invoiceOut to find no results', async() => {
            await page.doSearch('T2222222');
            const nResults = await page.countElement(selectors.ticketsIndex.searchResult);

            expect(nResults).toEqual(0);
        });

        it('should now navigate to the invoiceOut index', async() => {
            await page.waitToClick(selectors.globalItems.applicationsMenuButton);
            await page.waitForSelector(selectors.globalItems.applicationsMenuVisible);
            await page.waitToClick(selectors.globalItems.invoiceOutButton);
            await page.waitForState('invoiceOut.index');
        });

        it(`should search and access to the invoiceOut summary`, async() => {
            await page.accessToSearchResult('T1111111');
            await page.waitForState('invoiceOut.card.summary');
        });

        it(`should check the invoiceOut is booked in the summary data`, async() => {
            await page.waitForTextInElement(selectors.invoiceOutSummary.bookedLabel, '/');
            const result = await page.waitToGetProperty(selectors.invoiceOutSummary.bookedLabel, 'innerText');

            expect(result.length).toBeGreaterThan(1);
        });

        it('should re-book the invoiceOut using the descriptor more menu', async() => {
            await page.waitToClick(selectors.invoiceOutDescriptor.moreMenu);
            await page.waitToClick(selectors.invoiceOutDescriptor.moreMenuBookInvoiceOut);
            await page.waitToClick(selectors.invoiceOutDescriptor.acceptBookingButton);
            const message = await page.waitForSnackbar();

            expect(message.text).toContain('InvoiceOut booked');
        });

        it(`should check the invoiceOut booked in the summary data`, async() => {
            let today = Date.vnNew();

            let day = today.getDate();
            if (day < 10) day = `0${day}`;

            let month = (today.getMonth() + 1);
            if (month < 10) month = `0${month}`;

            let expectedDate = `${day}/${month}/${today.getFullYear()}`;

            await page.waitForContentLoaded();
            const result = await page
                .waitToGetProperty(selectors.invoiceOutSummary.bookedLabel, 'innerText');

            expect(result).toEqual(expectedDate);
        });
    });

    describe('as salesPerson', () => {
        it(`should log in as salesPerson then go to the target invoiceOut summary`, async() => {
            await page.loginAndModule('salesPerson', 'invoiceOut');
            await page.accessToSearchResult('A1111111');
        });

        it(`should check the salesPerson role doens't see the book option in the more menu`, async() => {
            await page.waitToClick(selectors.invoiceOutDescriptor.moreMenu);
            await page.waitForSelector(selectors.invoiceOutDescriptor.moreMenuShowInvoiceOutPdf);
            await page.waitForSelector(selectors.invoiceOutDescriptor.moreMenuBookInvoiceOut, {hidden: true});
        });

        it(`should check the salesPerson role doens't see the delete option in the more menu`, async() => {
            await page.waitForSelector(selectors.invoiceOutDescriptor.moreMenuDeleteInvoiceOut, {hidden: true});
        });
    });
});