import { Injectable } from "@angular/core";

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 CatalogsService {
  time: number = 6000;

  constructor(private http: HttpClient) {}

  getGenericURL(url: string) {
    return this.http
      .get<any>(`${environment.productionApiUrl}/${url}`, {})
      .pipe(
        map(response => {
          return response;
        }),
        catchError(this.errorHandl)
      );
  }
  // paises
  getCountries() {
    return this.http
      .get<any>(`${environment.productionApiUrl}/paises`, {})
      .pipe(
        map(response => {
          return response;
        }),
        catchError(this.errorHandl)
      );
  }

  // empresas
  getCompanies() {
    return this.http
      .get<any>(`${environment.productionApiUrl}/empresas`, {})
      .pipe(
        map(response => {
          return response;
        }),
        catchError(this.errorHandl)
      );
  }

  // entidades-financieras
  getFinancialEntities() {
    return this.http
      .get<any>(`${environment.productionApiUrl}/entidades-financieras`, {})
      .pipe(
        map(response => {
          return response;
        }),
        catchError(this.errorHandl)
      );
  }

  // origenes-fondos
  getFundsOrigins() {
    return this.http
      .get<any>(`${environment.productionApiUrl}/origenes-fondos`, {})
      .pipe(
        map(response => {
          return response;
        }),
        catchError(this.errorHandl)
      );
  }

  // periodicidades
  getPeriodicities() {
    return this.http
      .get<any>(`${environment.productionApiUrl}/periodicidades`, {})
      .pipe(
        map(response => {
          return response;
        }),
        catchError(this.errorHandl)
      );
  }

  // plazos
  getPaymentTerms() {
    return this.http
      .get<any>(`${environment.productionApiUrl}/plazos`, {})
      .pipe(
        map(response => {
          return response;
        }),
        catchError(this.errorHandl)
      );
  }

  // tipos-bases
  getBaseTypes() {
    return this.http
      .get<any>(`${environment.productionApiUrl}/tipos-bases`, {})
      .pipe(
        map(response => {
          return response;
        }),
        catchError(this.errorHandl)
      );
  }

  // tipos-emisores
  getEmitterTypes() {
    return this.http
      .get<any>(`${environment.productionApiUrl}/tipos-emisores`, {})
      .pipe(
        map(response => {
          return response;
        }),
        catchError(this.errorHandl)
      );
  }

  // tipos-entidades
  getEntityTypes() {
    return this.http
      .get<any>(`${environment.productionApiUrl}/tipos-entidades`, {})
      .pipe(
        map(response => {
          return response;
        }),
        catchError(this.errorHandl)
      );
  }

  // tipos-instrumentos
  getInstrumentTypes() {
    return this.http
      .get<any>(`${environment.productionApiUrl}/tipos-instrumentos`, {})
      .pipe(
        map(response => {
          return response;
        }),
        catchError(this.errorHandl)
      );
  }

  // tipos-mercados
  getMarketTypes() {
    return this.http
      .get<any>(`${environment.productionApiUrl}/tipos-mercados`, {})
      .pipe(
        map(response => {
          return response;
        }),
        catchError(this.errorHandl)
      );
  }

  // tipos-operaciones
  getOperationTypes() {
    return this.http
      .get<any>(`${environment.productionApiUrl}/tipos-operaciones`, {})
      .pipe(
        map(response => {
          return response;
        }),
        catchError(this.errorHandl)
      );
  }

  // tipos-pagos
  getPaymentTypes() {
    return this.http
      .get<any>(`${environment.productionApiUrl}/tipos-pagos`, {})
      .pipe(
        map(response => {
          return response;
        }),
        catchError(this.errorHandl)
      );
  }

  // tipos-rentas
  getRevenueTypes() {
    return this.http
      .get<any>(`${environment.productionApiUrl}/tipos-rentas`, {})
      .pipe(
        map(response => {
          return response;
        }),
        catchError(this.errorHandl)
      );
  }

  // tipos-tasas
  getRateTypes() {
    return this.http
      .get<any>(`${environment.productionApiUrl}/tipos-tasas`, {})
      .pipe(
        map(response => {
          return response;
        }),
        catchError(this.errorHandl)
      );
  }

  // calificadoras
  getRateAgencies() {
    return this.http
      .get<any>(`${environment.productionApiUrl}/calificadoras`, {})
      .pipe(
        map(response => {
          return response;
        }),
        catchError(this.errorHandl)
      );
  }

  // calificaciones
  getScores() {
    return this.http
      .get<any>(`${environment.productionApiUrl}/calificaciones`, {})
      .pipe(
        map(response => {
          return response;
        }),
        catchError(this.errorHandl)
      );
  }

  // formato ingreso
  getIncomeFormat() {
    return this.http
      .get<any>(`${environment.productionApiUrl}/formato-ingresos`, {})
      .pipe(
        map(response => {
          return response;
        }),
        catchError(this.errorHandl)
      );
  }

  /**
 * 
 * api/calificacion/<int:pk>
^ api/calificaciones
^ api/calificadora/<int:pk>
^ api/calificadoras
^ api/empresa/<int:pk>
^ api/empresas
^ api/entidad-financiera/<int:pk>
^ api/entidades-financieras
^ api/estado-inversion/<int:pk>
^ api/estados-inversiones

 * 
 * 
 * 
 */

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