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.clearInput(selectors.itemsIndex.searchItemInput);
        await page.write(selectors.itemsIndex.searchItemInput, 'Ranged weapon longbow 2m');
        await page.waitToClick(selectors.itemsIndex.searchButton);
        await page.waitForNumberOfElements(selectors.itemsIndex.searchResult, 1);
        const result = await page.countElement(selectors.itemsIndex.searchResult);

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

    it(`should click on the search result summary button to open the item summary popup`, async() => {
        await page.waitForTextInElement(selectors.itemsIndex.searchResult, 'Ranged weapon longbow 2m');
        await page.waitToClick(selectors.itemsIndex.searchResultPreviewButton);
        const isVisible = await page.isVisible(selectors.itemSummary.basicData);

        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 niche`, async() => {
        await page.waitForTextInElement(selectors.itemSummary.niche, 'A1');
        const result = await page.waitToGetProperty(selectors.itemSummary.niche, 'innerText');

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

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

        expect(result).toContain('Hedera helix');
    });

    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.keyboard.press('Escape');
        await page.waitUntilNotPresent(selectors.itemSummary.basicData);
        await page.waitFor(selectors.itemSummary.basicData, {hidden: true});
    });

    it('should search for other item', async() => {
        await page.clearInput('vn-searchbar');
        await page.waitToClick(selectors.itemsIndex.searchButton);
        await page.write(selectors.itemsIndex.searchItemInput, 'Melee weapon combat fist 15cm');
        await page.waitToClick(selectors.itemsIndex.searchButton);
        await page.waitForNumberOfElements(selectors.itemsIndex.searchResult, 1);
        const result = await page.countElement(selectors.itemsIndex.searchResult);

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

    it(`should now click on the search result summary button to open the item summary popup`, async() => {
        await page.waitToClick(selectors.itemsIndex.searchResultPreviewButton);
        await page.waitForSelector(selectors.itemSummary.basicData, {visible: true});
    });

    it(`should now check the item summary preview shows fields from basic data`, 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 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 niche`, async() => {
        await page.waitForTextInElement(selectors.itemSummary.niche, 'A4');
        const result = await page.waitToGetProperty(selectors.itemSummary.niche, 'innerText');

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

    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 check the item summary preview shows fields from barcode`, async() => {
        await page.waitForTextInElement(selectors.itemSummary.barcode, '4');
        const result = await page.waitToGetProperty(selectors.itemSummary.barcode, 'innerText');

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

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

    it(`should navigate to the one of the items detailed section`, async() => {
        await page.waitToClick(selectors.itemsIndex.searchResult);
        await page.waitForURL('summary');
        const url = await page.parsedUrl();

        expect(url.hash).toContain('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 niches section`, async() => {
        const result = await page.waitToGetProperty(selectors.itemSummary.niche, 'innerText');

        expect(result).toContain('One A4');
    });

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

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

    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');
    });
});