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

// #1186 repearar e2e ticket.sale, item.regularize.
xdescribe('Ticket Edit sale path', () => {
    const nightmare = createNightmare();

    beforeAll(() => {
        nightmare
            .loginAndModule('salesPerson', 'ticket')
            .accessToSearchResult('16')
            .accessToSection('ticket.card.sale');
    });

    it(`should click on the second claim id to navigate over there`, async() => {
        const url = await nightmare
            .waitToClick(selectors.ticketSales.secondSaleClaimIcon)
            .wait(selectors.claimBasicData.claimStateAutocomplete)
            .parsedUrl();

        expect(url.hash).toEqual('#!/claim/2/basic-data');
    });


    it('should now click on the Tickets button of the top bar menu', async() => {
        const url = await nightmare
            .waitToClick(selectors.globalItems.applicationsMenuButton)
            .wait(selectors.globalItems.applicationsMenuVisible)
            .waitToClick(selectors.globalItems.ticketsButton)
            .wait(selectors.ticketsIndex.searchTicketInput)
            .parsedUrl();

        expect(url.hash).toEqual('#!/ticket/index');
    });

    it('should again search for a specific ticket', async() => {
        const result = await nightmare
            .write(selectors.ticketsIndex.searchTicketInput, '16')
            .waitToClick(selectors.ticketsIndex.searchButton)
            .waitForNumberOfElements(selectors.ticketsIndex.searchResult, 1)
            .countElement(selectors.ticketsIndex.searchResult);

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

    it(`should now click on the search result to access to the ticket Sale`, async() => {
        const url = await nightmare
            .waitForTextInElement(selectors.ticketsIndex.searchResult, 'address 21')
            .waitToClick(selectors.ticketsIndex.searchResult)
            .waitToClick(selectors.ticketSales.saleButton)
            .waitForURL('/sale')
            .parsedUrl();

        expect(url.hash).toContain('/sale');
    });

    it(`should check the zoomed image isnt present`, async() => {
        const result = await nightmare
            .countElement(selectors.ticketSales.firstSaleZoomedImage);

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

    it(`should click on the thumbnail image of the 1st sale and see the zoomed image`, async() => {
        const result = await nightmare
            .clickIfVisible(selectors.ticketSales.firstSaleThumbnailImage)
            .countElement(selectors.ticketSales.firstSaleZoomedImage);

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

    it(`should click on the zoomed image to close it`, async() => {
        const result = await nightmare
            .wait(1000)
            .clickIfVisible(selectors.ticketSales.firstSaleZoomedImage)
            .countElement(selectors.ticketSales.firstSaleZoomedImage);

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

    it(`should confirm the item descriptor insnt visible yet`, async() => {
        const visible = await nightmare
            .isVisible(selectors.ticketSales.saleDescriptorPopover);

        expect(visible).toBeFalsy();
    });

    it(`should click on the first sale ID making the item descriptor visible`, async() => {
        const visible = await nightmare
            .waitToClick(selectors.ticketSales.firstSaleID)
            .waitImgLoad(selectors.ticketSales.firstSaleDescriptorImage)
            .isVisible(selectors.ticketSales.saleDescriptorPopover);

        expect(visible).toBeTruthy();
    });

    it(`should click on the descriptor image of the 1st sale and see the zoomed image`, async() => {
        const result = await nightmare
            .clickIfVisible('vn-item-descriptor img')
            .countElement(selectors.ticketSales.firstSaleZoomedImage);

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

    it(`should now click on the zoomed image to close it`, async() => {
        const result = await nightmare
            .clickIfVisible(selectors.ticketSales.firstSaleZoomedImage)
            .countElement(selectors.ticketSales.firstSaleZoomedImage);

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

    it(`should click on the summary icon of the item-descriptor to access to the item summary`, async() => {
        const url = await nightmare
            .waitToClick(selectors.ticketSales.saleDescriptorPopoverSummaryButton)
            .waitForURL('/summary')
            .parsedUrl();

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

    it('should return to ticket sales section', async() => {
        const url = await nightmare
            .waitToClick(selectors.globalItems.applicationsMenuButton)
            .wait(selectors.globalItems.applicationsMenuVisible)
            .waitToClick(selectors.globalItems.ticketsButton)
            .write(selectors.ticketsIndex.searchTicketInput, '16')
            .waitToClick(selectors.ticketsIndex.searchButton)
            .waitForNumberOfElements(selectors.ticketsIndex.searchResult, 1)
            .waitForTextInElement(selectors.ticketsIndex.searchResult, 'address 21')
            .waitToClick(selectors.ticketsIndex.searchResult)
            .waitToClick(selectors.ticketSales.saleButton)
            .waitForURL('/sale')
            .parsedUrl();

        expect(url.hash).toContain('/sale');
    });

    it('should try to add a higher quantity value and then receive an error', async() => {
        const result = await nightmare
            .waitToFocus(selectors.ticketSales.firstSaleQuantityCell)
            .write(selectors.ticketSales.firstSaleQuantity, '9\u000d')
            .waitForLastSnackbar();

        expect(result).toEqual('The new quantity should be smaller than the old one');
    });

    it('should remove 1 from quantity', async() => {
        const result = await nightmare
            .waitToFocus(selectors.ticketSales.firstSaleQuantityCell)
            .write(selectors.ticketSales.firstSaleQuantity, '4\u000d')
            .waitForLastSnackbar();

        expect(result).toEqual('Data saved!');
    });

    it('should update the price', async() => {
        const result = await nightmare
            .waitToClick(selectors.ticketSales.firstSalePrice)
            .write(selectors.ticketSales.firstSalePriceInput, '5\u000d')
            .waitForLastSnackbar();

        expect(result).toEqual('Data saved!');
    });

    it('should confirm the price have been updated', async() => {
        const result = await nightmare
            .waitToGetProperty(selectors.ticketSales.firstSalePrice, 'innerText');

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

    it('should confirm the total price for that item have been updated', async() => {
        const result = await nightmare
            .waitToGetProperty(selectors.ticketSales.firstSaleImport, 'innerText');

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

    it('should update the discount', async() => {
        const result = await nightmare
            .waitToClick(selectors.ticketSales.firstSaleDiscount)
            .write(selectors.ticketSales.firstSaleDiscountInput, '50\u000d')
            // .write('body', '\u000d') // simulates enter
            .waitForLastSnackbar();

        expect(result).toEqual('Data saved!');
    });

    it('should confirm the discount have been updated', async() => {
        const result = await nightmare
            .waitForTextInElement(selectors.ticketSales.firstSaleDiscount, '50 %')
            .waitToGetProperty(selectors.ticketSales.firstSaleDiscount, 'innerText');

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

    it('should confirm the total import for that item have been updated', async() => {
        const result = await nightmare
            .waitForTextInElement(selectors.ticketSales.firstSaleImport, '10.00')
            .waitToGetProperty(selectors.ticketSales.firstSaleImport, 'innerText');

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

    it('should select the third sale and create a claim of it', async() => {
        const url = await nightmare
            .waitToClick(selectors.ticketSales.thirdSaleCheckbox)
            .waitToClick(selectors.ticketSales.moreMenuButton)
            .waitToClick(selectors.ticketSales.moreMenuCreateClaim)
            .wait(selectors.claimBasicData.claimStateAutocomplete)
            .parsedUrl();

        expect(url.hash).toEqual('#!/claim/5/basic-data');
    });

    it('should click on the Claims button of the top bar menu', async() => {
        const url = await nightmare
            .waitToClick(selectors.globalItems.applicationsMenuButton)
            .wait(selectors.globalItems.applicationsMenuVisible)
            .waitToClick(selectors.globalItems.claimsButton)
            .wait(selectors.claimsIndex.searchClaimInput)
            .parsedUrl();

        expect(url.hash).toEqual('#!/claim/index');
    });

    it('should search for the claim with id 4', async() => {
        const result = await nightmare
            .write(selectors.claimsIndex.searchClaimInput, 4)
            .waitToClick(selectors.claimsIndex.searchButton)
            .waitForNumberOfElements(selectors.claimsIndex.searchResult, 1)
            .countElement(selectors.claimsIndex.searchResult);

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

    it('should click the Tickets button of the top bar menu', async() => {
        const url = await nightmare
            .waitToClick(selectors.globalItems.applicationsMenuButton)
            .wait(selectors.globalItems.applicationsMenuVisible)
            .waitToClick(selectors.globalItems.ticketsButton)
            .wait(selectors.ticketsIndex.searchTicketInput)
            .parsedUrl();

        expect(url.hash).toEqual('#!/ticket/index');
    });

    it('should search the ticket', async() => {
        const result = await nightmare
            .write(selectors.ticketsIndex.searchTicketInput, '16')
            .waitToClick(selectors.ticketsIndex.searchButton)
            .waitForNumberOfElements(selectors.ticketsIndex.searchResult, 1)
            .countElement(selectors.ticketsIndex.searchResult);

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

    it(`should click on the result to access to the ticket Sale`, async() => {
        const url = await nightmare
            .waitForTextInElement(selectors.ticketsIndex.searchResult, 'address 21')
            .waitToClick(selectors.ticketsIndex.searchResult)
            .waitToClick(selectors.ticketSales.saleButton)
            .waitForURL('/sale')
            .parsedUrl();

        expect(url.hash).toContain('/sale');
    });

    it('should select the third sale and delete it', async() => {
        const result = await nightmare
            .waitToClick(selectors.ticketSales.thirdSaleCheckbox)
            .waitToClick(selectors.ticketSales.deleteSaleButton)
            .waitToClick(selectors.ticketSales.acceptDeleteLineButton)
            .waitForLastSnackbar();

        expect(result).toEqual('Data saved!');
    });

    it(`should confirm the third sale was deleted`, async() => {
        const result = await nightmare
            .countElement(selectors.ticketSales.saleLine);

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

    it('should select the third sale and attempt to send it to a frozen client ticket', async() => {
        const result = await nightmare
            .waitToClick(selectors.ticketSales.thirdSaleCheckbox)
            .waitToClick(selectors.ticketSales.transferSaleButton)
            .write(selectors.ticketSales.moveToTicketInput, 2)
            .waitToClick(selectors.ticketSales.moveToTicketButton)
            .waitForLastSnackbar();

        expect(result).toEqual(`The sales of this ticket can't be modified`);
    });

    it('should transfer the third sale to a valid ticket', async() => {
        const result = await nightmare
            .waitToClick(selectors.ticketSales.moveToTicketInputClearButton)
            .write(selectors.ticketSales.moveToTicketInput, 12)
            .waitToClick(selectors.ticketSales.moveToTicketButton)
            .waitForURL('ticket/12/sale')
            .parsedUrl();

        expect(result.hash).toContain(`ticket/12/sale`);
    });

    it('should confirm the transfered line is the correct one', async() => {
        const result = await nightmare
            .wait(selectors.ticketSales.firstSaleText)
            .waitToGetProperty(selectors.ticketSales.firstSaleText, 'innerText');

        expect(result).toContain(`Melee weapon heavy shield 1x0.5m`);
    });

    it('should go back to the original ticket sales section', async() => {
        const url = await nightmare
            .waitToClick(selectors.itemsIndex.goBackToModuleIndexButton)
            .write(selectors.ticketsIndex.searchTicketInput, '16')
            .waitToClick(selectors.ticketsIndex.searchButton)
            .waitForNumberOfElements(selectors.ticketsIndex.searchResult, 1)
            .waitForTextInElement(selectors.ticketsIndex.searchResult, 'address 21')
            .waitToClick(selectors.ticketsIndex.searchResult)
            .waitToClick(selectors.ticketSales.saleButton)
            .waitForURL('/sale')
            .parsedUrl();

        expect(url.hash).toContain('/sale');
    });

    it(`should confirm the original ticket has only two lines now`, async() => {
        const result = await nightmare
            .wait(selectors.ticketSales.saleLine)
            .countElement(selectors.ticketSales.saleLine);

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

    it('should go back to the receiver ticket sales section', async() => {
        const url = await nightmare
            .waitToClick(selectors.itemsIndex.goBackToModuleIndexButton)
            .write(selectors.ticketsIndex.searchTicketInput, 'id:12')
            .waitToClick(selectors.ticketsIndex.searchButton)
            .waitForNumberOfElements(selectors.ticketsIndex.searchResult, 1)
            .waitForTextInElement(selectors.ticketsIndex.searchResult, 'address 21')
            .waitToClick(selectors.ticketsIndex.searchResult)
            .waitToClick(selectors.ticketSales.saleButton)
            .waitForURL('/sale')
            .parsedUrl();

        expect(url.hash).toContain('/sale');
    });

    it('should transfer the sale back to the original ticket', async() => {
        const result = await nightmare
            .waitToClick(selectors.ticketSales.firstSaleCheckbox)
            .waitToClick(selectors.ticketSales.transferSaleButton)
            .write(selectors.ticketSales.moveToTicketInput, '16')
            .waitToClick(selectors.ticketSales.moveToTicketButton)
            .waitForURL('ticket/16/sale')
            .parsedUrl();

        expect(result.hash).toContain(`ticket/16/sale`);
    });

    it('should confirm the original ticket received the line', async() => {
        const result = await nightmare
            // .waitForNumberOfElements(selectors.ticketSales.saleLine, 3)
            .countElement(selectors.ticketSales.saleLine);

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

    it('should now go back to the original ticket sales section', async() => {
        const url = await nightmare
            .waitToClick(selectors.itemsIndex.goBackToModuleIndexButton)
            .write(selectors.ticketsIndex.searchTicketInput, 'id:8')
            .waitToClick(selectors.ticketsIndex.searchButton)
            .waitForNumberOfElements(selectors.ticketsIndex.searchResult, 1)
            .waitForTextInElement(selectors.ticketsIndex.searchResult, 'address 24')
            .waitToClick(selectors.ticketsIndex.searchResult)
            .waitToClick(selectors.ticketSales.saleButton)
            .waitForURL('/sale')
            .parsedUrl();

        expect(url.hash).toContain('/sale');
    });

    it('should select the second and third sale and tranfer them to a new ticket then get to the ticket index', async() => {
        const url = await nightmare
            .waitToClick(selectors.ticketSales.firstSaleCheckbox)
            .waitToClick(selectors.ticketSales.transferSaleButton)
            .waitToClick(selectors.ticketSales.moveToNewTicketButton)
            .resetLogin()
            .waitForLogin('salesPerson')
            .waitToClick(selectors.globalItems.applicationsMenuButton)
            .wait(selectors.globalItems.applicationsMenuVisible)
            .waitToClick(selectors.globalItems.ticketsButton)
            .wait(selectors.ticketsIndex.searchTicketInput)
            .parsedUrl();

        expect(url.hash).toEqual('#!/ticket/index');
    });

    it('should search for a specific created ticket', async() => {
        const result = await nightmare
            .write(selectors.ticketsIndex.searchTicketInput, 'nickname:(address 24) stateFk:2')
            .waitToClick(selectors.ticketsIndex.searchButton)
            .waitForNumberOfElements(selectors.ticketsIndex.searchResult, 1)
            .countElement(selectors.ticketsIndex.searchResult);

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

    it(`should click on the search result to access to the ticket Sale once more`, async() => {
        const url = await nightmare
            .waitForTextInElement(selectors.ticketsIndex.searchResult, 'address 24')
            .waitToClick(selectors.ticketsIndex.searchResult)
            .waitToClick(selectors.ticketSales.saleButton)
            .waitForURL('/sale')
            .parsedUrl();

        expect(url.hash).toContain('/sale');
    });

    it('should confirm the new ticket received both lines', async() => {
        const result = await nightmare
            .countElement(selectors.ticketSales.saleLine);

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

    it('should check the first sale reserved icon isnt visible', async() => {
        const result = await nightmare
            .isVisible(selectors.ticketSales.firstSaleReservedIcon);

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

    it('should mark the first sale as reserved', async() => {
        const result = await nightmare
            .waitToClick(selectors.ticketSales.firstSaleCheckbox)
            .waitToClick(selectors.ticketSales.moreMenuButton)
            .waitToClick(selectors.ticketSales.moreMenuReseveOption)
            .waitForClassNotPresent(selectors.ticketSales.firstSaleReservedIcon, 'ng-hide')
            .isVisible(selectors.ticketSales.firstSaleReservedIcon);

        expect(result).toBeTruthy();
    });

    it('should unmark the first sale as reserved', async() => {
        const result = await nightmare
            .waitToClick(selectors.ticketSales.firstSaleCheckbox)
            .waitToClick(selectors.ticketSales.moreMenuButton)
            .waitToClick(selectors.ticketSales.moreMenuUnmarkResevedOption)
            .waitForClassPresent(selectors.ticketSales.firstSaleReservedIcon, 'ng-hide')
            .isVisible(selectors.ticketSales.firstSaleReservedIcon);

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

    it('should update all sales discount', async() => {
        const result = await nightmare
            .waitToClick(selectors.ticketSales.selectAllSalesCheckbox)
            .waitToClick(selectors.ticketSales.moreMenuButton)
            .waitToClick(selectors.ticketSales.moreMenuUpdateDiscount)
            .write(selectors.ticketSales.moreMenuUpdateDiscountInput, 100)
            .write('body', '\u000d')
            .waitForTextInElement(selectors.ticketSales.totalImport, '0.00')
            .waitToGetProperty(selectors.ticketSales.totalImport, 'innerText');

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

    it('should log in as Production role and go to the ticket index', async() => {
        const url = await nightmare
            .waitForLogin('production')
            .waitToClick(selectors.globalItems.applicationsMenuButton)
            .wait(selectors.globalItems.applicationsMenuVisible)
            .waitToClick(selectors.globalItems.ticketsButton)
            .wait(selectors.ticketsIndex.searchTicketInput)
            .parsedUrl();

        expect(url.hash).toEqual('#!/ticket/index');
    });

    it('should now search for a specific ticket', async() => {
        const result = await nightmare
            .write(selectors.ticketsIndex.searchTicketInput, '16')
            .waitToClick(selectors.ticketsIndex.searchButton)
            .waitForNumberOfElements(selectors.ticketsIndex.searchResult, 1)
            .countElement(selectors.ticketsIndex.searchResult);

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

    it(`should now click on the search result to access to the ticket Tracking`, async() => {
        const url = await nightmare
            .waitForTextInElement(selectors.ticketsIndex.searchResult, 'address 21')
            .waitToClick(selectors.ticketsIndex.searchResult)
            .waitToClick(selectors.ticketTracking.trackingButton)
            .waitForURL('/tracking/index')
            .parsedUrl();

        expect(url.hash).toContain('/tracking/index');
    });

    it(`should click on the edit ticket tracking state button`, async() => {
        const url = await nightmare
            .waitToClick(selectors.ticketTracking.createStateButton)
            .waitForURL('/tracking/edit')
            .parsedUrl();

        expect(url.hash).toContain('/tracking/edit');
    });

    it(`should set the state of the ticket to preparation`, async() => {
        const url = await nightmare
            .autocompleteSearch(selectors.ticketTracking.stateAutocomplete, 'PreparaciĆ³n')
            .waitToClick(selectors.ticketTracking.saveButton)
            .waitForURL('/tracking/index')
            .parsedUrl();

        expect(url.hash).toContain('/tracking/index');
    });

    it(`should click on the ticket Sale menu button`, async() => {
        const url = await nightmare
            .waitToClick(selectors.ticketSales.saleButton)
            .waitForURL('/sale')
            .parsedUrl();

        expect(url.hash).toContain('/sale');
    });

    describe('when state is preparation and loged as Production', () => {
        it(`should not be able to edit the sale price`, async() => {
            const result = await nightmare
                .wait(selectors.ticketSales.firstSaleID)
                .exists(selectors.ticketSales.firstSalePrice);

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

        it(`should not be able to edit the sale discount`, async() => {
            const result = await nightmare
                .waitToClick(selectors.ticketSales.firstSaleID)
                .exists(selectors.ticketSales.firstSaleDiscount);

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

        it(`should not be able to edit the sale state`, async() => {
            const result = await nightmare
                .waitToClick(selectors.ticketSales.stateMenuButton)
                .exists(selectors.ticketSales.stateMenuOptions);

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

        it('should log in as salesPerson role and go to the ticket index', async() => {
            const url = await nightmare
                .waitForLogin('salesPerson')
                .waitToClick(selectors.globalItems.applicationsMenuButton)
                .wait(selectors.globalItems.applicationsMenuVisible)
                .waitToClick(selectors.globalItems.ticketsButton)
                .wait(selectors.ticketsIndex.searchTicketInput)
                .parsedUrl();

            expect(url.hash).toEqual('#!/ticket/index');
        });

        it('should once again search for a specific ticket', async() => {
            const result = await nightmare
                .write(selectors.ticketsIndex.searchTicketInput, '16')
                .waitToClick(selectors.ticketsIndex.searchButton)
                .waitForNumberOfElements(selectors.ticketsIndex.searchResult, 1)
                .countElement(selectors.ticketsIndex.searchResult);

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

        it(`should click on the search result to access to the ticket Sale once again`, async() => {
            const url = await nightmare
                .waitForTextInElement(selectors.ticketsIndex.searchResult, 'address 21')
                .waitToClick(selectors.ticketsIndex.searchResult)
                .waitToClick(selectors.ticketSales.saleButton)
                .waitForURL('/sale')
                .parsedUrl();

            expect(url.hash).toContain('/sale');
        });
    });

    describe('when state is preparation and loged as salesPerson', () => {
        it(`shouldnt be able to edit the sale price`, async() => {
            const result = await nightmare
                .waitToClick(selectors.ticketSales.firstSaleID)
                .exists(selectors.ticketSales.firstSalePrice);

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

        it(`shouldnt be able to edit the sale discount`, async() => {
            const result = await nightmare
                .waitToClick(selectors.ticketSales.firstSaleID)
                .exists(selectors.ticketSales.firstSaleDiscount);

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

        it(`shouldnt be able to edit the sale state`, async() => {
            const result = await nightmare
                .waitToClick(selectors.ticketSales.stateMenuButton)
                .exists(selectors.ticketSales.stateMenuOptions);

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