import config from '../helpers/config.js';
import createNightmare from '../helpers/nightmare';
import selectors from '../helpers/selectors.js';
import {catchErrors} from '../../services/utils/jasmineHelpers';
const nightmare = createNightmare();
const moduleAccessViewHashURL = '#!/';

describe('create client path', () => {
    describe('warm up', () => {
        it('should warm up login and fixtures', done => {
            nightmare
            .login()
            .waitForURL(moduleAccessViewHashURL)
            .waitToClick(selectors.globalItems.logOutButton)
            .then(() => {
                done();
            })
            .catch(catchErrors(done));
        });
    });

    it('should log in', done => {
        nightmare
        .login()
        .waitForURL(moduleAccessViewHashURL)
        .url()
        .then(url => {
            expect(url).toEqual(config.url + moduleAccessViewHashURL);
            done();
        })
        .catch(catchErrors(done));
    });

    it('should access to the clients index by clicking the clients button', done => {
        nightmare
        .click(selectors.moduleAccessView.clientsSectionButton)
        .wait(selectors.clientsIndex.createClientButton)
        .url()
        .then(url => {
            expect(url).toEqual(config.url + '#!/clients');
            done();
        })
        .catch(catchErrors(done));
    });

    it(`should search for the user Carol Danvers to confirm it isn't created yet`, done => {
        nightmare
        .wait(selectors.clientsIndex.searchResult)
        .type(selectors.clientsIndex.searchClientInput, 'Carol Danvers')
        .click(selectors.clientsIndex.searchButton)
        .waitForNumberOfElements(selectors.clientsIndex.searchResult, 0)
        .countSearchResults(selectors.clientsIndex.searchResult)
        .then(result => {
            expect(result).toEqual(0);
            done();
        })
        .catch(catchErrors(done));
    });

    it('should access to the create client view by clicking the create-client floating button', done => {
        nightmare
        .click(selectors.clientsIndex.createClientButton)
        .wait(selectors.createClientView.createButton)
        .url()
        .then(url => {
            expect(url).toEqual(config.url + '#!/create');
            done();
        })
        .catch(catchErrors(done));
    });

    it('should receive an error when clicking the create button having all the form fields empty', done => {
        nightmare
        .click(selectors.createClientView.createButton)
        .wait(selectors.globalItems.snackbarIsActive)
        .getInnerText(selectors.globalItems.snackbarIsActive)
        .then(result => {
            expect(result).toEqual('No hay cambios que guardar');
            done();
        })
        .catch(catchErrors(done));
    });

    it('should receive an error when clicking the create button having all the form fields empty but name', done => {
        nightmare
        .waitForSnackbarReset()
        .type(selectors.createClientView.name, 'Carol Danvers')
        .click(selectors.createClientView.createButton)
        .wait(selectors.globalItems.snackbarIsActive)
        .getInnerText(selectors.globalItems.snackbarIsActive)
        .then(result => {
            expect(result).toContain(`Error: La instancia`);
            done();
        })
        .catch(catchErrors(done));
    });

    it('should receive an error when clicking the create button having all the form fields empty but Tax Number', done => {
        nightmare
        .waitForSnackbarReset()
        .clearInput(selectors.createClientView.name)
        .type(selectors.createClientView.taxNumber, 'AVG tax')
        .click(selectors.createClientView.createButton)
        .wait(selectors.globalItems.snackbarIsActive)
        .getInnerText(selectors.globalItems.snackbarIsActive)
        .then(result => {
            expect(result).toContain(`Error: La instancia`);
            done();
        })
        .catch(catchErrors(done));
    });

    it('should receive an error when clicking the create button having all the form fields empty but Business Name', done => {
        nightmare
        .waitForSnackbarReset()
        .clearInput(selectors.createClientView.taxNumber)
        .type(selectors.createClientView.socialName, 'Avengers team')
        .click(selectors.createClientView.createButton)
        .wait(selectors.globalItems.snackbarIsActive)
        .getInnerText(selectors.globalItems.snackbarIsActive)
        .then(result => {
            expect(result).toContain(`Error: La instancia`);
            done();
        })
        .catch(catchErrors(done));
    });

    it('should receive an error when clicking the create button having all the form fields empty but User Name', done => {
        nightmare
        .waitForSnackbarReset()
        .clearInput(selectors.createClientView.socialName)
        .type(selectors.createClientView.userName, 'CaptainMarvel')
        .click(selectors.createClientView.createButton)
        .wait(selectors.globalItems.snackbarIsActive)
        .getInnerText(selectors.globalItems.snackbarIsActive)
        .then(result => {
            expect(result).toContain(`Error: La instancia`);
            done();
        })
        .catch(catchErrors(done));
    });

    it('should receive an error when clicking the create button having all the form fields empty but email while email have incorrect format', done => {
        nightmare
        .waitForSnackbarReset()
        .clearInput(selectors.createClientView.userName)
        .type(selectors.createClientView.email, 'I will save the Avengers!')
        .click(selectors.createClientView.createButton)
        .wait(selectors.globalItems.snackbarIsActive)
        .getInnerText(selectors.globalItems.snackbarIsActive)
        .then(result => {
            expect(result).toEqual(`Algunos campos no son válidos`);
            done();
        })
        .catch(catchErrors(done));
    });

    it('should receive an error when clicking the create button having all the form fields empty but email', done => {
        nightmare
        .waitForSnackbarReset()
        .clearInput(selectors.createClientView.email)
        .type(selectors.createClientView.email, 'CarolDanvers@verdnatura.es')
        .click(selectors.createClientView.createButton)
        .wait(selectors.globalItems.snackbarIsActive)
        .getInnerText(selectors.globalItems.snackbarIsActive)
        .then(result => {
            expect(result).toContain(`Error: La instancia`);
            done();
        })
        .catch(catchErrors(done));
    });

    it(`should create a new user with all it's data`, done => {
        nightmare
        .waitForSnackbarReset()
        .wait(selectors.createClientView.email)
        .clearInput(selectors.createClientView.email)
        .type(selectors.createClientView.name, 'Carol Danvers')
        .type(selectors.createClientView.taxNumber, 'Avengers Tax Number')
        .type(selectors.createClientView.socialName, 'AVG tax')
        .type(selectors.createClientView.userName, 'CaptainMarvel')
        .type(selectors.createClientView.email, 'CarolDanvers@verdnatura.es')
        .click(selectors.createClientView.createButton)
        .wait(selectors.globalItems.snackbarIsActive)
        .getInnerText(selectors.globalItems.snackbarIsActive)
        .then(result => {
            expect(result).toContain(`¡Datos guardados!`);
            done();
        })
        .catch(catchErrors(done));
    });

    it('should click on the Clients button of the top bar menu', done => {
        nightmare
        .waitToClick(selectors.globalItems.applicationsMenuButton)
        .wait(selectors.globalItems.applicationsMenuVisible)
        .waitToClick(selectors.globalItems.clientsButton)
        .wait(selectors.clientsIndex.createClientButton)
        .url()
        .then(url => {
            expect(url).toEqual(config.url + '#!/clients');
            done();
        })
        .catch(catchErrors(done));
    });

    it(`should search for the user Carol Danvers to confirm it exists`, done => {
        nightmare
        .wait(selectors.clientsIndex.searchResult)
        .type(selectors.clientsIndex.searchClientInput, 'Carol Danvers')
        .click(selectors.clientsIndex.searchButton)
        .waitForNumberOfElements(selectors.clientsIndex.searchResult, 1)
        .countSearchResults(selectors.clientsIndex.searchResult)
        .then(result => {
            expect(result).toEqual(1);
            done();
        })
        .catch(catchErrors(done));
    });
});