import {HttpClient} from '@angular/common/http';
import {Injectable} from '@angular/core';
import {NGXLogger} from 'ngx-logger';
import {environment} from '../../environments/environment';
import {Observable} from 'rxjs';
import {tap} from 'rxjs/operators';
import {GetImageResponse, ImagePostResponse, StorageHealth, Validity} from '../interfaces/interface';

@Injectable()
export class RestService {
    constructor(
    private logger: NGXLogger,
    private http: HttpClient
  ) {
  }

  private currentLocation = 'http://' + environment.location + ':' + environment.port;
  private imageUrl = this.currentLocation + '/image';
  private getImageUrl = this.imageUrl + '/get';
  private deleteImageUrl = this.imageUrl + '/delete';
  private postImageUrl = this.imageUrl + '/post';
  private updateImageUrl = this.imageUrl + '/update';
  private getValidityOfImageUrl = this.imageUrl + '/status';
  private healthCheckUrl = this.currentLocation + '/check';

  public getAllImages(): Observable<any> {
    return this.http.get<any>(this.getImageUrl + '/all').pipe(
      tap(next => this.logger.debug('getAllImages', next))
    );
  }

  public getImage(id: string): Observable<GetImageResponse> {
    return this.http.get<GetImageResponse>(this.getImageUrl + '/' + id).pipe(
      tap(next => this.logger.debug('getImage', id, next))
    );
  }

  public getImageVersion(id: string, version: string): Observable<any> {
    return this.http.get<any>(this.getImageUrl + '/' + id + '/version/' + version).pipe(
      tap(next => this.logger.debug('getImageVersion', id, version, next))
    );
  }

  public deleteAllImages(): Observable<any> {
    return this.http.delete<any>(this.deleteImageUrl + 'all').pipe(
      tap(next => this.logger.debug('deleteAllImages', next))
    );
  }

  public deleteImage(filename: string): Observable<any> {
    return this.http.delete<any>(this.deleteImageUrl + '/' + filename).pipe(
      tap(next => this.logger.debug('deleteImage', filename, next))
    );
  }

  public postImage(imageForm: FormData): Observable<ImagePostResponse> {
    return this.http.post<ImagePostResponse>(this.postImageUrl, imageForm).pipe(
      tap(next => this.logger.debug('postImage', imageForm, next))
    );
  }

  // public getPostImageUrl(): string {
  //   return this.postImageUrl;
  // }

  public updateImage(id: string, imageForm: FormData): Observable<any> {
    return this.http.put<FormData>(this.updateImageUrl + '/' + id, imageForm).pipe(
      tap(next => this.logger.debug('updateImage', id, imageForm, next))
    );
  }

  // public getUpdateImageUrl(filenameWithExtension: string): string {
  //   return this.updateImageUrl + '/' + RestService.stripUploadFileName(filenameWithExtension);
  // }

  public healthCheck(): Observable<StorageHealth> {
    return this.http.get<StorageHealth>(this.healthCheckUrl).pipe(
      tap(next => this.logger.debug('healthCheck', next))
    );
  }

  public checkImageValid(id: string): Observable<Validity> {
    return this.http.get<Validity>(this.getValidityOfImageUrl + '/' + id).pipe(
      tap(next => this.logger.debug('checkValid', id, next))
    );
  }
}
