import selectors from '../../helpers/selectors';
import getBrowser from '../../helpers/puppeteer';

describe('Client lock verified data path', () => {
    let browser;
    let page;
    beforeAll(async() => {
        browser = await getBrowser();
        page = browser.page;
        await page.loginAndModule('salesPerson', 'client');
        await page.accessToSearchResult('Hank Pym');
        await page.accessToSection('client.card.fiscalData');
    });

    afterAll(async() => {
        await browser.close();
    });

    describe('as salesPerson', () => {
        it('should confirm verified data button is disabled for salesPerson', async() => {
            await page.wait(200);
            await page.wait(selectors.clientFiscalData.verifiedDataCheckbox);
            const result = await page.isDisabled(selectors.clientFiscalData.verifiedDataCheckbox);

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

        it('should edit the social name', async() => {
            await page.wait(selectors.clientFiscalData.socialName);
            await page.clearInput(selectors.clientFiscalData.socialName);
            await page.write(selectors.clientFiscalData.socialName, 'Captain America Civil War');
            await page.waitToClick(selectors.clientFiscalData.saveButton);
            const message = await page.waitForSnackbar();

            expect(message.type).toBe('success');
        });

        it('should confirm the social name have been edited', async() => {
            await page.reloadSection('client.card.fiscalData');
            const result = await page.waitToGetProperty(selectors.clientFiscalData.socialName, 'value');

            expect(result).toEqual('Captain America Civil War');
        });
    });

    describe('as administrative', () => {
        beforeAll(async() => {
            await page.loginAndModule('administrative', 'client');
            await page.accessToSearchResult('Hank Pym');
            await page.accessToSection('client.card.fiscalData');
        });

        it('should confirm verified data button is enabled for administrative', async() => {
            const result = await page.isDisabled(selectors.clientFiscalData.verifiedDataCheckbox);

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

        it('should check the Verified data checkbox', async() => {
            await page.waitToClick(selectors.clientFiscalData.verifiedDataCheckbox);
            await page.waitToClick(selectors.clientFiscalData.saveButton);
            await page.waitToClick(selectors.clientFiscalData.acceptDuplicationButton);
            const message = await page.waitForSnackbar();

            expect(message.type).toBe('success');
        });

        it('should confirm Verified data checkbox is checked', async() => {
            await page.reloadSection('client.card.fiscalData');
            const isChecked = await page.checkboxState(selectors.clientFiscalData.verifiedDataCheckbox);

            expect(isChecked).toEqual('checked');
        });

        it('should again edit the social name', async() => {
            await page.wait(selectors.clientFiscalData.socialName);
            await page.clearInput(selectors.clientFiscalData.socialName);
            await page.write(selectors.clientFiscalData.socialName, 'Ant man and the Wasp');
            await page.waitToClick(selectors.clientFiscalData.saveButton);
            const message = await page.waitForSnackbar();

            expect(message.type).toBe('success');
        });

        it('should again confirm the social name have been edited', async() => {
            await page.reloadSection('client.card.fiscalData');
            const result = await page.waitToGetProperty(selectors.clientFiscalData.socialName, 'value');

            expect(result).toEqual('Ant man and the Wasp');
        });
    });

    describe('as salesPerson second run', () => {
        beforeAll(async() => {
            await page.loginAndModule('salesPerson', 'client');
            await page.accessToSearchResult('Hank Pym');
            await page.accessToSection('client.card.fiscalData');
        });

        it('should confirm verified data button is disabled once again for salesPerson', async() => {
            const isDisabled = await page.isDisabled(selectors.clientFiscalData.verifiedDataCheckbox);

            expect(isDisabled).toBeTruthy();
        });

        it('should not be able to save change throwing a verified data error', async() => {
            await page.clearInput(selectors.clientFiscalData.socialName);
            await page.write(selectors.clientFiscalData.socialName, 'This wont happen');
            await page.waitToClick(selectors.clientFiscalData.saveButton);
            const message = await page.waitForSnackbar();

            expect(message.text).toBe(`You can't make changes on a client with verified data`);
        });
    });

    describe('as salesAssistant', () => {
        it('should log in as salesAssistant then get to the client fiscal data', async() => {
            await page.forceReloadSection('client.card.fiscalData');
            await page.loginAndModule('salesAssistant', 'client');
            await page.accessToSearchResult('Hank Pym');
            await page.accessToSection('client.card.fiscalData');
        }, 20000);

        it('should confirm verified data button is enabled for salesAssistant', async() => {
            const isDisabled = await page.isDisabled(selectors.clientFiscalData.verifiedDataCheckbox);

            expect(isDisabled).toBeFalsy();
        });

        it('should now edit the social name', async() => {
            await page.clearInput(selectors.clientFiscalData.socialName);
            await page.write(selectors.clientFiscalData.socialName, 'new social name edition');
            await page.waitToClick(selectors.clientFiscalData.saveButton);
            const message = await page.waitForSnackbar();

            expect(message.type).toBe('success');
        });

        it('should now confirm the social name have been edited once and for all', async() => {
            await page.reloadSection('client.card.fiscalData');
            const result = await page.waitToGetProperty(selectors.clientFiscalData.socialName, 'value');

            expect(result).toEqual('new social name edition');
        });
    });

    describe('as salesPerson third run', () => {
        beforeAll(async() => {
            await page.loginAndModule('salesPerson', 'client');
            await page.accessToSearchResult('Hank Pym');
            await page.accessToSection('client.card.fiscalData');
        });

        it('should confirm verified data button is enabled once again', async() => {
            const isDisabled = await page;
            await page.isDisabled(selectors.clientFiscalData.verifiedDataCheckbox);

            expect(isDisabled).toBeTruthy();
        });

        it('should confirm the form is enabled for salesPerson', async() => {
            await page.wait(selectors.clientFiscalData.socialName);
            const result = await page.evaluate(selector => {
                return document.querySelector(selector).disabled;
            }, 'vn-textfield[ng-model="$ctrl.client.socialName"] > div');

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