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

describe('Item summary 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 search for an item', async() => {
        await page.doSearch('Ranged weapon');
        const resultsCount = await page.countElement(selectors.itemsIndex.searchResult);

        await page.waitForTextInElement(selectors.itemsIndex.searchResult, 'Ranged weapon');
        await page.waitToClick(selectors.itemsIndex.firstResultPreviewButton);
        const isVisible = await page.isVisible(selectors.itemSummary.basicData);

        expect(resultsCount).toBe(3);
        expect(isVisible).toBeTruthy();
    });

    it(`should check the item summary preview shows fields from basic data`, async() => {
        await page.waitForTextInElement(selectors.itemSummary.basicData, 'Ranged weapon longbow 2m');
        const result = await page.waitToGetProperty(selectors.itemSummary.basicData, 'innerText');

        expect(result).toContain('Ranged weapon longbow 2m');
    });

    it(`should check the item summary preview shows fields from tags`, async() => {
        await page.waitForTextInElement(selectors.itemSummary.tags, 'Brown');
        const result = await page.waitToGetProperty(selectors.itemSummary.tags, 'innerText');

        expect(result).toContain('Brown');
    });

    it(`should check the item summary preview shows fields from botanical`, async() => {
        await page.waitForTextInElement(selectors.itemSummary.botanical, 'Abelia');
        const result = await page.waitToGetProperty(selectors.itemSummary.botanical, 'innerText');

        expect(result).toContain('Abelia');
    });

    it(`should check the item summary preview shows fields from barcode`, async() => {
        await page.waitForTextInElement(selectors.itemSummary.barcode, '1');
        const result = await page.waitToGetProperty(selectors.itemSummary.barcode, 'innerText');

        expect(result).toContain('1');
    });

    it(`should close the summary popup`, async() => {
        await page.closePopup();
        await page.waitForSelector(selectors.itemSummary.basicData, {hidden: true});
    });

    it('should search for other item', async() => {
        await page.doSearch('Melee Reinforced');
        const resultsCount = await page.countElement(selectors.itemsIndex.searchResult);

        await page.waitToClick(selectors.itemsIndex.firstResultPreviewButton);
        await page.waitForSelector(selectors.itemSummary.basicData, {visible: true});

        expect(resultsCount).toBe(2);
    });

    it(`should now check the item summary preview shows fields from basic data`, async() => {
        await page.waitForTextInElement(selectors.itemSummary.basicData, 'Melee Reinforced weapon combat fist 15cm');
        const result = await page.waitToGetProperty(selectors.itemSummary.basicData, 'innerText');

        expect(result).toContain('Melee Reinforced weapon combat fist 15cm');
    });

    it(`should now check the item summary preview shows fields from tags`, async() => {
        await page.waitForTextInElement(selectors.itemSummary.tags, 'Silver');
        const result = await page.waitToGetProperty(selectors.itemSummary.tags, 'innerText');

        expect(result).toContain('Silver');
    });

    it(`should now check the item summary preview shows fields from botanical`, async() => {
        await page.waitForTextInElement(selectors.itemSummary.botanical, '-');
        const result = await page.waitToGetProperty(selectors.itemSummary.botanical, 'innerText');

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

    it(`should now close the summary popup`, async() => {
        await page.closePopup();
        await page.waitForSelector(selectors.itemSummary.basicData, {hidden: true});
    });

    it(`should navigate to one of the items detailed section`, async() => {
        await page.accessToSearchResult('Melee weapon combat fist 15cm');
        await page.waitForState('item.card.summary');
    });

    it(`should check the descritor edit button is not visible for employee`, async() => {
        const visibleButton = await page.isVisible(selectors.itemDescriptor.editButton);

        expect(visibleButton).toBeFalsy();
    });

    it(`should check the item summary shows fields from basic data section`, async() => {
        await page.waitForTextInElement(selectors.itemSummary.basicData, 'Melee weapon combat fist 15cm');
        const result = await page.waitToGetProperty(selectors.itemSummary.basicData, 'innerText');

        expect(result).toContain('Melee weapon combat fist 15cm');
    });

    it(`should check the item summary shows fields from tags section`, async() => {
        const result = await page.waitToGetProperty(selectors.itemSummary.tags, 'innerText');

        expect(result).toContain('Silver');
    });

    it(`should check the item summary shows fields from botanical section`, async() => {
        const result = await page.waitToGetProperty(selectors.itemSummary.botanical, 'innerText');

        expect(result).toContain('procera');
    });

    it(`should check the item summary shows fields from barcodes section`, async() => {
        const result = await page.waitToGetProperty(selectors.itemSummary.barcode, 'innerText');

        expect(result).toContain('4');
    });
});