<?php

defined("ABSPATH") or die("");

/**
 * Variables
 *
 * @var Duplicator\Core\Controllers\ControllersManager $ctrlMng
 * @var Duplicator\Core\Views\TplMng $tplMng
 */

if (!$tplMng->dataValueExists('redetectRan')) {
    return;
}

$messageClasses = [
    'notice',
    'dupli-admin-notice',
    'is-dismissible',
    'dupli-diagnostic-action-redetect-server',
];

if (!$tplMng->getDataValueBool('redetectRan')) { ?>
    <div id="message" class="<?php echo esc_attr(implode(' ', array_merge($messageClasses, ['notice-warning']))); ?>">
        <p>
            <?php esc_html_e(
                'Server detection tests skipped: a backup is currently in progress. Try again when no backup is running.',
                'duplicator-pro'
            ); ?>
        </p>
    </div>
    <?php
    return;
}

$lockSql      = $tplMng->getDataValueBool('redetectLockSql');
$lockFile     = $tplMng->getDataValueBool('redetectLockFile');
$lockModeSql  = $tplMng->getDataValueBool('redetectLockModeSql');
$loopbackPass = $tplMng->getDataValueBool('redetectLoopbackPass');

$isWarning        = (!$lockSql && !$lockFile) || !$loopbackPass;
$messageClasses[] = ($isWarning ? 'notice-warning' : 'notice-success');

if ($lockModeSql) {
    $lockMessage = __('Process lock: SQL (database lock, reliable on this server).', 'duplicator-pro');
} elseif ($lockFile) {
    $lockMessage = __('Process lock: File (database lock unavailable, file lock passed the reliability test).', 'duplicator-pro');
} else {
    $lockMessage = __(
        'Process lock: File as last resort. Neither the database lock nor the file lock passed the reliability test,
        so concurrent build workers may not be prevented on this server.',
        'duplicator-pro'
    );
}

if ($loopbackPass) {
    $kickoffMessage = __('Kickoff self-request: PASS. The server can trigger build steps by itself (server-side kickoff).', 'duplicator-pro');
} else {
    $kickoffMessage = __(
        'Kickoff self-request: FAIL. The server cannot reach itself,
        so builds will rely on the browser staying open (client-side kickoff).',
        'duplicator-pro'
    );
}

?>
<div id="message" class="<?php echo esc_attr(implode(' ', $messageClasses)); ?>">
    <p><b><?php esc_html_e('Server detection tests completed.', 'duplicator-pro'); ?></b></p>
    <p><?php echo esc_html($lockMessage); ?></p>
    <p><?php echo esc_html($kickoffMessage); ?></p>
</div>
