<?php
use Icinga\Web\Url;
use Icinga\Util\Color;

$groupInfo = $timeline->getGroupInfo();
$firstRow = !$beingExtended;

?>
<?php if (!$beingExtended): ?>
<div class="controls">
    <?= $this->tabs ?>
    <div style="margin: 1em;" class="dontprint">
        <?= $intervalBox; ?>
    </div>
    <div style="margin: 1em;" class="timeline-legend">
        <h2><?= $this->translate('Legend'); ?></h2>
<?php foreach ($groupInfo as $labelAndColor): ?>
        <span style="background-color: <?= $labelAndColor['color']; ?>;">
            <span><?= $labelAndColor['label']; ?></span>
        </span>
<?php endforeach ?>
    </div>
</div>
<div class="content" data-base-target="_next">
    <div class="timeline">
<?php endif ?>
<?php if ($switchedContext): ?>
        <hr>
<?php endif ?>
<?php foreach ($timeline as $timeInfo): ?>
        <div class="timeframe">
            <span>
                <a href="<?= $this->href(
                    '/monitoring/list/eventhistory',
                    array(
                        'timestamp<' => $timeInfo[0]->start->getTimestamp(),
                        'timestamp>' => $timeInfo[0]->end->getTimestamp()
                    )
                ); ?>">
                    <?= $timeInfo[0]->end->format($intervalFormat); ?>
                </a>
            </span>
<?php foreach ($groupInfo as $groupName => $labelAndColor): ?>
<?php if (array_key_exists($groupName, $timeInfo[1])): ?>
<?php
$circleWidth = $timeline->calculateCircleWidth($timeInfo[1][$groupName], 2);
$extrapolatedCircleWidth = $timeline->getExtrapolatedCircleWidth($timeInfo[1][$groupName], 2);
?>
<?php if ($firstRow && $extrapolatedCircleWidth !== $circleWidth): ?>
            <div class="circle-box" style="width: <?= $extrapolatedCircleWidth; ?>;">
                <div class="outer-circle extrapolated" style="<?= sprintf(
                    'width: %4$s; height: %4$s; border-color: %3$s; background-color: %1$s; margin-top: -%2$Fem;',
                    Color::changeBrightness($timeInfo[1][$groupName]->getColor(), 0.7),
                    (float) substr($extrapolatedCircleWidth, 0, -2) / 2,
                    $timeInfo[1][$groupName]->getColor(),
                    $extrapolatedCircleWidth
                ); ?>">
<?php else: ?>
            <div class="circle-box" style="width: <?= $circleWidth; ?>;">
                <div class="outer-circle" style="<?= sprintf(
                    'width: %2$s; height: %2$s; margin-top: -%1$Fem;',
                    (float) substr($circleWidth, 0, -2) / 2,
                    $circleWidth
                ); ?>">
<?php endif ?>
                    <a class="inner-circle" style="<?= sprintf(
                        'width: %3$s; height: %3$s; background-color: %2$s; margin-top: -%1$Fem; margin-left: -%1$Fem;',
                        (float) substr($circleWidth, 0, -2) / 2,
                        $timeInfo[1][$groupName]->getColor(),
                        (string) $circleWidth
                    ); ?>" href="<?= $timeInfo[1][$groupName]->getDetailUrl()->overwriteParams(
                        array(
                            'timestamp<' => $timeInfo[0]->start->getTimestamp(),
                            'timestamp>'   => $timeInfo[0]->end->getTimestamp(),
                            'type'  => $groupName
                        )
                    ); ?>" title="<?= $timeInfo[1][$groupName]->getValue(); ?> <?= $labelAndColor['label']; ?>"></a>
                </div>
            </div>
<?php endif ?>
<?php endforeach ?>
        </div>
        <?php $firstRow = false; ?>
<?php endforeach ?>
        <a id="end" href="<?= Url::fromRequest()->remove(
            array(
                'raw_timestamp<',
                'raw_timestamp>'
            )
        )->overwriteParams(
            array(
                'start'     => $nextRange->getStart()->getTimestamp(),
                'end'       => $nextRange->getEnd()->getTimestamp(),
                'extend'    => 1
            )
        ); ?>"></a>
<?php if (!$beingExtended): ?>
    </div>
</div>
<?php endif ?>
