import { Component, OnInit, OnDestroy, Inject } from '@angular/core';

import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material';
import { NGXLogger } from 'ngx-logger';
import { DeviceType } from '@ucap-webmessenger/core';
import { FileDownloadItem } from '@ucap-webmessenger/api';
import { CommonApiService } from '@ucap-webmessenger/api-common';
import { AppFileService } from '@app/services/file.service';
import { FileInfo, isMedia } from '@ucap-webmessenger/protocol-file';
import { SelectFileInfo } from '@ucap-webmessenger/ui';

export interface FileViewerDialogData {
  fileInfos: FileInfo[];
  selectFileInfo: SelectFileInfo;
  downloadUrl: string;
  userSeq: number;
  deviceType: DeviceType;
  token: string;
}

export interface FileViewerDialogResult {}

@Component({
  selector: 'app-layout-common-file-viewer',
  templateUrl: './file-viewer.dialog.component.html',
  styleUrls: ['./file-viewer.dialog.component.scss']
})
export class FileViewerDialogComponent implements OnInit, OnDestroy {
  isMediaType: boolean;
  fileInfo: {
    fileInfos: FileInfo[];
    selectFileInfo: SelectFileInfo;
  };

  currentFileInfo: FileInfo;

  constructor(
    public dialogRef: MatDialogRef<
      FileViewerDialogData,
      FileViewerDialogResult
    >,
    @Inject(MAT_DIALOG_DATA) public data: FileViewerDialogData,
    private commonApiService: CommonApiService,
    private appFileService: AppFileService,
    private logger: NGXLogger
  ) {
    this.currentFileInfo = this.data.fileInfos.find(
      f => f.seq === this.data.selectFileInfo.attachmentSeq
    );

    if (!this.currentFileInfo) {
      this.logger.warn(
        'file info is exist',
        this.data.fileInfos,
        this.data.selectFileInfo
      );
      this.dialogRef.close();
      return;
    }

    this.isMediaType = isMedia(this.currentFileInfo);

    this.fileInfo = {
      fileInfos: this.data.fileInfos.filter(f => {
        const i = isMedia(f);
        return this.isMediaType ? i : !i;
      }),
      selectFileInfo: this.data.selectFileInfo
    };
  }

  ngOnInit() {}

  ngOnDestroy(): void {}

  onDownload(info: {
    attachmentSeq?: number;
    downloadUrl?: string;
    fileName: string;
    fileDownloadItem: FileDownloadItem;
  }): void {
    this.appFileService.fileTalkDownlod({
      req: {
        userSeq: this.data.userSeq,
        deviceType: this.data.deviceType,
        token: this.data.token,
        attachmentsSeq: info.attachmentSeq,
        fileDownloadItem: info.fileDownloadItem
      },
      directDownloadUrl: info.downloadUrl,
      fileName: info.fileName,
      fileDownloadUrl: this.data.downloadUrl
    });
  }

  onClosedViewer(): void {
    this.dialogRef.close();
  }

  fileDownloadUrl = (attachmentSeq: number) => {
    return this.commonApiService.urlForFileTalkDownload(
      {
        userSeq: this.data.userSeq,
        deviceType: this.data.deviceType,
        token: this.data.token,
        attachmentsSeq: attachmentSeq
      },
      this.data.downloadUrl
    );
  };
}