// Copyright 2019 The Nomulus Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

{namespace google.registry.gradle.plugin}

{template .coverPage}
  {@param title: string}
  {@param cssFiles: list<trusted_resource_uri>}
  {@param projectState: string}
  {@param invocation: string}
  {@param tasksByState: map<string, list<[uniqueName: string, description: string, log: string, reports: map<string, string>]>>}

  <title>{$title}</title>
  {for $cssFile in $cssFiles}
    <link rel="stylesheet" type="text/css" href="{$cssFile}">
  {/for}
  <body>
    <div class="project">
      <h1 class="project_title {'task_state_' + $projectState}">{$title}</h1>
      <span class="project_subtitle">
        Build results for <span class="project_invocation">{$invocation}</span>
      </span>

      {for $taskState in mapKeys($tasksByState)}
        {if length($tasksByState[$taskState]) > 0}
          {call .tasksOfState}
            {param state: $taskState /}
            {param tasks: $tasksByState[$taskState] /}
          {/call}
        {/if}
      {/for}

    </div>
  </body>
{/template}

{template .tasksOfState}
  {@param state: string}
  {@param tasks: list<[uniqueName: string, description: string, log: string, reports: map<string, string>]>}

  <div class="{'task_state_' + $state}">
    <p>{$state}</p>
    // Place the tasks with actual reports first, since those are more likely to be useful
    {for $task in $tasks}
      {if length(mapKeys($task.reports)) > 0}
        {call .task}
          {param task: $task /}
        {/call}
      {/if}
    {/for}
    // Followup with reports without links
    {for $task in $tasks}
      {if length(mapKeys($task.reports)) == 0}
        {call .task}
          {param task: $task /}
        {/call}
      {/if}
    {/for}
  </div>
{/template}

{template .task}
  {@param task: [uniqueName: string, description: string, log: string, reports: map<string, string>]}
  {call .taskInternal}
    {param uniqueName: $task.uniqueName /}
    {param description: $task.description /}
    {param log: $task.log /}
    {param reports: $task.reports /}
  {/call}
{/template}

{template .taskInternal}
  {@param uniqueName: string}
  {@param description: string}
  {@param log: string}
  {@param reports: map<string, string>}

  <div class="task">
    <span class="task_name">{$uniqueName}</span>
    <span class="task_description">{$description}</span>
    <span class="report_links">
      {if $log}
        <a href="{$log}">[log]</a>
      {else}
        <span class="report_link_broken">[log]</span>
      {/if}
      {for $type in mapKeys($reports)}
        {if $reports[$type]}
          <a href="{$reports[$type]}">[{$type}]</a>
        {else}
          <span class="report_link_broken">[{$type}]</span>
        {/if}
      {/for}
    </span>
  </div>
{/template}
