import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { ResourcesService } from './resources.service';
import { apiTemp, apiUriJean } from 'src/environments/environment.prod';


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

  private _urlQA: string;
  private _url:string;
  private _idUser:string;
  private _token:string;
  private _line:string;

  constructor(private _http: HttpClient, private resourcesService: ResourcesService) {
    this._url = `${apiUriJean}`;
    this._urlQA = `${apiTemp}`;
    this._token = `Bearer ${localStorage.getItem('token')}`;
    this._idUser = this.resourcesService.getUser();
    this._line = this.resourcesService.getLineNumber();
  }

  public getQuery(query: string) {
    const URL = `${this._url}/${query}/${this._idUser}/${this._line}`;      // API JEAN
    // const URL = `${this._urlQA}${query}/${this._idUser}/${this._line}`;        // API QA
    return this._http.get(URL, {
      headers: new HttpHeaders({ Authorization: this._token }),
    });
  }

  public postQuery(query: string, body: any) {
    const URL = `${this._url}/${query}`;                                    // API JEAN
    // const URL = `${this._urlQA}${query}`;                                      // API QA
    return this._http.post(URL, body, {
      headers: new HttpHeaders({ Authorization: this._token }),
    });
  }

  public putQuery(query: string, body: any) {
    const URL = `${this._url}/${query}/`;                                   // API JEAN
    // const URL = `${this._urlQA}${query}/`;                                     // API QA
    return this._http.put(URL, body, {
      headers: new HttpHeaders({ Authorization: this._token }),
    });
  }

  

  public getQueryQA(query: string) {
    // const URL = `${this._url}/${query}/${this._line}`;
    const URL = `${this._urlQA}${query}/${this._line}`;
    return this._http.get(URL, {
      headers: new HttpHeaders({ Authorization: this._token }),
    });
  }

  public postQueryQA(query: string, body: any) {
    const URL = `${this._urlQA}${query}`;
    return this._http.post(URL, body, {
      headers: new HttpHeaders({ Authorization: this._token }),
    });
  }

  public putQueryQA(query: string, body: any) {
    const URL = `${this._urlQA}${query}/`;
    return this._http.put(URL, body, {
      headers: new HttpHeaders({ Authorization: this._token }),
    });
  }

}
