<?php
use Icinga\Data\Filter\Filter;
use Icinga\Web\Widget\Chart\HistoryColorGrid;
?>


<? if (!$compact): ?>
    <div class="controls">
        <?= $this->tabs->render($this); ?>
        <div class="fake-controls">
            <br />
            <?= $form ?>
        </div>
        <?php if (! $this->filterEditor): ?>
            <?= $this->filterPreview ?>
        <?php endif ?>
        <?= $this->filterEditor ?>
    </div>
<? endif; ?>

<div class="content" data-base-target="_next">
<?php

$settings = array(
    'cnt_up' => array(
        'tooltip' => mt('monitoring', '%d ok on %s'),
        'color' => '#49DF96',
        'opacity' => '0.55'
    ),
    'cnt_unreachable_hard' => array(
        'tooltip' => mt('monitoring', '%d unreachable on %s'),
        'color' => '#77AAFF',
        'opacity' => '0.55'
    ),
    'cnt_critical_hard' => array(
        'tooltip' => mt('monitoring', '%d critical on %s'),
        'color' => '#ff5566',
        'opacity' => '0.9'
    ),

    'cnt_warning_hard' => array(
        'tooltip' => mt('monitoring', '%d warning on %s'),
        'color' => '#ffaa44',
        'opacity' => '1.0'
    ),

    'cnt_down_hard' => array(
        'tooltip' => mt('monitoring', '%d down on %s'),
        'color' => '#ff5566',
        'opacity' => '0.9'
    ),
    'cnt_unknown_hard' => array(
        'tooltip' => mt('monitoring', '%d unknown on %s'),
        'color' => '#cc77ff',
        'opacity' => '0.7'
    ),
    'cnt_ok' => array(
        'tooltip' => mt('monitoring', '%d ok on %s'),
        'color' => '#49DF96',
        'opacity' => '0.55'
    )
);

$from = intval($form->getValue('from', strtotime('3 months ago')));
$to = intval($form->getValue('to', time()));

// don't display more than ten years, or else this will get really slow
if ($to - $from > 315360000) {
    $from = $to - 315360000;
}
$data = array();

if (count($summary) === 0) {
    echo mt('monitoring', 'No state changes in the selected time period.');
}
foreach ($summary as $entry) {
    $day = $entry->day;
    $value = $entry->$column;
    $caption = sprintf(
        $settings[$column]['tooltip'],
        $value,
        $this->dateFormat()->formatDate(strtotime($day))
    );
    $linkFilter = Filter::matchAll(
        Filter::expression('timestamp', '<', strtotime($day . ' 23:59:59')),
        Filter::expression('timestamp', '>', strtotime($day . ' 00:00:00')),
        $form->getFilter(),
        $filter
    );
    $data[$day] = array(
        'value'   => $value,
        'caption' => $caption,
        'url'     => $this->href('monitoring/list/eventhistory?' . $linkFilter->toQueryString())
    );
}

$f = new DateTime();
$f->setTimestamp($from);
$t = new DateTime();
$t->setTimestamp($to);
$diff = $t->diff($f);
$step = 124;

for ($i = 0; $i < $diff->days; $i += $step) {
    $end = clone $f;
    if ($diff->days - $i > $step) {
        // full range, move last day to next chunk
        $end->add(new DateInterval('P' . ($step - 1) . 'D'));
    } else {
        // include last day
        $end->add(new DateInterval('P' . ($diff->days - $i) . 'D'));
    }
    $grid = new HistoryColorGrid(null, $f->getTimestamp(), $end->getTimestamp());
    $grid->setColor($settings[$column]['color']);
    $grid->opacity = $settings[$column]['opacity'];
    $grid->orientation = $orientation;
    $grid->setData($data);
    $grids[] = $grid;

    $f->add(new DateInterval('P' . $step . 'D'));
}
?>
<div style="width: 33.5em;">
<? foreach (array_reverse($grids) as $key => $grid) { ?>
    <div style=" <?= $this->orientation === 'horizontal' ? '' : 'display: inline-block; vertical-align: top; top; margin: 0.5em;' ?>">
        <?= $grid; ?>
        <?= $this->orientation === 'horizontal' ? '<br />' : '' ?>
    </div>
<? } ?>
    </div>
</div>
