<%
  log_url = "#{ @base_url }/log.gz"
  artifacts_url = "#{ @base_url }/artifacts.tar.gz"
%>
<div class="row">
  <div class="col-lg-12">
    <ol class='breadcrumb'>
      <li><a href='/packages/<%= @package.prefix %>'><%= @package.prefix %></a></li>
      <li><a href='/packages/<%= @package.prefix %>/<%= @package.name %>'><%= @package.name %></a></li>
      <li><a href='/packages/<%= @package.prefix %>/<%= @package.name %>/<%= @job.suite %>/<%= @job.arch %>/'><%= @job.suite %>/<%= @job.arch %></a></li>
      <li class='active'>Job <%=  @job.run_id %></li>
    </ol>
    <h2>
      <%= @package.name %>
      <small>
        [ <a href=".."><%= @job.suite %>/<%= @job.arch %></a> ]
      </small>
      &mdash;
      job <%= @job.run_id %>
    <a class="btn btn-info btn-lg" href="<%= log_url %>"><span class=" fa fa-download"></span> Download raw log</a>
    <a class="btn btn-info btn-lg" href="<%= artifacts_url %>"><span class=" fa fa-download"></span> Download test artifacts</a>
    <a class="btn btn-default btn-lg" href="/user/:user/retry/<%= @job.run_id %>"><span class=" fa fa-recycle"></span> Retry</a>
    </h2>
  </div>
  <% if @too_large %>

    <div class="col-lg-12">
      <p class="alert alert-warning">
      This log file is too large to be displayed. Please download the raw log instead.
      </p>
    </div>
  <% else %>
    <div class="col-md-2 list-group" id="logview-index">
      <% i = 0 %>
      <% @log.sections.each do |section| %>
        <div class="list-group">
          <% klass = {"pass" => "list-group-item-success", "fail" => "list-group-item-danger"}[section.result] || "list-group-item-info" %>
          <div class="list-group-item <%= klass %>">
            <strong><%= section.name %></strong>
          </div>
          <% section.subsections.each do |sub| %>
            <% i += 1 %>
            <a class="list-group-item" href="#S<%= i %>"><%= sub.name %></a>
          <% end %>
        </div>
      <% end %>
    </div>
    <div class="col-md-10">
      <div class="log-view">
        <% line = 0 %>
        <% i = 0 %>
        <% @log.sections.each do |section| %>
          <% section.subsections.each do |sub| %>
            <% i += 1 %>
            <div id="S<%= i %>" class="log-divider log-divider-<%= section.result %>" data-siblings="<%= section.subsections.length - 1 %>">
              <a href="#S<%= i %>" onclick="javascript: return toggleLogSection('#S<%= i %>')">
                <span class="log-toggle log-open">▸</span>
                <span class="log-toggle">▾</span>
                <% if section.test? %><%= section.name %>:<% end %>
                <span class="log-section-name"><%= sub.name %></span>
              </a>
            </div>
            <div class="log-section">
              <% sub.output.each do |log_line| %>
                <% line += 1 %>
                <span id="L<%= line %>" class="line-number"><a href="#L<%= line%>"><%= line %></a></span>
                <div class="log-line"><%== ansi_to_html(log_line) %></div>
              <% end %>
            </div>
          <% end %>
        <% end %>
      </div>
    </div>
    <script type="text/javascript">
      function toggleLogSection(section) {
        $(section).next(".log-section").toggle()
        $(section).find(".log-toggle").toggle()
        return false
      }
      document.addEventListener('DOMContentLoaded', function() {
        $(".log-divider").each(function() {
          var anchor = document.location.hash
          var is_test = $(this).find(".log-section-name").html().match(/test run/)
          var is_failure = $(this).hasClass("log-divider-fail")
          var siblings = $(this).data("siblings")
          if (!is_test && !(is_failure && siblings == 0)) {
            if (anchor != "#" + $(this).attr("id")) {
              toggleLogSection(this)
            }
          }
        });
      })

    </script>
  <% end %>
</div>
