import dotenv from 'dotenv';
import {MD5} from "crypto-js";
import {expect, test} from '@jest/globals';
import {jestState} from './form.spec';

// please, set path to .env file in the root of WP directory
// JEST_VAR__APP_ROOT=""
// JEST_VAR__WP_LOGIN=""
// JEST_VAR__WP_PASSWORD=""
// JEST_VAR__CHROME_EXECUTABLE=""
// JEST_VAR__CHROME_SET_UA=""
// JEST_VAR__HEADLESS="1"
dotenv.config({path: '..\\..\\..\\.env'});

/**
 * Get env data.
 * @return {{root: string, wp_login: string, wp_password: string,
 * chrome_executable: string, chrome_ua: string, headless: boolean}}
 */
export function getEnvData() {
    const envData = {
        root: process.env.JEST_VAR__APP_ROOT,
        wp_login: process.env.JEST_VAR__WP_LOGIN,
        wp_password: process.env.JEST_VAR__WP_PASSWORD,
        chrome_executable: process.env.JEST_VAR__CHROME_EXECUTABLE,
        chrome_ua: process.env.JEST_VAR__CHROME_SET_UA,
        headless: process.env.JEST_VAR__HEADLESS === '1',
        api_key: process.env.JEST_VAR__API_KEY,
    };
    if (
        envData.root === undefined ||
        envData.wp_login === undefined ||
        envData.wp_password === undefined ||
        envData.chrome_ua === undefined ||
        envData.chrome_executable === undefined ||
        envData.headless === undefined ||
        envData.api_key === undefined
    ) {
        throw new Error('Environment data is not set');
    }
    return envData;
}

/**
 * Get globals.
 * @return {{browserWidth: number, browserHeight: number, globalTimeout: number}}
 */
export function getGlobals() {
    return {
        browserWidth: 1920,
        browserHeight: 1080,
        globalTimeout: 200000,
    };
}


/**
 * Update plugin settings tool.
 * Run this as async function before perform any actions appropriated to selected setting.
 * @param settingName
 * @param newValue
 * @returns {Promise<void>}
 */
export async function updateSingleSetting(settingName, newValue) {
    let key_md5 = MD5(jestState.envData.api_key).toString()
    let rnd = Math.random(1000000000);

    await jestState.page.goto(
        jestState.envData.root
        + '/?spbc_remote_call_token=' + key_md5
        + '&spbc_remote_call_action=update_settings&plugin_name=security&settings={"'
        + settingName
        +  '":"'
        + newValue
        + '"}&nocache='
        + rnd,
        {waitUntil: 'load'},
    );

    await new Promise(resolve => setTimeout(resolve, 3000));

    const settingsSaveResult = await jestState.page.$eval('body', (el) => el.innerText);
    await expect(settingsSaveResult).toContain('"' + settingName + '":true');
}

export async function getSettingState(settingName) {
    let key_md5 = MD5(jestState.envData.api_key).toString()
    let rnd = Math.random(1000000000);

    await jestState.page.goto(
        jestState.envData.root
        + '/?spbc_remote_call_token=' + key_md5
        + '&spbc_remote_call_action=debug&plugin_name=security',
        {waitUntil: 'load'},
    );
    await new Promise(resolve => setTimeout(resolve, 3000));

    const settingsSaveResult = await jestState.page.$eval('body', (el) => el.innerText);
    await expect(settingsSaveResult).toContain(settingName);
    async function getNumericValue(content, settingName) {
        const regex = new RegExp(settingName + '.+(\\d)');
        let val = content.match(regex)
        if (val.length === 2) {
            return val[1]
        }
        throw DOMException('');
    }
    return await getNumericValue(settingsSaveResult, settingName);
}
