import {Component, EventEmitter, Input, OnInit, Output, ViewChild} from '@angular/core';
import {Router} from '@angular/router';
import {ImagePopupDialogComponent} from '../image-popup-dialog/image-popup-dialog.component';
import {AuthService} from '../service/auth.service';
import {UploadMetaModel} from '../models/uploadMetaModel';
import {UploadAttachService} from '../service/upload.attach.service';



@Component({
  selector: 'app-upload-cards',
  templateUrl: './upload-cards.component.html',
  styleUrls: ['./upload-cards.component.scss']
})
export class UploadCardsComponent implements OnInit {
  @Input() uploadId: number;
  @Input() icon  = '';
  @ViewChild('imgBox', {static: true}) imgBox: ImagePopupDialogComponent;
  public uploadMeta: UploadMetaModel = new UploadMetaModel({});

  public thumbImage = 'https://svr2021.lawipac.com:8080/api/v1/upload-as-thumbnail/31';
  public fullImage = '';

  constructor(private router: Router, private auth: AuthService, private uas: UploadAttachService) { }

  ngOnInit(): void {
    let url = this.auth.getUrl('upload-as-thumbnail/' + this.uploadId);
    this.thumbImage = url ;
    url = this.auth.getUrl('upload-as-image/' + this.uploadId);
    this.fullImage = url;

    this.uas.getUploadMeta(this.uploadId).subscribe( resp => {
      this.uploadMeta = new UploadMetaModel(resp);
      this.icon = this.uploadMeta.iconUrl;
    });
  }

  public onNavigateTo(id: number ): void{
    this.router.navigate(['/upload-details/' + id]);
  }
  public onDownload(id: number ): void{
    this.router.navigate(['/upload-details/' + id]);
  }
  public onSearch(id: number ): void{
    this.popupImage(this.fullImage);
    // this.popupImage(this.thumbImage);
  }

  public popupImage(url: string): void {
    this.imgBox.showImg(url, this.uploadId + ' - ' + this.uploadMeta.FileName);
  }

}
