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

describe('Client Add address path', () => {
    const nightmare = createNightmare();

    beforeAll(() => {
        nightmare
            .loginAndModule('employee', 'client')
            .accessToSearchResult('Bruce Banner')
            .accessToSection('client.card.address.index');
    });

    it(`should click on the add new address button to access to the new address form`, async() => {
        const url = await nightmare
            .waitToClick(selectors.clientAddresses.createAddress)
            .waitForURL('address/create')
            .parsedUrl();

        expect(url.hash).toContain('address/create');
    });

    it('should receive an error after clicking save button as consignee, street and town fields are empty', async() => {
        const result = await nightmare
            .waitToClick(selectors.clientAddresses.defaultCheckboxInput)
            .clearInput(selectors.clientAddresses.streetAddressInput)
            .autocompleteSearch(selectors.clientAddresses.provinceAutocomplete, 'Province one')
            .clearInput(selectors.clientAddresses.cityInput)
            .write(selectors.clientAddresses.cityInput, 'Valencia')
            .clearInput(selectors.clientAddresses.postcodeInput)
            .write(selectors.clientAddresses.postcodeInput, '46000')
            .autocompleteSearch(selectors.clientAddresses.agencyAutocomplete, 'Entanglement')
            .write(selectors.clientAddresses.phoneInput, '999887744')
            .write(selectors.clientAddresses.mobileInput, '999887744')
            .waitToClick(selectors.clientFiscalData.saveButton)
            .waitForLastSnackbar();

        expect(result).toEqual('Some fields are invalid');
    });

    it('should confirm the postcode have been edited', async() => {
        const result = await nightmare
            .waitToGetProperty(`${selectors.clientAddresses.postcodeInput}`, 'value');

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

    it('should confirm the city have been autocompleted', async() => {
        const result = await nightmare
            .waitToGetProperty(`${selectors.clientAddresses.cityInput}`, 'value');

        expect(result).toEqual('Valencia');
    });


    it(`should confirm the province have been autocompleted`, async() => {
        const result = await nightmare
            .waitToGetProperty(`${selectors.clientAddresses.provinceAutocomplete} input`, 'value');

        expect(result).toEqual('Province one');
    });

    it(`should create a new address with all it's data`, async() => {
        const result = await nightmare
            .write(selectors.clientAddresses.consigneeInput, 'Bruce Bunner')
            .write(selectors.clientAddresses.streetAddressInput, '320 Park Avenue New York')
            .waitToClick(selectors.clientAddresses.saveButton)
            .waitForLastSnackbar();

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

    it(`should click on the addresses button confirm the new address exists and it's the default one`, async() => {
        const result = await nightmare
        // .waitToClick(selectors.clientAddresses.addressesButton)
            .waitToGetProperty(selectors.clientAddresses.defaultAddress, 'innerText');

        expect(result).toContain('320 Park Avenue New York');
    });

    it(`should click on the make default icon of the second address then confirm it is the default one now`, async() => {
        const result = await nightmare
            .waitToClick(selectors.clientAddresses.secondMakeDefaultStar)
            .waitForTextInElement(selectors.clientAddresses.defaultAddress, 'Somewhere in Thailand')
            .waitToGetProperty(selectors.clientAddresses.defaultAddress, 'innerText');

        expect(result).toContain('Somewhere in Thailand');
    });

    it(`should click on the edit icon of the default address`, async() => {
        const url = await nightmare
            .waitForTextInElement(selectors.clientAddresses.defaultAddress, 'Somewhere in Thailand')
            .waitToClick(selectors.clientAddresses.firstEditAddress)
            .waitForURL('/edit')
            .parsedUrl();

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

    it(`should click on the active checkbox and receive an error to save it because it is the default address`, async() => {
        const result = await nightmare
            .waitToClick(selectors.clientAddresses.activeCheckbox)
            .waitToClick(selectors.clientAddresses.saveButton)
            .waitForLastSnackbar();

        expect(result).toEqual('The default consignee can not be unchecked');
    });

    // this "it" should be removed if the watcher doesn't prevent the navigation upon state changes
    it(`should go back to the addreses section by clicking the cancel button`, async() => {
        const url = await nightmare
            .waitToClick(selectors.clientAddresses.cancelEditAddressButton)
            .waitToClick('vn-confirm button[response="ACCEPT"]')
            .waitForURL('address/index')
            .parsedUrl();

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