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

  constructor(private _http: HttpClient) { }

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

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

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

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

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

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

  counterConsultLastMeasures(idCounter: string, idUser: string, line: number){
    return this.getQuery(`counter/consult/last-measure/${idCounter}/${idUser}/${line}`).pipe(map((data: any) => data));
  }

  counterConsultMeasures(idCounter: string, $order: string, $direction: string, $limit: number, $offset: number, idUser: string, line: number){
    return this.getQuery(`counter/consult/measures/${idCounter}/${$order}/${$direction}/${$limit}/${$offset}/${idUser}/${line}`).pipe(map((data: any) => data));
  }

  counterConsultMeasuresLength(idCounter: string, idUser: string, line: number){
    return this.getQuery(`counter/consult/measures-length/${idCounter}/${idUser}/${line}`).pipe(map((data: any) => data));
  }

  registerActivator(body: any){
    return this.postQuery("register-activator", body).pipe(map((data: any) => data));
  }

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

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

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

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

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

  relateCounters(body: any){
    return this.postQuery("counter/relate-counters", body).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
      })
    });
  }

  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!,
      }),
    });
  }
}
