import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { map } from 'rxjs';
import { apiTemp } from '../../environments/environment.prod';

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

  constructor(private _http: HttpClient,) {}

  getDocumentsByUser(idUser: string, line: number){
    return this.getQuery(`get-documents/${idUser}/${line}`).pipe(map((data: any) => data));
  }

  getFiles(mod: string, cla: string, sda: string, eda: string, dna: string, ext: string, mode: string, idUser: string, line: number){
    return this.getQuery(`get-files/${mod}/${cla}/${sda}/${eda}/${dna}/${ext}/${mode}/${idUser}/${line}`).pipe(map((data: any) => data));
  }

  getDownloadToken(id: string, idUser: string, line: number){
    return this.getQuery(`get-download-token/${id}/${idUser}/${line}`).pipe(map((data: any) => data));
  }

  getFileInfo(id: string, idUser: string, line: number) {
    return this.getQuery(`get-file-info/${id}/${idUser}/${line}`).pipe(map((data: any) => data));
  }

  getPublicDocumentUrl(id: string, idUser: string, line: number){
    return this.getQuery(`get-public-document-url/${id}/${idUser}/${line}`).pipe(map((data: any) => data));
  }

  getFileAccess(id: string, idUser: string, line: number){
    return this.getQuery(`get-file-access/${id}/${idUser}/${line}`).pipe(map((data: any) => data));
  }

  getAssociatedWorkOrders(id: string, idUser: string, line: number){
    return this.getQuery(`get-associated-work-orders/${id}/${idUser}/${line}`).pipe(map((data: any) => data));
  }

  getPublicTempDocumentUrl(id: string, idUser: string, line: number){
    return this.getQuery(`get-public-temp-document-url/${id}/${idUser}/${line}`).pipe(map((data: any) => data));
  }

  uploadTempFile(body: any) {
    return this.postQuery('upload-temp-file', body).pipe(map((data: any) => data));
  }

  deleteTempFile(body: any) {
    return this.postQuery('delete-temp-file', body).pipe(map((data: any) => data));
  }

  saveFinalFile(body: any) {
    return this.postQuery('save-final-file', body).pipe(map((data: any) => data));
  }

  updateFilePermissions(body: any) {
    return this.postQuery('update-file-permissions', body).pipe(map((data: any) => data));
  }

  deleteFile(body: any) {
    return this.postQuery('delete-file', body).pipe(map((data: any) => data));
  }

  changeAssociationStatus(body: any) {
    return this.postQuery('change-association-status', body).pipe(map((data: any) => data));
  }


  postQuery(query: string, body: any){
    const JWT = `Bearer ${localStorage.getItem('token')}`;
    const URL = `${apiTemp}${query}`;
    return this._http.post(URL, body, {
      headers: new HttpHeaders({
        Authorization: JWT!
      })
    });
  }

  getQuery(query: string){
    const JWT = `Bearer ${localStorage.getItem('token')}`;
    const URL = `${apiTemp}${query}`; 
    return this._http.get(URL, {
      headers: new HttpHeaders({
        Authorization: JWT,
      })
    });
  }
}
