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

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

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

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

    describe('Delete ticket', () => {
        it('should search for an specific ticket', async() => {
            await page.write(selectors.ticketsIndex.topbarSearch, '18');
            await page.waitToClick(selectors.ticketsIndex.searchButton);
            await page.waitForNumberOfElements(selectors.ticketsIndex.searchResult, 1);
            const result = await page.countElement(selectors.ticketsIndex.searchResult);

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

        it(`should click on the search result to access to the ticket summary`, async() => {
            await page.waitForTextInElement(selectors.ticketsIndex.searchResult, 'Cerebro');
            await page.waitToClick(selectors.ticketsIndex.searchResult);
            let url = await page.expectURL('/summary');

            expect(url).toBe(true);
        });

        it(`should update the shipped hour using the descriptor menu`, async() => {
            await page.waitForContentLoaded();
            await page.waitToClick(selectors.ticketDescriptor.moreMenu);
            await page.waitToClick(selectors.ticketDescriptor.moreMenuChangeShippedHour);
            await page.pickTime(selectors.ticketDescriptor.changeShippedHour, '08:15');
            await page.waitToClick(selectors.ticketDescriptor.acceptChangeHourButton);
            const result = await page.waitForLastSnackbar();

            expect(result).toEqual('Shipped hour updated');
        });

        it(`should confirm the ticket descriptor shows the correct shipping hour`, async() => {
            const result = await page
                .waitToGetProperty(selectors.ticketDescriptor.descriptorDeliveryDate, 'innerText');

            expect(result).toContain('08:15');
        });

        it('should delete the ticket using the descriptor more menu', async() => {
            await page.waitToClick(selectors.ticketDescriptor.moreMenu);
            await page.waitToClick(selectors.ticketDescriptor.moreMenuDeleteTicket);
            await page.waitToClick(selectors.ticketDescriptor.acceptDeleteButton);
            const result = await page.waitForLastSnackbar();

            expect(result).toEqual('Ticket deleted');
        });

        it('should have been relocated to the ticket index', async() => {
            let url = await page.expectURL('#!/ticket/index');

            expect(url).toBe(true);
        });

        it(`should search for the deleted ticket and check it's date`, async() => {
            await page.write(selectors.ticketsIndex.topbarSearch, '18');
            await page.waitToClick(selectors.ticketsIndex.searchButton);
            await page.waitForNumberOfElements(selectors.ticketsIndex.searchResult, 1);
            await page.wait(selectors.ticketsIndex.searchResultDate);
            const result = await page.waitToGetProperty(selectors.ticketsIndex.searchResultDate, 'innerText');

            expect(result).toContain(2000);
        });
    });

    describe('add stowaway', () => {
        it('should search for a ticket', async() => {
            await page.clearInput(selectors.ticketsIndex.topbarSearch);
            await page.write(selectors.ticketsIndex.topbarSearch, '16');
            await page.waitToClick(selectors.ticketsIndex.searchButton);
            await page.waitForNumberOfElements(selectors.ticketsIndex.searchResult, 1);
            const result = await page.countElement(selectors.ticketsIndex.searchResult);

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

        it(`should now click on the search result to access to the ticket summary`, async() => {
            await page.waitForTextInElement(selectors.ticketsIndex.searchResult, 'Many Places');
            await page.waitToClick(selectors.ticketsIndex.searchResult);
            let url = await page.expectURL('/summary');

            expect(url).toBe(true);
        });

        it('should open the add stowaway dialog', async() => {
            await page.waitForFunction(() => {
                let element = document.querySelector('vn-ticket-descriptor');
                return element.$ctrl.canShowStowaway === true;
            });
            await page.waitToClick(selectors.ticketDescriptor.moreMenu);
            await page.waitToClick(selectors.ticketDescriptor.moreMenuAddStowaway);
            await page.wait(selectors.ticketDescriptor.addStowawayDialogFirstTicket);
            const isVisible = await page.isVisible(selectors.ticketDescriptor.addStowawayDialogFirstTicket);

            expect(isVisible).toBeTruthy();
        });

        it('should add a ticket as stowaway', async() => {
            await page.waitToClick(selectors.ticketDescriptor.addStowawayDialogFirstTicket);
            const result = await page.waitForLastSnackbar();

            expect(result).toEqual('Data saved!');
        });

        it(`should check the state of the stowaway ticket is embarked`, async() => {
            const state = await page.waitToGetProperty(selectors.ticketDescriptor.stateLabelValue, 'innerText');

            expect(state).toEqual('State Embarcando');
        });

        it(`should navigate back to the added ticket using the descriptors ship button`, async() => {
            await page.waitToClick(selectors.ticketDescriptor.shipButton);
            let url = await page.expectURL('#!/ticket/17/summary');

            expect(url).toBe(true);
        });

        it('should delete the stowaway', async() => {
            await page.waitForContentLoaded();
            await page.waitToClick(selectors.ticketDescriptor.moreMenu);
            await page.waitToClick(selectors.ticketDescriptor.moreMenuDeleteStowawayButton);
            await page.waitToClick(selectors.ticketDescriptor.acceptDeleteStowawayButton);
            const result = await page.waitForLastSnackbar();

            expect(result).toEqual('Data saved!');
        });

        it(`should confirm the ship buton doesn't exisist any more`, async() => {
            await page.waitForSelector(selectors.ticketDescriptor.shipButton, {hidden: true});
        });
    });

    describe('Make invoice', () => {
        it('should login as adminBoss role then search for a ticket', async() => {
            const invoiceableTicketId = '14';

            await page.loginAndModule('adminBoss', 'ticket');
            await page.accessToSearchResult(invoiceableTicketId);
            let url = await page.expectURL(`ticket/${invoiceableTicketId}/summary`);

            expect(url).toBe(true);
        });

        it(`should make sure the ticket doesn't have an invoiceOutFk yet`, async() => {
            const result = await page
                .waitToGetProperty(selectors.ticketSummary.invoiceOutRef, 'innerText');

            expect(result).toEqual('-');
        });

        it('should invoice the ticket using the descriptor more menu', async() => {
            await page.waitForContentLoaded();
            await page.waitToClick(selectors.ticketDescriptor.moreMenu);
            await page.waitToClick(selectors.ticketDescriptor.moreMenuMakeInvoice);
            await page.waitToClick(selectors.ticketDescriptor.acceptInvoiceOutButton);
            const result = await page.waitForLastSnackbar();

            expect(result).toEqual('Ticket invoiced');
        });

        it(`should make sure the ticket summary have an invoiceOutFk`, async() => {
            await page.waitForTextInElement(selectors.ticketSummary.invoiceOutRef, 'T4444445');
            const result = await page.waitToGetProperty(selectors.ticketSummary.invoiceOutRef, 'innerText');

            expect(result).toEqual('T4444445');
        });
    });
});