import { Injectable } from '@angular/core';
import { Organization } from '../../models/organization';
import { Plant } from '../../models/plant';

import { of as observableOf, Observable, throwError } from 'rxjs';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { retry, catchError, map, timeout } from 'rxjs/operators';

import { environment } from '@environments/environment';

@Injectable({
  providedIn: 'root'
})
export class AssetsService {

  time:number = 10000;

  constructor(private http: HttpClient) {
  }

  getAssets() {
    return this.http.get(`${environment.api2Url}/users/api_v1/movies`)
    .pipe(
      timeout(this.time),
      map(response =>{
        console.log(response);
        return response;
      }),
      catchError(this.errorHandl)
    )
  }
  
  getAssetsId() {
    return this.http.get(`${environment.api2Url}/users/api_v1/movies/1`)
    .pipe(
      timeout(this.time),
      map(response =>{
        console.log(response);
        return response;
      }),
      catchError(this.errorHandl)
    )
  }
  

  errorHandl(error) {
    let errorMessage = '';
    if(error.error instanceof ErrorEvent) {
      // Get client-side error
      errorMessage = error.error.message;
    } else {
      // Get server-side error
      errorMessage = `Error Code: ${error.status}\nMessage: ${error.message}`;
    }
    console.log(error);
    return throwError(errorMessage);
 }

}
