<h1>
  <my-global-icon iconName="videos" aria-hidden="true"></my-global-icon>
  <ng-container i18n>Videos</ng-container>
</h1>

<p-table
  [value]="videos" [paginator]="totalRecords > 0" [totalRecords]="totalRecords" [rows]="rowsPerPage" [first]="pagination.start"
  [rowsPerPageOptions]="rowsPerPageOptions" [sortField]="sort.field" [sortOrder]="sort.order" dataKey="id" [resizableColumns]="true"
  [(selection)]="selectedRows" [lazy]="true" (onLazyLoad)="loadLazy($event)" [lazyLoadOnInit]="false" [selectionPageOnly]="true"
  [showCurrentPageReport]="true" [currentPageReportTemplate]="getPaginationTemplate()"
  [expandedRowKeys]="expandedRows" [ngClass]="{ loading: loading }"
>
  <ng-template pTemplate="caption">
    <div class="caption">
      <div class="left-buttons">
        <my-action-dropdown
          *ngIf="isInSelectionMode()" i18n-label label="Batch actions" theme="orange"
          [actions]="bulkActions" [entry]="selectedRows"
        >
        </my-action-dropdown>
      </div>

      <div class="ms-auto right-form">
        <my-advanced-input-filter [filters]="inputFilters" (search)="onSearch($event)"></my-advanced-input-filter>

        <my-button i18n-label label="Refresh" icon="refresh" (click)="reloadData()"></my-button>
      </div>

    </div>
  </ng-template>

  <ng-template pTemplate="header">
    <tr>
      <th scope="col" style="width: 40px">
        <p-tableHeaderCheckbox ariaLabel="Select all rows" i18n-ariaLabel></p-tableHeaderCheckbox>
      </th>
      <th scope="col" style="width: 40px;">
        <span i18n class="visually-hidden">More information</span>
      </th>
      <th scope="col" style="width: 70px;">
        <span i18n class="visually-hidden">Actions</span>
      </th>
      <th scope="col" i18n>Video</th>
      <th scope="col" i18n>Info</th>
      <th scope="col" i18n [ngbTooltip]="sortTooltip" container="body" pSortableColumn="localVideoFilesSize">Files <p-sortIcon field="localVideoFilesSize"></p-sortIcon></th>
      <th scope="col" style="width: 150px;" i18n [ngbTooltip]="sortTooltip" container="body" pSortableColumn="publishedAt">Published <p-sortIcon field="publishedAt"></p-sortIcon></th>
    </tr>
  </ng-template>

  <ng-template pTemplate="body" let-expanded="expanded" let-video>

    <tr [pSelectableRow]="video">
      <td class="checkbox-cell">
        <p-tableCheckbox [value]="video" ariaLabel="Select this row" i18n-ariaLabel></p-tableCheckbox>
      </td>

      <td class="expand-cell">
        <my-table-expander-icon [pRowToggler]="video" [expanded]="expanded"></my-table-expander-icon>
      </td>

      <td class="action-cell">
        <my-video-actions-dropdown
          placement="bottom auto" buttonDirection="horizontal" buttonStyled="true" [video]="video" [displayOptions]="videoActionsOptions"
          (videoRemoved)="reloadData()" (videoFilesRemoved)="reloadData()" (transcodingCreated)="reloadData()"
        ></my-video-actions-dropdown>
      </td>

      <td>
        <my-video-cell [video]="video"></my-video-cell>
      </td>

      <td>
        <div>
          @if (video.isLocal) {
            <span class="pt-badge badge-blue" i18n>Local</span>
          } @else {
            <span class="pt-badge badge-purple" i18n>Remote</span>
          }

          <span [ngClass]="getPrivacyBadgeClass(video)" class="pt-badge">{{ video.privacy.label }}</span>

          <span *ngIf="video.nsfw" class="pt-badge badge-red" i18n>NSFW</span>

          <span *ngIf="isUnpublished(video)" class="pt-badge badge-yellow">{{ video.state.label }}</span>

          <span *ngIf="isAccountBlocked(video)" class="pt-badge badge-red" i18n>Account muted</span>
          <span *ngIf="isServerBlocked(video)" class="pt-badge badge-red" i18n>Server muted</span>

          <span *ngIf="isVideoBlocked(video)" class="pt-badge badge-red" i18n>Blocked</span>
        </div>

        <div>
          @for (tag of video.automaticTags; track tag) {
            <a
              i18n-title title="Only display videos with this tag"
              class="pt-badge badge-secondary me-1"
              [routerLink]="[ '.' ]" [queryParams]="{ 'search': buildSearchAutoTag(tag) }"
            >{{ tag }}</a>
          }
        </div>
      </td>

      <td>
        <span *ngIf="hasOriginalFile(video)" class="pt-badge badge-blue" i18n>Original file</span>
        <span *ngIf="hasHLS(video)" class="pt-badge badge-blue" i18n>HLS</span>
        <span *ngIf="hasWebVideos(video)" class="pt-badge badge-blue" i18n>Web Videos ({{ video.files.length }})</span>
        <span *ngIf="video.isLive" class="pt-badge badge-blue" i18n>Live</span>
        <span *ngIf="hasObjectStorage(video)" class="pt-badge badge-purple" i18n>Object storage</span>

        <span *ngIf="!isImport(video) && !video.isLive && video.isLocal">{{ getFilesSize(video) | bytes: 1 }}</span>
      </td>

      <td>
        {{ video.publishedAt | date: 'short' }}
      </td>

    </tr>
  </ng-template>

  <ng-template pTemplate="rowexpansion" let-video>
    <tr>
      <td class="video-info expand-cell" myAutoColspan>
        <div>
          <div class="me-3" *ngIf="hasOriginalFile(video)">
            <ng-container i18n>Original file:</ng-container>

            <ul>
              <li>
                {{ video.videoSource.inputFilename }}: {{ video.videoSource.size | bytes: 1 }}

                <button
                  *ngIf="canRemoveOneFile(video)" class="border-0 p-0"
                  i18n-title title="Delete this file"
                  (click)="removeVideoSourceFile(video)"
                >
                  <my-global-icon iconName="delete"></my-global-icon>
                </button>
              </li>
            </ul>
          </div>

          <div class="me-3" *ngIf="hasWebVideos(video)">
            <ng-container i18n>Web Videos:</ng-container>

            <ul>
              <li *ngFor="let file of video.files">
                <a target="_blank" rel="noopener noreferrer" [href]="file.fileUrl">{{ file.resolution.label }}</a>: {{ file.size | bytes: 1 }}

                <button
                  *ngIf="canRemoveOneFile(video)" class="border-0 p-0"
                  i18n-title title="Delete this file"
                  (click)="removeVideoFile(video, file, 'web-videos')"
                >
                  <my-global-icon iconName="delete"></my-global-icon>
                </button>
              </li>
            </ul>
          </div>

          <div *ngIf="hasHLS(video)">
            <ng-container i18n>HLS:</ng-container>

            <ul>
              <li *ngFor="let file of video.streamingPlaylists[0].files">
                <a target="_blank" rel="noopener noreferrer" [href]="file.fileUrl">{{ file.resolution.label }}</a>: {{ file.size | bytes: 1 }}

                <button
                  *ngIf="canRemoveOneFile(video)" class="border-0 p-0"
                  i18n-title title="Delete this file"
                  (click)="removeVideoFile(video, file, 'hls')"
                >
                  <my-global-icon iconName="delete"></my-global-icon>
                </button>
              </li>
            </ul>
          </div>

          <my-embed class="ms-auto" [video]="video"></my-embed>
        </div>
      </td>
    </tr>
  </ng-template>
</p-table>

<my-video-block #videoBlockModal (videoBlocked)="reloadData()"></my-video-block>
