import {expect, test} from '@jest/globals';
import {jestState} from '../form.spec';
import {updateSingleSetting, getSettingState} from '../utils.js';

export async function __loginProtectionPassChangeOptionEnabled() {
    await updateSingleSetting('misc__prevent_logins_collecting_on_password_reset', '1');

    await jestState.page.goto(
        jestState.envData.root
        + '/wp-login.php?action=lostpassword',
        {waitUntil: 'load'},
    );

    const protectionPhrase = 'If the user with the specified credentials exists, check your email for the password reset confirmation link. Then visit';

    let field = '#user_login';
    await jestState.page.waitForSelector(field);
    await jestState.page.$eval( field, (element) => element.value = '');
    await jestState.page.click(field);
    await jestState.page.type(field, 'non_existing_admin');
    await jestState.page.click('#wp-submit');
    await jestState.page.waitForSelector('#login-message');
    const loginMSGProtected = await jestState.page.$eval('#login-message', (el) => el.innerText);
    await expect(loginMSGProtected).toContain(protectionPhrase);
}

export async function __loginProtectionPassChangeOptionDisabled() {
    await updateSingleSetting('misc__prevent_logins_collecting_on_password_reset', '0');

    await jestState.page.goto(
        jestState.envData.root
        + '/wp-login.php?action=lostpassword',
        {waitUntil: 'load'},
    );

    const protectionPhrase = 'Error: There is no account with that username or email address.';

    let field = '#user_login';
    await jestState.page.waitForSelector(field);
    await jestState.page.$eval( field, (element) => element.value = '');
    await jestState.page.click(field);
    await jestState.page.type(field, 'non_existing_admin');
    await jestState.page.click('#wp-submit');
    await jestState.page.waitForSelector('#login_error');
    const loginMSGUnprotected = await jestState.page.$eval('#login_error', (el) => el.innerText);
    await expect(loginMSGUnprotected).toContain(protectionPhrase);
}

export async function __loginProtectionAuthorURLOptionEnabled() {
    await updateSingleSetting('misc__prevent_logins_collecting', '1');

    await jestState.page.goto(
        jestState.envData.root
        + '/?author=1',
        {waitUntil: 'load'},
    );

    let field = '.archive-title';
    const URLProtected = await jestState.page.$(field);
    await expect(URLProtected).toBeNull();
}

export async function __loginProtectionAuthorURLOptionDisabled() {
    await updateSingleSetting('misc__prevent_logins_collecting', '0');

    await jestState.page.goto(
        jestState.envData.root
        + '/?author=1',
        {waitUntil: 'load'},
    );

    const protectionPhrase = '404';

    let field = '.archive-title';
    const URLProtected = await jestState.page.$(field);
    await expect(URLProtected).toBeDefined();
}

/**
 * Run tests.
 * This test is a wrapper for group of tests.
 * @param {boolean} doLogin
 * @return {void}
 */
export function groupLoginCollectPrevention() {
    let settingsState = {};
    test('SETTINGS GET STATE', async () => {
        settingsState.misc__prevent_logins_collecting = await getSettingState('misc__prevent_logins_collecting');
        settingsState.misc__prevent_logins_collecting_on_password_reset = await getSettingState('misc__prevent_logins_collecting_on_password_reset');
    }, jestState.globals.globalTimeout);

    test('MISC SECURITY LOGIN PROTECTION ON PASSWORD CHANGE - ENABLED', async () => {
        await __loginProtectionPassChangeOptionEnabled();
    }, jestState.globals.globalTimeout);

    test('MISC SECURITY LOGIN PROTECTION ON PASSWORD CHANGE - DISABLED', async () => {
        await __loginProtectionPassChangeOptionDisabled();
    }, jestState.globals.globalTimeout);

    test('MISC SECURITY LOGIN PROTECTION ON ?AUTHOR URL - ENABLED', async () => {
        await __loginProtectionAuthorURLOptionEnabled();
    }, jestState.globals.globalTimeout);

    test('MISC SECURITY LOGIN PROTECTION ON ?AUTHOR URL - ENABLED', async () => {
        await __loginProtectionAuthorURLOptionDisabled();
    }, jestState.globals.globalTimeout);

    test('SETTINGS SET STATE', async () => {
        updateSingleSetting('misc__prevent_logins_collecting',settingsState.misc__prevent_logins_collecting)
        updateSingleSetting('misc__prevent_logins_collecting_on_password_reset',settingsState.misc__prevent_logins_collecting_on_password_reset)
    }, jestState.globals.globalTimeout);
}
