import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {AuthService} from './auth.service';
import {Observable} from 'rxjs';
import {UploadMetalList, UploadMetaModel} from '../models/uploadMetaModel';
import {UploadAnalysisModel} from '../models/upload.analysis.model';


@Injectable({providedIn: 'root'})
export class UploadAttachService {
  constructor(private http: HttpClient, private auth: AuthService) {
  }

  public getUploadMeta(id: number): Observable<UploadMetaModel> {
    return this.http.get<UploadMetaModel>(this.auth.getUrl('upload-meta/' + id));
  }

  public getUploadAnalysis(id: number): Observable<UploadAnalysisModel> {
    return this.http.get<UploadAnalysisModel>(this.auth.getUrl('upload-analysis/' + id));
  }

  public getUploadMetaList(Skip: number, Take: number, Filter: string): Observable<UploadMetalList> {
    return this.http.post<UploadMetalList>(this.auth.getUrl('upload-meta-list/'), {Skip, Take, Filter});
  }

  public deleteUpload(id: number): Observable<UploadMetaModel> {
    return this.http.delete<UploadMetaModel>(this.auth.getUrl('upload/' + id));
  }

  public getUploadAsJpgUrl(id: number): string {
    const ts = Date.now();
    return this.auth.getUrl('upload-as-image/' + id  + '?date=' + ts);
  }

  public getUploadAsJpgUrlDefault(): string {
    return this.auth.getUrl('upload-as-image/default');
  }

  public getUploadAsPdfUrl(id: number): string {
    const ts = Date.now();
    return this.auth.getUrl('upload-as-pdf/' + id + '?date=' + ts);
  }

  public getUploadAsOriginalUrl(id: number): string {
    const ts = Date.now();
    return this.auth.getUrl('upload-original/' + id + '?date=' + ts + '&download=force');
  }

  public getUploadAsPdfUrlDefault(): string {
    const ts = Date.now();
    return this.auth.getUrl('upload-as-pdf/default' + '?date=' + ts);
  }

  public getUploadAsPdfUrlForDownload(id: number): string {
    return this.auth.getUrl('upload-as-pdf/' + id + '?download=force');
  }

  public getUploadAsThumbnailUrl(id: number): string {
    return this.auth.getUrl('upload-as-thumbnail/' + id );
  }


  public createThumb(id: number): Observable<boolean> {
    return this.http.put<boolean>(this.auth.getUrl('upload-as-thumbnail/' + id), {});
  }

  public createPreview(id: number): Observable<boolean>{
    return this.http.put<boolean>(this.auth.getUrl('upload-as-image/' + id), {});
  }

  public createPDF(id: number): Observable<boolean>{
    return this.http.put<boolean>(this.auth.getUrl('upload-as-pdf/' + id), {});
  }

  public createAnalysis(id: number): Observable<UploadAnalysisModel> {
    return this.http.put<UploadAnalysisModel>(this.auth.getUrl('upload-analysis/' + id), {});
  }
}
