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 InvestmentsService {
  time: number = 10000;

  constructor(private http: HttpClient) {}

  // Obtener listado de propuestas de inversiones
  getProposalInvestmentsList(type: string = "") {
    if (type != ""){
      return this.http
      .get<any>(`${environment.productionApiUrl}/inversiones?tipo=`+type, {})
      .pipe(
        map(response => {
          return response;
        }),
        catchError(this.errorHandl)
      );  
    }
    return this.http
      .get<any>(`${environment.productionApiUrl}/inversiones`, {})
      .pipe(
        map(response => {
          return response;
        }),
        catchError(this.errorHandl)
      );
  }

  // Obtener propuesta de inversion segun ID
  getProposalInvestment(id: string) {
    return this.http
      .get<any>(`${environment.productionApiUrl}/inversion/${id}`)
      .pipe(
        timeout(this.time),
        map(response => {
          return response;
        }),
        catchError(this.errorHandl)
      );
  }

  getInvestmentProposalLogs(id: string) {
    return this.http
      .get<any>(`${environment.productionApiUrl}/auditoria/${id}`)
      .pipe(
        timeout(this.time),
        map(response => {
          return response;
        }),
        catchError(this.errorHandl)
      );
  }

  createProposalInvestment(inversion: Object): Observable<boolean> {
    return this.http
      .post<any>(`${environment.productionApiUrl}/inversion`, inversion)
      .pipe(
        map(response => {
          return response;
        }),
        catchError(this.errorHandl)
      );
  }

  updateProposalInvestment(id: string, inversion: Object): Observable<boolean> {
    return this.http
      .put<any>(`${environment.productionApiUrl}/inversion/${id}`, inversion)
      .pipe(
        map(response => {
          return response;
        }),
        catchError(this.errorHandl)
      );
  }

  sendReviewProposalInvestment(inversion: Object): Observable<boolean> {
    return this.http
      .post<any>(`${environment.productionApiUrl}/auditoria`, inversion)
      .pipe(
        map(response => {
          return response;
        }),
        catchError(this.errorHandl)
      );
  }

  sendProposalInvestmentToNextStep(inversion: Object): Observable<boolean> {
    return this.http
      .post<any>(`${environment.productionApiUrl}/auditoria`, inversion)
      .pipe(
        map(response => {
          return response;
        }),
        catchError(this.errorHandl)
      );
  }

  sendLiquidationFile(inversion: Object): Observable<boolean> {
    return this.http
      .post<any>(`${environment.productionApiUrl}/hoja-liquidacion`, inversion)
      .pipe(
        map(response => {
          return response;
        }),
        catchError(this.errorHandl)
      );
  }

  sendPaymentInfoProposalInvestment(
    id: string,
    inversion: Object
  ): Observable<boolean> {
    return this.http
      .post<any>(`${environment.productionApiUrl}/pagos/${id}`, inversion)
      .pipe(
        map(response => {
          return response;
        }),
        catchError(this.errorHandl)
      );
  }

  updatePaymentInfoProposalInvestment(
    id: string,
    inversion: Object
  ): Observable<boolean> {
    return this.http
      .put<any>(`${environment.productionApiUrl}/pagos/${id}`, inversion)
      .pipe(
        map(response => {
          return response;
        }),
        catchError(this.errorHandl)
      );
  }

  getPaymentInfoProposalInvestment(id: string): Observable<boolean> {
    return this.http
      .get<any>(`${environment.productionApiUrl}/pagos/${id}`)
      .pipe(
        map(response => {
          return response;
        }),
        catchError(this.errorHandl)
      );
  }

  updateInstrumentProjection(id: string, list: any): Observable<boolean> {
    return this.http
      .put<any>(`${environment.productionApiUrl}/proyeccion/${id}`, list)
      .pipe(
        map(response => {
          return response;
        }),
        catchError(this.errorHandl)
      );
  }

  getAvailableUsers(id: any): Observable<boolean> {
    return this.http
      .get<any>(
        `${environment.productionApiUrl}/usuarios/disponibles?inversion=${id}`
      )
      .pipe(
        map(response => {
          return response;
        }),
        catchError(this.errorHandl)
      );
  }

  //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);
  }
}
