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 EquipmentManagementService {

  constructor(private _http: HttpClient) {}

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

  familyConsultOnly(familyCode: string, idUser: string, line: number){
    return this.getQuery(`family/consult/only/${familyCode}/${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));
  }

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

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

  equipmentConsultPending(offset: number, idUser: string, line: number){
    return this.getQuery(`equipment/consult/pending/${offset}/${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));
  }

  equipmentConsultFiltered(filterValue: string, idUser: string, line: number){
    return this.getQuery(`equipment/consult/filter/${filterValue}/${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));
  }

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

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

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

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

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

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

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

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

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

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

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

  getQuery(query: string){
    const JWT = `Bearer ${localStorage.getItem('token')}`;
    const URL = `${apiTemp}${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));
  }

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

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

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

  equipmentGraphicArborescence(body: any){
    return this.postQuery("equipment/graphic-arborescence", body).pipe(map((data: any) => data));
  }

  equipmentUpdateSecurityCriticality(body: any){
    return this.postQuery("equipment/update-security-criticality", body).pipe(map((data: any) => data));
  }

  equipmentGenerateEquipmentFile(body: any){
    return this.postQuery("equipment/generate-equipment-file", 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!
      })
    });
  }
}
