import selectors from '../../helpers/selectors.js';
import createNightmare from '../../helpers/nightmare';

describe('Item summary path', () => {
    const nightmare = createNightmare();

    beforeAll(() => {
        nightmare
            .loginAndModule('employee', 'item');
    });

    it('should search for an item', async() => {
        const result = await nightmare
            .clearInput(selectors.itemsIndex.searchItemInput)
            .write(selectors.itemsIndex.searchItemInput, 'Ranged weapon longbow 2m')
            .waitToClick(selectors.itemsIndex.searchButton)
            .waitForNumberOfElements(selectors.itemsIndex.searchResult, 1)
            .countElement(selectors.itemsIndex.searchResult);

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

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

        const isVisibleAfter = await nightmare
            .waitToClick(selectors.itemsIndex.searchResultPreviewButton)
            .isVisible(selectors.itemSummary.basicData);

        expect(isVisibleBefore).toBeFalsy();
        expect(isVisibleAfter).toBeTruthy();
    });

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

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

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

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

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

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

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

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

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

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

    it(`should close the summary popup`, async() => {
        const result = await nightmare
            .waitToClick(selectors.itemsIndex.closeItemSummaryPreview)
            .isVisible(selectors.itemSummary.basicData);

        expect(result).toBeFalsy();
    });

    it('should search for other item', async() => {
        const result = await nightmare
            .clearInput('vn-item-index vn-searchbar input')
            .waitToClick(selectors.itemsIndex.searchButton)
            .write(selectors.itemsIndex.searchItemInput, 'Melee weapon combat fist 15cm')
            .waitToClick(selectors.itemsIndex.searchButton)
            .waitForNumberOfElements(selectors.itemsIndex.searchResult, 1)
            .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() => {
        const isVisibleBefore = await nightmare
            .waitForTextInElement(selectors.itemsIndex.searchResult, 'Melee weapon combat fist 15cm')
            .isVisible(selectors.itemSummary.basicData);

        const isVisibleAfter = await nightmare
            .waitToClick(selectors.itemsIndex.searchResultPreviewButton)
            .isVisible(selectors.itemSummary.basicData);


        expect(isVisibleBefore).toBeFalsy();
        expect(isVisibleAfter).toBeTruthy();
    });

    it(`should now check the item summary preview shows fields from basic data`, async() => {
        const result = await nightmare
            .waitForTextInElement(selectors.itemSummary.basicData, 'Melee weapon combat fist 15cm')
            .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() => {
        const result = await nightmare
            .waitForTextInElement(selectors.itemSummary.tags, 'Silver')
            .waitToGetProperty(selectors.itemSummary.tags, 'innerText');

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

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

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

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

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

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

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

    it(`should now close the summary popup`, async() => {
        const result = await nightmare
            .waitToClick(selectors.itemsIndex.closeItemSummaryPreview)
            .isVisible(selectors.itemSummary.basicData);

        expect(result).toBeFalsy();
    });

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

        expect(url.hash).toContain('summary');
    });

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

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

    it(`should check the item summary shows fields from basic data section`, async() => {
        const result = await nightmare
            .waitForTextInElement(selectors.itemSummary.basicData, 'Melee weapon combat fist 15cm')
            .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 nightmare
            .waitToGetProperty(selectors.itemSummary.tags, 'innerText');

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

    it(`should check the item summary shows fields from niches section`, async() => {
        const result = await nightmare
            .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 nightmare
            .waitToGetProperty(selectors.itemSummary.botanical, 'innerText');

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

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

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