import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpErrorResponse } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map, catchError } from 'rxjs/operators';
import { apiConfig } from '../../environments/server.config';

const httpOptions = {
  headers: new HttpHeaders({
    'Content-Type': 'application/json',
    'Auth': 'my_key'
  })
};

const httplive = {
  headers: new HttpHeaders({
    'Access-Control-Allow-Origin': '*'
  })
};

const multipartHeader = {
  headers: new HttpHeaders({
    'enctype': 'multipart/form-data',
    'Accept': 'application/json',
    'Auth': 'my_key'
  })
};

@Injectable({
  providedIn: 'root'
})

export class WebService {
  constructor(private http: HttpClient) {}

  post_data(url: string, data) {
    return this.http.post(`${apiConfig + url}`, data, httpOptions).pipe(map((response: any) => response));
  }
  multipart_post(url: string, data) {
    return this.http.post(`${apiConfig + url}`, data, multipartHeader).pipe(map((response: any) => response));
  }
  get_data(url) {
    return this.http.get(`${apiConfig + url}`, httpOptions).pipe(map((response: any) => response));
  }
  setLocalStorageItem(id: string, data: string) {
    localStorage.setItem(id, data);
  }
  getLocalStorageItem(data: string): string {
    return localStorage.getItem(data);
  }
  removeLocalStorageItem(data) {
    localStorage.removeItem(data);
    localStorage.clear();
  }
  removeLocalItem(data) {
    localStorage.removeItem(data);
  }
  get_stored_json(json_file: string) {
    return this.http.get("assets/json_data/" + json_file);
  }
}