<div class="content">
  <project-page>
    <div>
      <div class="page-header page-header-bleed-right">
        <h1>Builds</h1>
      </div>
      <alerts alerts="alerts"></alerts>
      <!-- TODO probably move empty message -->
      <table class="table table-bordered table-hover table-mobile">
        <thead>
          <tr>
            <th>Name</th>
            <th>Last Build</th>
            <th>Status</th>
            <th>Started</th>
            <th>Type</th>
            <th>Source</th>
          </tr>
        </thead>
        <tbody ng-if="((buildsByBuildConfig | hashSize) == 0)">
          <tr><td colspan="6" data-title="Name"><em>{{emptyMessage}}</em></td></tr>
        </tbody>
        <tbody ng-repeat="(buildConfigName, buildConfigBuilds) in buildsByBuildConfig">
          <!-- Build config with no builds-->
          <tr ng-if="(buildConfigBuilds | hashSize) == 0">
            <td data-title="Name">
              <a href="{{buildConfigName | navigateResourceURL : 'BuildConfig' : projectName}}">{{buildConfigName}}</a>
            </td>
            <td data-title="Last Build"><em>No builds</em></td>
            <td class="hidden-xs">&nbsp;</td>
            <td class="hidden-xs">&nbsp;</td>
            <td data-title="Type">{{buildConfigs[buildConfigName].spec.strategy.type}}</td>
            <td data-title="Source"><span ng-if="buildConfigs[buildConfigName].spec.source.type == 'Git'" ng-bind-html='buildConfigs[buildConfigName].spec.source.git.uri | githubLink : buildConfigs[buildConfigName].spec.source.git.ref : buildConfigs[buildConfigName].spec.source.contextDir | linky'></span></td>
          </tr>
          <!-- Build config with builds, or builds whose build config has since been deleted -->
          <!-- We only show the first build from a build config for now-->
          <tr ng-repeat="build in buildConfigBuilds | orderObjectsByDate : true | limitTo : 1" ng-if="buildConfigs[buildConfigName] || !unfilteredBuildConfigs[buildConfigName]">
            <td data-title="Name">
              <a ng-if="buildConfigs[buildConfigName]" href="{{buildConfigName | navigateResourceURL : 'BuildConfig' : projectName}}">{{buildConfigName}}</a>
              <span ng-if="buildConfigs && !buildConfigs[buildConfigName] && buildConfigName != ''">
                {{buildConfigName}}
                <span class="pficon pficon-warning-triangle-o" data-toggle="tooltip" data-placement="right" title="This build config no longer exists" style="cursor: help;"></span>
              </span>
              <span ng-if="buildConfigName == ''"><em>none</em></span>
            </td>
            <td data-title="Last Build">
              <!-- Build number and link -->
              <span ng-if="(build | annotation : 'buildNumber') && buildConfigName != ''">
                <a ng-href="{{build | navigateResourceURL}}">#{{build | annotation : 'buildNumber'}}</a>
              </span>
              <span ng-if="!(build | annotation : 'buildNumber') && buildConfigName != ''">
                <a ng-href="{{build | navigateResourceURL}}">{{build.metadata.name}}</a>
              </span>
              <span ng-if="buildConfigName == ''">
                <a ng-href="{{build | navigateResourceURL}}">{{build.metadata.name}}</a>
              </span>
            </td>
            <td data-title="Status">
              <div row class="status">
                <status-icon status="build.status.phase" disable-animation></status-icon>
                <span flex>
                  <span>{{build.status.phase}}</span>
                  <!-- Build duration -->
                  <span ng-switch="build.status.phase" class="hide-ng-leave">
                    <span ng-switch-when="Complete"> in {{(build.status.startTimestamp || build.metadata.creationTimestamp) | duration : build.status.completionTimestamp}}</span>
                    <span ng-switch-when="Failed">after {{build.status.startTimestamp | duration : build.status.completionTimestamp}}</span>
                    <span ng-switch-when="Cancelled"> after {{(build.status.startTimestamp || build.metadata.creationTimestamp) | duration : build.status.completionTimestamp}}</span>
                    <span ng-switch-when="Running"> for <duration-until-now timestamp="build.status.startTimestamp"></duration-until-now></span>
                    <span ng-switch-when="New">, waiting for <duration-until-now timestamp="build.metadata.creationTimestamp"></duration-until-now></span>
                    <span ng-switch-when="Pending"> for <duration-until-now timestamp="build.metadata.creationTimestamp"></duration-until-now></span>
                    <span ng-switch-default>
                      <span ng-if="build.status.startTimestamp">, finished in {{build.status.startTimestamp | duration : build.status.completionTimestamp}}</span>
                      <span ng-if="!build.status.startTimestamp">, waited for {{build.metadata.creationTimestamp | duration : build.status.completionTimestamp}}</span>
                    </span>
                  </span>
                </span>
              </div>
            </td>
            <td data-title="Started">
              <relative-timestamp timestamp="build.status.startTimestamp || build.metadata.creationTimestamp"></relative-timestamp>
              <span>- {{(build.status.startTimestamp || build.metadata.creationTimestamp) | date : 'short'}}</span>
            </td>
            <td data-title="Type">{{build.spec.strategy.type}}</td>
            <td data-title="Source">
              <span ng-if="build.spec.source">
                <span ng-if="build.spec.source.type == 'Git'" ng-bind-html='build.spec.source.git.uri | githubLink : build.spec.source.git.ref : build.spec.source.contextDir | linky' class="word-break"></span>
              </span>
            </td>
          </tr>
        </tbody>
      </table>
    </div>
  </project-page>
</div>
