import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
import {UploadMetaModel} from '../models/uploadMetaModel';
import {UploadAttachService} from '../service/upload.attach.service';

@Component({
  selector: 'app-uploading-progress-card',
  templateUrl: './uploading-progress-card.component.html',
  styleUrls: ['./uploading-progress-card.component.scss']
})
export class UploadingProgressCardComponent implements OnInit {

  @Input() MetaId = 0 ;
  @Input() FileName = '';
  @Input() UniqueId = '';
  @Input() Progress = 0 ;
  @Input() IsDuplicate = false ;
  @Output() Complete: EventEmitter<string> = new EventEmitter<string>();

  public Operation = 'Uploading';
  public icon = '';
  public thumbImage = UploadMetaModel.thumbDefault();
  // sub steps after uploading
  public thumbSuccess = false;
  public pdfSuccess = false;
  public previewSuccess = false;
  public analysisSuccess = false;
  // indicator
  public analyzing = false;
  public currentStep = '';

  private waitingTimer: any;

  constructor( private uas: UploadAttachService) { }

  ngOnInit(): void {
    const suffix = this.FileName.split('.').pop();
    this.icon = UploadMetaModel.iconUrlBySuffix(suffix);
    this.prePareToStartAnalysis();
    return;
  }

  private prePareToStartAnalysis(): void {
    this.waitingTimer = setInterval(
      () => {
        if ( this.Progress >= 100 && this.MetaId !== 0) {
          clearInterval(this.waitingTimer);
          this.startAnalysis();
        }
      }
    , 500);
  }
  public startAnalysis(): void {
      this.thumbSuccess = false;
      this.pdfSuccess = false;
      this.previewSuccess = false;
      this.analysisSuccess = false;

      this.analyzing = true;
      this.Operation = 'Processing';
      this.createPdf();
  }

  private createPdf(): void {
    this.currentStep = 'pdf';
    this.uas.createPDF(this.MetaId).subscribe(
      resp => {    this.pdfSuccess = resp;   },
      error => {  this.pdfSuccess = false; this.createPreview(); },
      () => {  this.createPreview(); }
    );
  }

  private createPreview(): void {
    this.currentStep = 'preview';
    this.uas.createPreview(this.MetaId).subscribe(
      resp => {    this.previewSuccess = resp; },
      error => {  this.previewSuccess = false; this.createThumb(); },
      () => {  this.createThumb(); }
    );
  }

  private createThumb(): void {
    this.currentStep = 'thumb';
    this.uas.createThumb(this.MetaId).subscribe(
      resp => {    this.thumbSuccess = resp;  },
      error => {  this.thumbSuccess = false; this.createAnalysis(); },
      () => {  this.createAnalysis(); }
    );
  }


  private createAnalysis(): void {
    this.currentStep = 'analysis';
    this.uas.createAnalysis(this.MetaId).subscribe(
      resp => {    this.analysisSuccess = true;   },
      error => {  this.analysisSuccess = false; this.endAnalysis();  },
      () => {  this.endAnalysis(); }
    );
  }

  private endAnalysis(): void{
    this.analyzing = false;
    this.Operation = 'Done';
    this.currentStep = '';
    this.Complete.emit(this.UniqueId);
  }


}
