<!--
 Licensed to the Apache Software Foundation (ASF) under one
 or more contributor license agreements.  See the NOTICE file
 distributed with this work for additional information
 regarding copyright ownership.  The ASF licenses this file
 to you 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.
-->

<a
  nz-dropdown
  [nzDropdownMenu]="menu">
  <span
    nz-icon
    nzType="setting"
    nzTheme="outline"></span>
</a>
<nz-dropdown-menu #menu="nzDropdownMenu">
  <ul nz-menu>
    <li nz-menu-item>
      <nz-switch
        [(ngModel)]="showTimestamp"
        [nzCheckedChildren]="checkedTemplate"
        [nzUnCheckedChildren]="unCheckedTemplate"></nz-switch>
      Show Timestamp
      <ng-template #checkedTemplate
        ><span
          nz-icon
          nzType="check"></span
      ></ng-template>
      <ng-template #unCheckedTemplate
        ><span
          nz-icon
          nzType="close"></span
      ></ng-template>
    </li>
    <li nz-menu-item>
      <nz-switch
        [(ngModel)]="showSource"
        [nzCheckedChildren]="checkedTemplate"
        [nzUnCheckedChildren]="unCheckedTemplate"></nz-switch>
      Show Source
      <ng-template #checkedTemplate
        ><span
          nz-icon
          nzType="check"></span
      ></ng-template>
      <ng-template #unCheckedTemplate
        ><span
          nz-icon
          nzType="close"></span
      ></ng-template>
    </li>
  </ul>
</nz-dropdown-menu>

<nz-list
  nzSize="small"
  class="console-list-container"
  #consoleList>
  <nz-list-item
    *ngFor="let entry of consoleMessages"
    class="console-message-entry"
    nzNoFlex>
    <div
      nz-row
      nzJustify="end"
      nzAlign="top">
      <div
        nz-col
        nzSpan="1">
        <nz-badge
          [nzStatus]="getMessageLabel(entry)"
          [nzTitle]="entry.msgType.name"
          class="type-tag"></nz-badge>
      </div>

      <div
        nz-col
        nzSpan="15">
        <nz-collapse
          *ngIf="entry.message.length !== 0"
          nzGhost>
          <nz-collapse-panel
            [nzShowArrow]="false"
            [nzHeader]="customHeader"
            [nzActive]="false"
            [nzDisabled]="false"
            class="collapse-remove-padding">
            <ng-template #customHeader>
              <div class="collapse-message-header">{{ entry.title }}</div>
            </ng-template>
            <div class="console-message">{{ entry.message }}</div>
          </nz-collapse-panel>
        </nz-collapse>
        <div
          *ngIf="entry.message.length === 0"
          class="console-message">
          {{ entry.title }}
        </div>
      </div>
      <div
        nz-col
        nzSpan="8">
        <div
          nz-row
          nzAlign="top"
          nzJustify="end"
          nzGutter="0">
          <div
            nz-col
            nzFlex="auto">
            <nz-tag
              *ngIf="showSource"
              class="source-tag">
              {{ entry.source }}
            </nz-tag>
          </div>
          <div
            nzcol
            nz-span="12">
            <nz-tag
              *ngIf="showTimestamp"
              class="timestamp-tag">
              {{ (entry.timestamp.seconds * 1000 + entry.timestamp.nanos * 0.000001) | date : "M-d-yy, HH:mm:ss.SSS" }}
            </nz-tag>
            <nz-tag
              *ngIf="entry.workerId"
              class="worker-tag"
              [nzColor]="getWorkerColor(getWorkerIndex(entry.workerId))">
              {{ workerIdToAbbr(entry.workerId) }}
            </nz-tag>
          </div>
        </div>
      </div>
    </div>
  </nz-list-item>
</nz-list>

<nz-input-group
  *ngIf="consoleInputEnabled"
  nzCompact
  [nzAddOnBefore]="addOnBeforeTemplate"
  class="console-input-container">
  <div>
    <button
      (click)="onClickSkipTuples()"
      [disabled]="false"
      nzSize="small"
      nz-tooltip
      nzTooltipTitle="Skip the faulty Tuple"
      nz-button>
      <span
        nz-icon
        nzType="delete-row"
        nzTheme="outline">
      </span>
      Skip Tuple
    </button>
    <button
      (click)="onClickRetryTuples()"
      [disabled]="false"
      nzSize="small"
      nz-tooltip
      nzTooltipTitle="Retry the faulty Tuple"
      nz-button>
      <span
        nz-icon
        nzType="redo"
        nzTheme="outline">
      </span>
      Retry Tuple
    </button>
    <button
      (click)="onClickStep()"
      nz-button
      nzSize="small"
      nz-tooltip
      nzTooltipTitle="Step"
      title="Step">
      <i
        nz-icon
        nzTheme="outline"
        nzType="vertical-left"></i>
      Step
    </button>
    <button
      (click)="onClickContinue()"
      nz-button
      nz-tooltip
      nzTooltipTitle="Continue"
      nzSize="small"
      title="Continue">
      <i
        nz-icon
        nzTheme="outline"
        nzType="right"></i>
      Continue
    </button>
  </div>

  <input
    type="text"
    nz-input
    style="width: calc(100% - 160px)"
    [(ngModel)]="command"
    (keyup.enter)="submitDebugCommand()" />
</nz-input-group>

<ng-template #addOnBeforeTemplate>
  <nz-select [(ngModel)]="targetWorker">
    <nz-option
      *ngFor="let workerId of workerIds"
      [nzLabel]="workerIdToAbbr(workerId)"
      [nzValue]="workerId"></nz-option>
    <nz-option
      [nzLabel]="ALL_WORKERS"
      [nzValue]="ALL_WORKERS"></nz-option>
  </nz-select>
</ng-template>
