<!--
Copyright 2017 The Kubernetes Authors.

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

<div class="kd-logs-container">
  <kd-card [expandable]="false">
    <div title
         [fxLayout]="'row'"
         [fxFlex]="100">
      <span class="kd-logs-header-text"
            i18n>Logs from</span>
      <mat-form-field>
        <mat-select [(ngModel)]="container"
                    class="kd-logs-toolbar-select"
                    (selectionChange)="onContainerChange()">
          <mat-optgroup label="Containers"
                        i18n-label
                        *ngIf="logSources?.containerNames?.length > 0">
            <mat-option *ngFor="let item of logSources?.containerNames"
                        [value]="item">
              {{item}}
            </mat-option>
          </mat-optgroup>
          <mat-optgroup label="Init Containers"
                        i18n-label
                        *ngIf="logSources?.initContainerNames?.length > 0">
            <mat-option *ngFor="let item of logSources?.initContainerNames"
                        [value]="item">
              {{item}}
            </mat-option>
          </mat-optgroup>
        </mat-select>
      </mat-form-field>
      <span class="kd-logs-header-text"
            i18n>in</span>
      <mat-form-field class="kd-form-field">
        <mat-select [(ngModel)]="pod"
                    class="kd-logs-toolbar-select"
                    (selectionChange)="loadNewest()">
          <mat-option *ngFor="let item of logSources?.podNames"
                      [value]="item">
            {{item}}
          </mat-option>
        </mat-select>
      </mat-form-field>
      <div class="kd-logs-style-buttons">
        <button mat-icon-button
                i18n-matTooltip
                matTooltip="Download logs"
                (click)="downloadLog()">
          <mat-icon>file_download</mat-icon>
        </button>
        <button mat-icon-button
                [matMenuTriggerFor]="kdMenu">
          <mat-icon>more_vert</mat-icon>
        </button>
      </div>
    </div>

    <div content
         class="kd-log-view"
         [ngClass]="{'kd-logs-text-color-invert': logService?.getInverted(), 'kd-logs-text-color': !logService?.getInverted()}">
      <div kdLoadingSpinner
           [isLoading]="isLoading"></div>
      <div class="kd-log-view-container"
           #logViewContainer
           (scroll)="onLogsScroll()">
        <div class="kd-virtual-repeat-sizer"></div>
        <div *ngFor="let item of logsSet"
             class="kd-logs-element"
             [ngClass]="{'kd-logs-element-compact': logService.getCompact(), 'kd-logs-element': !logService.getCompact()}">
          <span class="kd-log-line"
                [innerHTML]="item | kdSafeHtml:'Colored'"></span>
        </div>
      </div>

      <div fxLayout="row"
           class="kd-muted">
        <span class="kd-logs-info"
              *ngIf="logsSet?.length > 1"
              i18n>
          Logs from <kd-date [date]="podLogs?.info.fromDate"
                   format="short"></kd-date> to <kd-date [date]="podLogs?.info.toDate"
                   format="short"></kd-date> UTC
        </span>
        <span fxFlex></span>
        <div class="kd-list-pagination-buttons">
          <button mat-icon-button
                  (click)="loadOldest()">
            <mat-icon>first_page</mat-icon>
          </button>
          <button mat-icon-button
                  (click)="loadOlder()">
            <mat-icon>chevron_left</mat-icon>
          </button>
          <button mat-icon-button
                  (click)="loadNewer()">
            <mat-icon>chevron_right</mat-icon>
          </button>
          <button mat-icon-button
                  (click)="loadNewest()">
            <mat-icon>last_page</mat-icon>
          </button>
        </div>
      </div>
    </div>
  </kd-card>
</div>

<mat-menu #kdMenu="matMenu">
  <button mat-menu-item
          (click)="onTextColorChange()">
    <mat-icon>{{logService?.getInverted() ? 'check_box' : 'check_box_outline_blank'}}</mat-icon>
    <span i18n>Invert colors</span>
  </button>
  <button mat-menu-item
          (click)="onFontSizeChange()">
    <mat-icon>{{logService.getCompact() ? 'check_box' : 'check_box_outline_blank'}}</mat-icon>
    <span i18n>Reduce font size</span>
  </button>
  <button mat-menu-item
          (click)="onShowTimestamp()">
    <mat-icon>{{logService.getShowTimestamp() ? 'check_box' : 'check_box_outline_blank'}}</mat-icon>
    <span i18n>Show timestamps</span>
  </button>
  <button mat-menu-item
          (click)="toggleLogAutoRefresh()">
    <mat-icon>{{logService.getAutoRefresh() ? 'check_box' : 'check_box_outline_blank'}}</mat-icon>
    <span i18n>Auto-refresh (every {{refreshInterval / 1000}} s.)</span>
  </button>
  <button mat-menu-item
          (click)="toggleLogFollow()">
    <mat-icon>{{logService.getFollowing() ? 'check_box' : 'check_box_outline_blank'}}</mat-icon>
    <span i18n>Follow logs</span>
  </button>
  <button mat-menu-item
          (click)="onPreviousChange()">
    <mat-icon>{{logService.getPrevious() ? 'check_box' : 'check_box_outline_blank'}}</mat-icon>
    <span i18n>Show previous logs</span>
  </button>
</mat-menu>
