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

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

    beforeAll(async() => {
        browser = await getBrowser();
        page = browser.page;
        await page.loginAndModule('employee', 'ticket');
        await page.accessToSearchResult('13');
        await page.accessToSection('ticket.card.sale');
    });

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

    it('should confirm the first ticket sale contains the colour tag', async() => {
        const value = await page
            .waitToGetProperty(selectors.ticketSales.firstSaleColour, 'innerText');

        expect(value).toContain('Black');
    });

    it('should confirm the first sale contains the price', async() => {
        const value = await page
            .waitToGetProperty(selectors.ticketSales.firstSalePrice, 'innerText');

        expect(value).toContain('1.72');
    });

    it('should confirm the first sale contains the discount', async() => {
        const value = await page
            .waitToGetProperty(selectors.ticketSales.firstSaleDiscount, 'innerText');

        expect(value).toContain('0.00%');
    });

    it('should confirm the first sale contains the total import', async() => {
        const value = await page
            .waitToGetProperty(selectors.ticketSales.firstSaleImport, 'innerText');

        expect(value).toContain('34.40');
    });

    it('should add an empty item to the sale list', async() => {
        await page.waitToClick(selectors.ticketSales.newItemButton);
        const sales = await page
            .countElement(selectors.ticketSales.saleLine);

        expect(sales).toEqual(2);
    });

    it('should select a valid item to be added as the second item in the sales list', async() => {
        let searchValue = 'Melee weapon heavy shield 1x0.5m';
        await page.autocompleteSearch(selectors.ticketSales.secondSaleIdAutocomplete, searchValue);
        await page.waitToClick(selectors.ticketSales.secondSaleQuantityCell);
        await page.type(selectors.ticketSales.secondSaleQuantity, '1');
        await page.keyboard.press('Enter');
        const message = await page.waitForSnackbar();

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

    it('should update the description of the new sale', async() => {
        await page.click(selectors.ticketSales.secondSaleConceptCell);
        await page.write(selectors.ticketSales.secondSaleConceptInput, 'Aegis of Valor');
        await page.keyboard.press('Enter');
        const message = await page.waitForSnackbar();

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

    it('should add a third empty item to the sale list', async() => {
        await page.waitToClick(selectors.ticketSales.newItemButton);
        await page.waitForNumberOfElements(selectors.ticketSales.saleLine, 3);
        const sales = await page.countElement(selectors.ticketSales.saleLine);

        expect(sales).toEqual(3);
    });

    it('should select the 2nd and 3th item and delete both', async() => {
        await page.waitToClick(selectors.ticketSales.secondSaleCheckbox);
        await page.waitToClick(selectors.ticketSales.thirdSaleCheckbox);
        await page.waitToClick(selectors.ticketSales.deleteSaleButton);
        await page.waitToClick(selectors.globalItems.acceptButton);
        const message = await page.waitForSnackbar();

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

    it(`should verify there's only 1 single line remaining`, async() => {
        const sales = await page.countElement(selectors.ticketSales.saleLine);

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