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

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

  constructor(private http: HttpClient) {}

  familyConsult(idUser: string, line: number){
    return this.getQuery(`family/consult/${idUser}/${line}`).pipe(map((data: any) => data));
  }

  subfamilyConsult(familyCode: string, idUser: string, line: number){
    return this.getQuery(`subfamily/consult/${familyCode}/${idUser}/${line}`).pipe(map((data: any) => data));
  }

  locationConsult(idUser: string, line: number){
    return this.getQuery(`location/consult/${idUser}/${line}`).pipe(map((data: any) => data));
  }

  occupationAreasConsult(idUser: string, line: number){
    return this.getQuery(`occupation/areas/consult/${idUser}/${line}`).pipe(map((data: any) => data));
  }

  occupationConsult(idUser: string, line: number){
    return this.getQuery(`occupation/consult/${idUser}/${line}`).pipe(map((data: any) => data));
  }

  occupationTrainConsult(idUser: string, line: number){
    return this.getQuery(`occupation/train/consult/${idUser}/${line}`).pipe(map((data: any) => data));
  }

  occupationTrainAreaConsult(trainOccupation: string, idUser: string, line: number){
    return this.getQuery(`occupation/train/area/consult/${trainOccupation}/${idUser}/${line}`).pipe(map((data: any) => data));
  }

  occupationTrainElements(idUser: string, line: number){
    return this.getQuery(`occupation/train/elements/${idUser}/${line}`).pipe(map((data: any) => data));
  }

  equipmentConsultPending(idUser: string, line: number){
    return this.getQuery(`equipment/consult/pending/${idUser}/${line}`).pipe(map((data: any) => data));
  }

  equipmentConsultPendingOnly(idReg: string, idUser: string, line: number){
    return this.getQuery(`equipment/consult/pending/only/${idReg}/${idUser}/${line}`).pipe(map((data: any) => data));
  }

  equipmentConsult(idUser: string, line: number){
    return this.getQuery(`equipment/consult/${idUser}/${line}`).pipe(map((data: any) => data));
  }

  equipmentConsultBySubfamily(family: string, subfamily: string, idUser: string, line: number){
    return this.getQuery(`equipment/consult/by-subfamily/${family}/${subfamily}/${idUser}/${line}`).pipe(map((data: any) => data));
  }

  equipmentConsultByParent(parent: string, idUser: string, line: number){
    return this.getQuery(`equipment/consult/by-parent/${parent}/${idUser}/${line}`).pipe(map((data: any) => data));
  }

  equipmentConsultOnly(code: string, idUser: string, line: number){
    return this.getQuery(`equipment/consult/only/${code}/${idUser}/${line}`).pipe(map((data: any) => data));
  }

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

  familyRegister(body: any){
    return this.postQuery("family/register", body).pipe(map((data: any) => data));
  }

  familyUpdate(body: any){
    return this.postQuery("family/update", body).pipe(map((data: any) => data));
  }

  familyDelete(body: any){
    return this.postQuery("family/delete", body).pipe(map((data: any) => data));
  }

  subfamilyRegister(body: any){
    return this.postQuery("subfamily/register", body).pipe(map((data: any) => data));
  }

  subfamilyUpdate(body: any){
    return this.postQuery("subfamily/update", body).pipe(map((data: any) => data));
  }

  subfamilyDelete(body: any){
    return this.postQuery("subfamily/delete", body).pipe(map((data: any) => data));
  }

  occupationRegister(body: any){
    return this.postQuery("occupation/register", body).pipe(map((data: any) => data));
  }

  occupationUpdate(body: any){
    return this.postQuery("occupation/update", body).pipe(map((data: any) => data));
  }

  occupationDelete(body: any){
    return this.postQuery("occupation/delete", body).pipe(map((data: any) => data));
  }

  equipmentPreCodification(body: any){
    return this.postQuery("equipment/pre-codification", body).pipe(map((data: any) => data));
  }

  equipmentPreCodificationStatusUpdate(body: any){
    return this.postQuery("equipment/pre-codification/status/update", body).pipe(map((data: any) => data));
  }

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