import { Injectable } from '@angular/core';
import { Organization } from '../models/organization';
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';
import { organizationsData } from '../data/organizations.data';


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

  private listData: Organization[] = organizationsData;

  constructor(private http: HttpClient) {}

  httpOptions = {
    headers: new HttpHeaders({
      'Content-Type': 'application/json; charset=utf-8',
      'Access-Control-Allow-Origin': '*',
    })
  }
 
  getListOrganizations() {
    return observableOf(this.listData);
  }

  getOrganizations(): Observable<any> {
    return this.http.get<any>(`${environment.apiUrl}/organizations`)
    .pipe(
      timeout(5000),
      map(response =>{
        return response;
      }),
      catchError(this.errorHandl)
    )
  }


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

  createOrganization(organization: { name :string, address :string, 
    city :string, country :string, contactName :string,
    contactNumber: string, assets:any}): Observable<boolean> {
      return this.http.post<any>(`${environment.productionApiUrl}/organizations`, organization)
      .pipe(
        map(response => {
          return response;
        }),
        catchError(this.errorHandl)
      )  
  }
  
  updateOrganization(id:string, organization: { name :string, address :string, 
    city :string, country :string, contactName :string,
    contactNumber: string, assets:any}): Observable<boolean> {
      return this.http.put<any>(`${environment.productionApiUrl}/organization/${id}`, organization)
      .pipe(
        map(response => {
          return response;
        }),
        catchError(this.errorHandl)
      )  
  }
  
  deleteOrganization(id:string): Observable<boolean> {
    return this.http.delete<any>(`${environment.productionApiUrl}/organization/${id}`)
    .pipe(
      map(response => {
        return response;
      }),
      catchError(this.errorHandl)
    )  
  }

  getAllOrganizations() {
    return this.http.get(`${environment.productionApiUrl}/organizations`)
    .pipe(
      map(response => {
        return response;
      }),
      catchError(this.errorHandl)
    )  }
  
  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);
 }
  //getOrganizations(): Observable<any> {
    //return this.http.get(`${environment.apiUrl}/organizations`);
  //}


  

  //getOrganization(id: number): Observable<Organization> {
  //  return observableOf(this.listData.find(e => e.id === id));
  //}
}
/*
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Bug } from './bug';
import { Observable, throwError } from 'rxjs';
import { retry, catchError } from 'rxjs/operators';

@Injectable({
  providedIn: 'root'
})

export class BugService {

  // Base url
  baseurl = 'http://localhost:3000';

  constructor(private http: HttpClient) { }

  // Http Headers
  httpOptions = {
    headers: new HttpHeaders({
      'Content-Type': 'application/json'
    })
  }

  // POST
  CreateBug(data): Observable<Bug> {
    return this.http.post<Bug>(this.baseurl + '/bugtracking/', JSON.stringify(data), this.httpOptions)
    .pipe(
      retry(1),
      catchError(this.errorHandl)
    )
  }  

  // GET
  GetIssue(id): Observable<Bug> {
    return this.http.get<Bug>(this.baseurl + '/bugtracking/' + id)
    .pipe(
      retry(1),
      catchError(this.errorHandl)
    )
  }




import { Injectable } from '@angular/core';
import { User } from '@app/models';

@Injectable({ providedIn: 'root' })
export class UserService {
    constructor(private http: HttpClient) { }

    getAll() {
        return this.http.get<User[]>(`${environment.apiUrl}/users`);
    }

    getById(id: number) {
        return this.http.get<User>(`${environment.apiUrl}/users/${id}`);
    }
}


*/