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

describe('Item regularize path', () => {
    let browser;
    let page;
    beforeAll(async() => {
        browser = await getBrowser();
        page = browser.page;
        await page.loginAndModule('employee', 'item');
    });

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

    it('should edit the user local warehouse', async() => {
        await page.waitForSpinnerLoad();
        await page.waitToClick(selectors.globalItems.userMenuButton);
        await page.autocompleteSearch(selectors.globalItems.userLocalWarehouse, 'Warehouse Four');
        const message = await page.waitForSnackbar();

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

    it('should check the local settings were saved', async() => {
        const userLocalWarehouse = await page
            .waitToGetProperty(selectors.globalItems.userLocalWarehouse, 'value');

        await page.closePopup();

        expect(userLocalWarehouse).toContain('Warehouse Four');
    });

    it('should search for a specific item', async() => {
        await page.accessToSearchResult('Ranged weapon pistol 9mm');
        await page.waitForState('item.card.summary');
    });

    it('should open the regularize dialog and check the warehouse matches the local user settings', async() => {
        await page.waitToClick(selectors.itemDescriptor.moreMenu);
        await page.waitToClick(selectors.itemDescriptor.moreMenuRegularizeButton);
        const result = await page.waitToGetProperty(selectors.itemDescriptor.regularizeWarehouse, 'value');

        expect(result).toEqual('Warehouse Four');
    });

    it('should regularize the item', async() => {
        await page.write(selectors.itemDescriptor.regularizeQuantity, '100');
        await page.autocompleteSearch(selectors.itemDescriptor.regularizeWarehouse, 'Warehouse One');
        await page.waitToClick(selectors.itemDescriptor.regularizeSaveButton);
        const message = await page.waitForSnackbar();

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

    it('should click on the Tickets button of the top bar menu', async() => {
        await page.waitToClick(selectors.globalItems.applicationsMenuButton);
        await page.waitForSelector(selectors.globalItems.applicationsMenuVisible);
        await Promise.all([
            page.waitForNavigation({waitUntil: ['load', 'networkidle0', 'domcontentloaded']}),
            page.waitToClick(selectors.globalItems.ticketsButton)
        ]);
        await page.waitForState('ticket.index');
    });

    it('should clear the user local settings now', async() => {
        await page.waitToClick(selectors.globalItems.userMenuButton);
        await page.waitForContentLoaded();
        await page.clearInput(selectors.globalItems.userConfigFirstAutocomplete);
        const message = await page.waitForSnackbar();

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

    it('should search for the ticket with alias missing', async() => {
        await page.keyboard.press('Escape');
        await page.accessToSearchResult('missing');
        await page.waitForState('ticket.card.summary');
    });

    it(`should check the ticket sale quantity is showing a negative value`, async() => {
        await page.waitForTextInElement(selectors.ticketSummary.firstSaleQuantity, '-100');
        const result = await page
            .waitToGetProperty(selectors.ticketSummary.firstSaleQuantity, 'innerText');

        expect(result).toContain('-100');
    });

    it(`should check the ticket sale discount is 100%`, async() => {
        const result = await page
            .waitToGetProperty(selectors.ticketSummary.firstSaleDiscount, 'innerText');

        expect(result).toContain('100 %');
    });

    it('should now click on the Items button of the top bar menu', async() => {
        await page.waitToClick(selectors.globalItems.applicationsMenuButton);
        await page.waitForSelector(selectors.globalItems.applicationsMenuVisible);
        await page.waitToClick(selectors.globalItems.itemsButton);
        await page.waitForState('item.index');
    });

    it('should search for the item once again', async() => {
        await page.accessToSearchResult('Ranged weapon pistol 9mm');
        await page.waitForState('item.card.summary');
    });

    it('should regularize the item once more', async() => {
        await page.waitToClick(selectors.itemDescriptor.moreMenu);
        await page.waitToClick(selectors.itemDescriptor.moreMenuRegularizeButton);
        await page.write(selectors.itemDescriptor.regularizeQuantity, '100');
        await page.autocompleteSearch(selectors.itemDescriptor.regularizeWarehouse, 'Warehouse One');
        await page.waitToClick(selectors.itemDescriptor.regularizeSaveButton);
        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 Promise.all([
            page.waitForNavigation({waitUntil: ['load', 'networkidle0', 'domcontentloaded']}),
            page.waitToClick(selectors.globalItems.ticketsButton)
        ]);
        await page.waitForState('ticket.index');
    });

    it('should search for the ticket with id 25 once again', async() => {
        await page.accessToSearchResult('25');
        await page.waitForState('ticket.card.summary');
    });

    it(`should check the ticket contains now two sales`, async() => {
        await page.waitForTextInElement(selectors.ticketSummary.firstSaleQuantity, '-100');
        const result = await page.countElement(selectors.ticketSummary.sale);

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