import { Component, OnInit, Input } from '@angular/core';
import { AuthService } from 'app/services/auth.service';
import { Utils } from 'app/utils';
import { DomSanitizer, SafeUrl } from '@angular/platform-browser';
import { SafeResourceUrl } from '@angular/platform-browser/src/security/dom_sanitization_service';

@Component({
  selector: 'app-ta-candidate-full-details-card',
  templateUrl: './ta-candidate-full-details-card.component.html',
  styleUrls: ['./ta-candidate-full-details-card.component.scss']
})
export class TaCandidateFullDetailsCardComponent implements OnInit {

  @Input() userData: any;
  dataUrl: SafeUrl;
  resourceUrl: SafeResourceUrl;
  fileName: string;
  canShowResumeOnUI = false;
  showResumeOnUI = false;

  constructor(
    private authService: AuthService,
    private sanitizer: DomSanitizer
  ) { }

  ngOnInit() {
    const resumeFileId = this.userData.userInfoMstr.pgUserFileMapping.find(file => file.fileType === 'RESUME');

    if (resumeFileId) {
      this.authService.getFile(resumeFileId.fileId).then(resp => {
        this.dataUrl = this.sanitizer.bypassSecurityTrustUrl(Utils.fileToDataURL(resp[0]));
        this.resourceUrl = this.sanitizer.bypassSecurityTrustResourceUrl(Utils.fileToDataURL(resp[0]));
        this.fileName = resp[0].fileName;
        this.canShowResumeOnUI = /pdf/i.test(resp[0].contentType);
      });
    }
  }

  toggleResume() {
    this.showResumeOnUI = !this.showResumeOnUI;
  }

}
