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('buyer', 'ticket');
        await page.accessToSection('ticket.weekly.index');
    });

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

    it('should count the amount of tickets in the turns section', async() => {
        const result = await page.countElement(selectors.ticketsIndex.weeklyTicket);

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

    it('should go back to the ticket index then search and access a ticket summary', async() => {
        await page.accessToSection('ticket.index');
        await page.accessToSearchResult('11');
    });

    it('should add the ticket to thursday turn using the descriptor more menu', async() => {
        await page.waitToClick(selectors.ticketDescriptor.moreMenu);
        await page.waitToClick(selectors.ticketDescriptor.moreMenuAddToTurn);
        await page.waitToClick(selectors.ticketDescriptor.thursdayButton);
        const message = await page.waitForSnackbar();

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

    it('should again click on the Tickets button of the top bar menu', 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 confirm the ticket 11 was added to thursday', async() => {
        await page.accessToSection('ticket.weekly.index');
        const result = await page.waitToGetProperty(selectors.ticketsIndex.sixthWeeklyTicket, 'value');

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

    it('should click on the Tickets button of the top bar menu once more', 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 now search for the ticket 11', async() => {
        await page.accessToSearchResult('11');
        await page.waitForState('ticket.card.summary');
    });

    it('should add the ticket to saturday turn using the descriptor more menu', async() => {
        await page.waitToClick(selectors.ticketDescriptor.moreMenu);
        await page.waitToClick(selectors.ticketDescriptor.moreMenuAddToTurn);
        await page.waitToClick(selectors.ticketDescriptor.saturdayButton);
        const message = await page.waitForSnackbar();

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

    it('should click on the Tickets button of the top bar menu once again', 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 confirm the ticket 11 was added on saturday', async() => {
        await page.accessToSection('ticket.weekly.index');
        const result = await page.waitToGetProperty(selectors.ticketsIndex.sixthWeeklyTicket, 'value');

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

    it('should now search for the weekly ticket 11', async() => {
        await page.doSearch('11');
        const nResults = await page.countElement(selectors.ticketsIndex.searchWeeklyResult);

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

    it('should delete the weekly ticket 11', async() => {
        await page.waitToClick(selectors.ticketsIndex.firstWeeklyTicketDeleteIcon);
        await page.waitToClick(selectors.ticketsIndex.acceptDeleteTurn);
        const message = await page.waitForSnackbar();

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

    it('should confirm the sixth weekly ticket was deleted', async() => {
        await page.doSearch();
        const nResults = await page.countElement(selectors.ticketsIndex.searchWeeklyResult);

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

    it('should update the agency then remove it afterwards', async() => {
        await page.autocompleteSearch(selectors.ticketsIndex.firstWeeklyTicketAgency, 'Silla247');
        let message = await page.waitForSnackbar();

        expect(message.text).toContain('Data saved!');

        await page.clearInput(selectors.ticketsIndex.firstWeeklyTicketAgency);
        message = await page.waitForSnackbar();

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