const { randomNumber, randomString } = require('../../support');

describe('VnLocation', () => {
    const locationOptions = '[role="listbox"] > div.q-virtual-scroll__content > .q-item';
    const dialogInputs = '.q-dialog label input';
    const createLocationButton = '.q-form > .q-card > .vn-row:nth-child(6) .--add-icon';
    const inputLocation = '.q-form input[aria-label="Location"]';
    const createForm = {
        prefix: '.q-dialog__inner > .column > #formModel > .q-card',
        sufix: ' .q-field__inner > .q-field__control',
    };
    const countrySelector = `${createForm.prefix} > :nth-child(5) > :nth-child(3) > ${createForm.sufix}`;
    const provinceSelector = `${createForm.prefix} > :nth-child(5) > :nth-child(2) > ${createForm.sufix}`;
    const citySelector = `${createForm.prefix} > :nth-child(4) > :nth-child(2) > ${createForm.sufix}`;
    describe('CreateFormDialog ', () => {
        beforeEach(() => {
            cy.viewport(1280, 720);
            cy.login('developer');
            cy.visit('/#/supplier/567/fiscal-data', { timeout: 7000 });
            cy.domContentLoad();
            cy.get(createLocationButton).click();
        });
        it('should filter provinces based on selected country', () => {
            const country = 'Ecuador';
            cy.selectOption(countrySelector, country);
            cy.get(countrySelector).should('have.length', 1);
            cy.get(citySelector).should('have.length', 1);
        });

        it('should filter towns based on selected province', () => {
            const country = 'Ecuador';
            cy.selectOption(countrySelector, country);
            cy.get(provinceSelector).should('have.length', 1);
            cy.get(citySelector).should('have.length', 1);
        });
        it('should pass selected country', () => {
            const country = 'Ecuador';
            const province = 'Province five';

            cy.selectOption(countrySelector, country);
            cy.selectOption(
                `${createForm.prefix} > :nth-child(4) > .q-select > ${createForm.sufix}`,
                province
            );
            cy.get(
                `${createForm.prefix} > :nth-child(4) > .q-select > ${createForm.sufix} > :nth-child(3) `
            ).click();
            cy.dataCy('locationProvince').should('have.value', province);
        });
    });
    describe('Worker Create', () => {
        beforeEach(() => {
            cy.viewport(1280, 720);
            cy.login('developer');
            cy.visit('/#/worker/create', { timeout: 5000 });
            cy.waitForElement('.q-card');
            cy.get(inputLocation).click();
        });
        it('Show all options', function () {
            cy.get(locationOptions).should('have.length.at.least', 5);
        });
        it('input filter location as "al"', function () {
            cy.get(inputLocation).clear();
            cy.get(inputLocation).type('al');
            cy.get(locationOptions).should('have.length.at.least', 4);
        });
        it('input filter location as "ecuador"', function () {
            cy.get(inputLocation).clear();
            cy.get(inputLocation).type('ecuador');
            cy.get(locationOptions).should('have.length.at.least', 1);
        });
    });
    describe('Fiscal-data', () => {
        beforeEach(() => {
            cy.viewport(1280, 720);
            cy.login('developer');
            cy.visit('/#/supplier/567/fiscal-data', { timeout: 7000 });
            cy.waitForElement('.q-form');
        });
        it('Find by postalCode', () => {
            const postCode = '46600';
            const postCodeLabel = '46600, Valencia(Province one), España';
            const firstOption = '[role="listbox"] .q-item:nth-child(1)';

            cy.get(inputLocation).click();
            cy.get(inputLocation).clear();
            cy.get(inputLocation).type(postCode);
            cy.get(locationOptions)
                .get(':nth-child(1)')
                .should('have.length.at.least', 2);
            cy.get(
                firstOption.concat(' > .q-item__section > .q-item__label--caption')
            ).should('have.text', postCodeLabel);
            cy.get(firstOption).click();
            cy.get('.q-btn-group > .q-btn--standard > .q-btn__content > .q-icon').click();
            cy.reload();
            cy.waitForElement('.q-form');
            cy.get(inputLocation).should('have.value', postCodeLabel);
        });

        it('Create postCode', () => {
            const postCode = Math.floor(100000 + Math.random() * 900000);
            const province = 'Valencia';
            cy.get(createLocationButton).click();
            cy.get('.q-card > h1').should('have.text', 'New postcode');
            cy.selectOption(
                `${createForm.prefix} > :nth-child(4) > .q-select > ${createForm.sufix}`,
                province
            );
            cy.get(dialogInputs).eq(0).clear();
            cy.get(dialogInputs).eq(0).type(postCode);
            cy.get('.q-mt-lg > .q-btn--standard').click();
            cy.get(`${createForm.prefix}`).should('not.exist');
            cy.waitForElement('.q-form');

            checkVnLocation(postCode, province);
        });

        it('Create city without country', () => {
            const postCode = randomNumber();
            const province = randomString({ length: 4 });
            cy.get(createLocationButton).click();
            cy.get(dialogInputs).eq(0).type(postCode);
            cy.dataCy('City_icon').click();
            cy.selectOption('[data-cy="locationProvince"]:last', 'Province one');
            cy.dataCy('cityName').type(province);
            cy.dataCy('FormModelPopup_save').eq(1).click();
            cy.dataCy('FormModelPopup_save').eq(0).click();

            cy.waitForElement('.q-form');
            checkVnLocation(postCode, province);
        });

        it('Create city with country', () => {
            const cityName = 'Saskatchew'.concat(Math.random(1 * 100));
            cy.get(createLocationButton).click();
            cy.selectOption(
                `${createForm.prefix} > :nth-child(5) > :nth-child(3) `,
                'Italia'
            );
            cy.dataCy('City_icon').click();
            cy.selectOption('[data-cy="locationProvince"]:last', 'Province four');
            cy.countSelectOptions('[data-cy="locationProvince"]:last', 1);

            cy.dataCy('cityName').type(cityName);
            cy.dataCy('FormModelPopup_save').eq(1).click();
        });

        it('Create province without country', () => {
            const provinceName = 'Saskatchew'.concat(Math.random(1 * 100));
            cy.get(createLocationButton).click();
            cy.dataCy('Province_icon').click();
            cy.selectOption('[data-cy="autonomyProvince"] ', 'Autonomy one');
            cy.countSelectOptions('[data-cy="autonomyProvince"]', 4);
            cy.dataCy('provinceName').type(provinceName);

            cy.dataCy('FormModelPopup_save').eq(1).click();
        });

        it('Create province with country', () => {
            const provinceName = 'Saskatchew'.concat(Math.random(1 * 100));
            cy.get(createLocationButton).click();
            cy.selectOption(
                `${createForm.prefix} > :nth-child(5) > :nth-child(3) `,
                'España'
            );
            cy.dataCy('Province_icon').click();

            cy.selectOption('[data-cy="autonomyProvince"] ', 'Autonomy one');
            cy.countSelectOptions('[data-cy="autonomyProvince"]', 2);

            cy.dataCy('provinceName').type(provinceName);
            cy.dataCy('FormModelPopup_save').eq(1).click();
        });

        function checkVnLocation(postCode, province) {
            cy.get(`${createForm.prefix}`).should('not.exist');
            cy.get('.q-form > .q-card > .vn-row:nth-child(6)')
                .find('input')
                .invoke('val')
                .then((text) => {
                    expect(text).to.contain(postCode);
                    expect(text).to.contain(province);
                });
        }
    });
});
