<?php use PHPCensor\Helper\Lang; ?>
<ul class="timeline">
    <?php $last = new \DateTime('-1 Year'); ?>

    <?php
    foreach ($builds as $build):
        $environment = $build->getEnvironment();
        $branches = $build->getExtra('branches');

        switch ($build->getStatus()) {
            case \PHPCensor\Model\Build::STATUS_PENDING:
                $updated = $build->getCreated();
                $label = Lang::get('pending');
                $color = 'blue';
                break;

            case \PHPCensor\Model\Build::STATUS_RUNNING:
                $updated = $build->getStarted();
                $label = Lang::get('running');
                $color = 'yellow';
                break;

            case \PHPCensor\Model\Build::STATUS_SUCCESS:
                $updated = $build->getFinished();
                $label = Lang::get('success');
                $color = 'green';
                break;

            case \PHPCensor\Model\Build::STATUS_FAILED:
                $updated = $build->getFinished();
                $label = Lang::get('failed');
                $color = 'red';
                break;
        }

        if (!$updated) {
            $updated = $build->getCreated();
        }

        if ($updated->format('Y-m-d') != $last->format('Y-m-d')): $last = $updated;
            ?>
            <li class="time-label">
    <span class="bg-gray">
        <?= $last->format('Y-m-d'); ?>
    </span>
            </li>
        <?php endif; ?>

        <!-- /.timeline-label -->
        <!-- timeline item -->
        <li>
            <i class="fa fa-<?php print $build->getProject()->getIcon(); ?> bg-<?php print $color; ?>"></i>
            <div class="timeline-item">
                <span class="time"><i class="fa fa-clock-o"></i>
                <?php
                echo $updated->format('H:i:s');
                if ($build->getStatus() != \PHPCensor\Model\Build::STATUS_PENDING) {
                    echo ' (' . $build->getPrettyDuration() . ')';
                }
                ?>
                </span>
                <h3 class="timeline-header">
                    <a href="<?php print APP_URL; ?>project/view/<?php print $build->getProjectId(); ?>">
                        <?php print $build->getProject()->getTitle(); ?>
                    </a>
                    <span><?php print $environment; ?></span>
                    -
                    <a href="<?php print APP_URL; ?>build/view/<?php print $build->getId(); ?>">
                        Build #<?php print $build->getId(); ?>
                    </a>
                    -
                    <?php print $label; ?>
                </h3>

                <div class="timeline-body">
                    <a href="<?php echo $build->getBranchLink();?>"><?php echo $build->getProject()->getBranch(); ?></a>
                    <?php print $branches ? ' + '.implode(', ', $branches) : ''; ?> -
                    <?php
                    if ($build->getCommitId() !== 'Manual') {
                        print sprintf(
                            '<a href="%s" target="_blank">%s (%s)</a>',
                            $build->getCommitLink(),
                            substr($build->getCommitId(), 0, 7),
                            $build->getCommitterEmail()
                        );
                    } else {
                        print Lang::get('manual_build');
                    }
                    ?>
                    - <?php print $build->getCommitMessage(); ?>
                </div>
            </div>
        </li>
        <!-- END timeline item -->

    <?php endforeach; ?>

    <li>
        <i class="fa fa-clock-o"></i>
    </li>
</ul>
