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

describe('Account create and basic data path', () => {
    let browser;
    let page;

    beforeAll(async() => {
        browser = await getBrowser();
        page = browser.page;
        await page.loginAndModule('itManagement', 'account');
    });

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

    it('should open the new account form by clicking the add button', async() => {
        await page.waitToClick(selectors.accountIndex.addAccount);
        await page.waitForState('account.create');
    });

    it('should fill the form and then save it by clicking the create button', async() => {
        await page.write(selectors.accountIndex.newName, 'remy');
        await page.write(selectors.accountIndex.newNickname, 'Gambit');
        await page.write(selectors.accountIndex.newEmail, 'RemyEtienneLeBeau@verdnatura.es');
        await page.autocompleteSearch(selectors.accountIndex.newRole, 'Trainee');
        await page.write(selectors.accountIndex.newPassword, 'cestlavie');
        await page.waitToClick(selectors.accountIndex.createAccountButton);
        const message = await page.waitForSnackbar();

        expect(message.text).toContain('Data saved!');
    });

    it('should redirect the user to the created account basic data section', async() => {
        await page.waitForState('account.card.basicData');
    });

    it('should check the name is as expected', async() => {
        const result = await page.waitToGetProperty(selectors.accountBasicData.name, 'value');

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

    it('should check the nickname is as expected', async() => {
        const result = await page.waitToGetProperty(selectors.accountBasicData.nickname, 'value');

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

    it('should check the email is as expected', async() => {
        const result = await page.waitToGetProperty(selectors.accountBasicData.email, 'value');

        expect(result).toEqual('RemyEtienneLeBeau@verdnatura.es');
    });

    it('should navigate to the roles section to check the roles are correct', async() => {
        await page.accessToSection('account.card.roles');
        const rolesCount = await page.countElement(selectors.accountRoles.anyResult);

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

    describe('Descriptor option', () => {
        describe('activate account', () => {
            it(`should check the active account icon isn't present in the descriptor`, async() => {
                await page.waitForNumberOfElements(selectors.accountDescriptor.activeAccountIcon, 0);
            });

            it('should activate the account using the descriptor menu', async() => {
                await page.waitToClick(selectors.accountDescriptor.menuButton);
                await page.waitToClick(selectors.accountDescriptor.activateAccount);
                await page.waitToClick(selectors.accountDescriptor.acceptButton);
                const message = await page.waitForSnackbar();

                expect(message.text).toContain('Account enabled!');
            });

            it('should check the active account icon is now present in the descriptor', async() => {
                await page.waitForSelector(selectors.accountDescriptor.activeAccountIcon, {visible: false});
            });
        });

        describe('deactivate user', () => {
            it(`should check the inactive user icon isn't present in the descriptor just yet`, async() => {
                await page.waitForNumberOfElements(selectors.accountDescriptor.activeUserIcon, 0);
            });

            it('should deactivate the user using the descriptor menu', async() => {
                await page.waitToClick(selectors.accountDescriptor.menuButton);
                await page.waitToClick(selectors.accountDescriptor.deactivateUser);
                await page.waitToClick(selectors.accountDescriptor.acceptButton);
                const message = await page.waitForSnackbar();

                expect(message.text).toContain('User deactivated!');
            });

            it('should check the inactive user icon is now present', async() => {
                await page.waitForNumberOfElements(selectors.accountDescriptor.activeUserIcon, 1);
            });
        });

        describe('activate user', () => {
            it('should activate the user using the descriptor menu', async() => {
                await page.waitToClick(selectors.accountDescriptor.menuButton);
                await page.waitToClick(selectors.accountDescriptor.activateUser);
                await page.waitToClick(selectors.accountDescriptor.acceptButton);
                const message = await page.waitForSnackbar();

                expect(message.text).toContain('User activated!');
            });

            it('should check the inactive user icon is not present anymore', async() => {
                await page.waitForNumberOfElements(selectors.accountDescriptor.activeUserIcon, 0);
            });
        });

        describe('mail forwarding', () => {
            it('should activate the mail forwarding and set the recipent email', async() => {
                await page.accessToSection('account.card.mailForwarding');
                await page.waitToClick(selectors.accountMailForwarding.mailForwardingCheckbox);
                await page.write(selectors.accountMailForwarding.email, 'someEmail@someDomain.es');
                await page.waitToClick(selectors.accountMailForwarding.save);
                const message = await page.waitForSnackbar();

                expect(message.text).toContain('Data saved!');
            });
        });

        describe('Set password', () => {
            it('should set the password using the descriptor menu', async() => {
                const newPassword = 'quantum.12345';

                await page.waitToClick(selectors.accountDescriptor.menuButton);
                await page.waitToClick(selectors.accountDescriptor.setPassword);
                await page.write(selectors.accountDescriptor.newPassword, newPassword);
                await page.write(selectors.accountDescriptor.repeatPassword, newPassword);
                await page.waitToClick(selectors.accountDescriptor.acceptButton);
                const message = await page.waitForSnackbar();

                expect(message.text).toContain('Password changed succesfully!');
            });

            // cant log into created account for unknown reasons
            // it('should login into the created account with the new password', async() => {
            //     await page.loginAndModule('Remy', 'quantum.crypt0graphy');
            // });
        });

        describe('delete account', () => {
            // it('should navigate to the account basic data section', async() => {
            // });

            it('should delete the account using the descriptor menu', async() => {
                await page.waitToClick(selectors.accountDescriptor.menuButton);
                await page.waitToClick(selectors.accountDescriptor.deleteAccount);
                await page.waitToClick(selectors.accountDescriptor.acceptButton);
                const message = await page.waitForSnackbar();

                expect(message.text).toContain('User removed');
            });
        });
    });
});