<?php

use Icinga\Util\Format;

function colorize($log) {
    $log = preg_replace(
        '/^(debug|notice|information|warning|critical)\/(\w+)/m',
        '<span class="loglevel \1">\1</span>/<span class="application">\2</span>',
        $log
    );

    $log = preg_replace('/([\^]{2,})/', '<span class="error-hint">\1</span>', $log);
    return $log;
}

?><div class="controls">
<?= $this->tabs ?>
<h1><?= $this->escape($this->title) ?></h1>
</div>

<div class="content">
<table class="name-value-table">
 <tr>
  <th><?= $this->translate('Deployment time') ?></th>
  <td><?= $deployment->start_time ?></td>
 </tr>
 <tr>
  <th><?= $this->translate('Sent to') ?></th>
  <td><?= $deployment->peer_identity ?></td>
 </tr>
 <tr>
  <th><?= $this->translate('Configuration') ?></th>
  <td><?= $this->qlink(
    sprintf(
        $this->translate('%d files'),
        $this->config->getFileCount()
    ),
    'director/config/files',
    array(
        'checksum'      => $config_checksum,
        'deployment_id' => $deployment->id
    )
) ?>, <?= sprintf(
    $this->translate('%d objects, %d templates'),
    $config->getObjectCount(),
    $config->getTemplateCount()
  )?>, <?= Format::bytes($config->getSize()) ?></td>
 </tr>
 <tr>
  <th><?= $this->translate('Duration') ?></th>
  <td><?= sprintf(
      $this->translate('Rendered in %0.2fs, deployed in %0.2fs'),
      $config->getDuration() / 1000,
      $deployment->duration_dump / 1000
  ) ?></td>
 </tr>
 <tr>
  <th><?= $this->translate('Stage name') ?></th>
  <td><?= $deployment->stage_name ?></td>
 </tr>
 <tr>
  <th><?= $this->translate('Startup') ?></th>
  <td><?php

if ($deployment->startup_succeeded === null) {
    if ($deployment->stage_collected === null) {
        echo $this->translate('Unknown, still waiting for config check outcome') . ' ' . $this->icon('spinner');
    } else {
        echo $this->translate('Unknown, failed to collect related information') . ' ' . $this->icon('help');
    }
} elseif ($deployment->startup_succeeded === 'y') {
    echo '<div style="color: green">';
    echo $this->translate('Succeeded') . ' ' . $this->icon('ok');
    echo '</div>';
} else {
    echo '<div style="color: red">';
    echo $this->translate('Failed') . ' ' . $this->icon('cancel');
    echo '</div>';
}

  ?></td>
 </tr>
</table>
<?php if ($deployment->startup_succeeded !== null): ?>
<h2>Startup log</h2>
<pre class="logfile">
<?= colorize(
    $this->escape($deployment->startup_log)
) ?>
</pre>
<?php endif ?>
</div>
