<%
  log_url = "#{ @base_url }/log.gz"
  artifacts_url = "#{ @base_url }/artifacts.tar.gz"
%>
<div class="row">
  <div class="col-lg-12">
    <nav aria-label="breadcrumb">
      <ol class='breadcrumb'>
        <li class="breadcrumb-item"><a href='/packages/<%= @package.prefix %>/'><%= @package.prefix %></a></li>
        <li class="breadcrumb-item"><a href='/packages/<%= @package.prefix %>/<%= @package.name %>'><%= @package.name %></a></li>
        <li class="breadcrumb-item"><a href='/packages/<%= @package.prefix %>/<%= @package.name %>/<%= @job.suite %>/<%= @job.arch %>/'><%= @job.suite %>/<%= @job.arch %></a></li>
        <li class='breadcrumb-item active'>Job <%=  @job.run_id %></li>
      </ol>
    </nav>
  </div>
</div>

<div class="row">
  <div class="col-lg-12">
    <div class='btn-group log-actions'>
      <a class="btn btn-outline-secondary" href="<%= log_url %>"><span class=" fa fa-download"></span> Download raw log</a>
      <a class="btn btn-outline-secondary" href="<%= artifacts_url %>"><span class=" fa fa-download"></span> Download test artifacts</a>
      <% if logged_in? %>
        <a class="btn btn-outline-secondary" href="/user/<%= @user.username %>/retry/<%= @job.run_id %>"><span class=" fa fa-recycle"></span> Retry</a>
      <% else %>
        <a class="btn btn-outline-secondary" disabled title="Login to be able to retry jobs"><span class=" fa fa-recycle"></span>Retry</a>
      <% end %>
    </div>
  </div>
</div>

<div class="row">

  <% 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>
