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

<nz-card
  [nzBodyStyle]="{padding: '3px'}"
  class="list-item-card"
  [class.selected]="entry.checked"
  [class.has-button-group]="isPrivateSearch"
  [class.editing-description]="editingDescription">
  <div
    (mouseenter)="hovering = true"
    (mouseleave)="hovering = false"
    [routerLink]="entryLink"
    nz-row
    nzAlign="middle">
    <div
      nz-col
      nzFlex="20px">
      <input
        type="checkbox"
        class="large-checkbox"
        [ngClass]="{showc: entry.checked}"
        [checked]="entry.checked"
        (change)="onCheckboxChange(entry)"
        (click)="$event.stopPropagation()"
        *ngIf="isPrivateSearch && entry.type==='workflow'" />
    </div>

    <div
      nz-col
      nzFlex="0"
      class="type-icon">
      <i
        nz-icon
        [nzType]="iconType"></i>
    </div>

    <div
      nz-col
      nzFlex="0"
      class="workflow-id">
      <i>#{{ entry.id }}</i>
    </div>

    <div
      nz-col
      nzFlex="30px"
      *ngIf="isPrivateSearch">
      <div class="edit-button">
        <button
          nz-button
          nzType="text"
          title="Rename"
          (click)="onEditName();$event.stopPropagation()">
          <i
            nz-icon
            nzType="edit"></i>
        </button>
      </div>

      <div class="edit-button">
        <button
          nz-button
          nzType="text"
          title="Edit Description"
          (click)="onEditDescription();$event.stopPropagation()">
          <i
            nz-icon
            nzType="form"></i>
        </button>
      </div>
    </div>

    <div
      nz-col
      nzFlex="1"
      class="resource-name-group">
      <div
        class="resource-name truncate-single-line"
        *ngIf="!editingName">
        {{ entry.name }}
      </div>
      <input
        *ngIf="editingName"
        #nameInput
        class="resource-name-edit-input"
        [(ngModel)]="entry.name"
        (blur)="confirmUpdateCustomName(entry.name)"
        (keydown.enter)="confirmUpdateCustomName(entry.name)"
        (click)="$event.stopPropagation()"
        autofocus />

      <div
        class="resource-description truncate-single-line"
        (click)="onEditDescription(); $event.stopPropagation()">
        {{ renderedDescription ? renderedDescription.slice(0, 200) : (hovering && editable) ? 'Write a description...' :
        '' }}
      </div>
    </div>

    <div
      nz-col
      nzFlex="50px"
      class="resource-info">
      <texera-user-avatar
        [googleAvatar]="entry.ownerGoogleAvatar"
        userColor="#1E90FF"
        [userName]="entry.ownerName || 'User'"
        [isOwner]="entry.ownerId === this.currentUid">
      </texera-user-avatar>
    </div>

    <div
      nz-col
      nzFlex="55px"
      class="resource-info">
      Views:<br />
      {{ formatCount(this.viewCount) }}
    </div>

    <div
      nz-col
      nzFlex="75px"
      class="resource-info">
      Size: <br />
      {{ formatSize(size) }}
    </div>

    <div
      nz-col
      nzFlex="90px"
      class="resource-info">
      Created:<br />
      {{ formatTime(entry.creationTime) }}
    </div>

    <div
      nz-col
      nzFlex="90px"
      class="resource-info">
      Edited:<br />
      {{ formatTime(entry.lastModifiedTime) }}
    </div>

    <div
      nz-col
      nzFlex="0"
      class="resource-info">
      <button
        *ngIf="!isPrivateSearch"
        nz-button
        nzType="default"
        class="like-button"
        title="Like"
        [disabled]="!currentUid"
        (click)="toggleLike(); $event.stopPropagation()"
        [attr.disabled]="!currentUid ? true : null">
        <i
          nz-icon
          [nzType]="'like'"
          [ngClass]="{liked: isLiked}">
        </i>
        <span>{{ formatCount(this.likeCount) }}</span>
      </button>
    </div>
  </div>

  <div
    *ngIf="isPrivateSearch"
    class="button-group">
    <button
      *ngIf="entry.type==='workflow'"
      nz-button
      nzType="text"
      title="Detail"
      (click)="openDetailModal(this.entry.id)">
      <i
        nz-icon
        nzType="eye"></i>
    </button>
    <button
      nz-button
      nzType="text"
      title="Share"
      (click)="onClickOpenShareAccess()">
      <i
        nz-icon
        nzType="share-alt"></i>
    </button>
    <button
      nz-button
      nzType="text"
      *ngIf="entry.type==='workflow'"
      title="Copy"
      (click)="duplicated.emit()">
      <i
        nz-icon
        nzType="copy"></i>
    </button>
    <button
      nz-button
      nzType="text"
      *ngIf="entry.type === 'workflow' || entry.type === 'dataset'"
      title="Download"
      (click)="onClickDownload()">
      <i
        nz-icon
        nzType="cloud-download"></i>
    </button>
    <button
      nz-button
      nzType="text"
      title="Delete"
      [disabled]="disableDelete"
      (nzOnConfirm)="deleted.emit()"
      nz-popconfirm
      nzPopconfirmTitle="Confirm to delete this item.">
      <i
        nz-icon
        nzType="delete"></i>
    </button>
  </div>
</nz-card>
