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

if (! $this->compact): ?>
<div class="controls">
    <?= $this->tabs ?>
    <?= $this->form ?>
</div>
<?php endif ?>
<div class="content" data-base-target="_next">
<?php

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

    'cnt_warning_hard' => array(
        'tooltip' => $this->translate('%d services warning on %s'),
        'color' => '#ffaa44',
        'opacity' => '1.0'
    ),

    'cnt_down_hard' => array(
        'tooltip' => $this->translate('%d hosts down on %s'),
        'color' => '#ff5566',
        'opacity' => '0.9'
    ),
    'cnt_unknown_hard' => array(
        'tooltip' => $this->translate('%d services unknown on %s'),
        'color' => '#cc77ff',
        'opacity' => '0.7'
    ),
    'cnt_ok' => array(
        'tooltip' => $this->translate('%d services ok on %s'),
        'color' => '#49DF96',
        'opacity' => '0.55'
    )
);

$data = array();
foreach ($summary as $entry) {
    $day = $entry->day;
    $value = $entry->$column;
    $caption = sprintf(
        $settings[$column]['tooltip'],
        $value,
        $this->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())
    );
}

if (! $summary->hasResult()) {
    echo $this->translate('No state changes in the selected time period.') . '</div>';
    return;
}

$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;
}

$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;">
<?php 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>
<?php endforeach ?>
    </div>
</div>
