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

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

  constructor(private http: HttpClient) { }

  getWorkOrdersByMonth(month: number, year: number, idUser: string, line: number){
    return this.getQuery(`get-work-orders-by-month/${month}/${year}/${idUser}/${line}`).pipe(map((data: any) => data))
  }

  getWorkOrdersByDay(day: number, month: number, year: number, idUser: string, line: number){
    return this.getQuery(`get-work-orders-by-day/${day}/${month}/${year}/${idUser}/${line}`).pipe(map((data: any) => data))
  }

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

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

  getExecRegister(date: string, idOrder: number, idUser: string, line: number){
    return this.getQuery(`get-exec-register/${date}/${idOrder}/${idUser}/${line}`).pipe(map((data: any) => data))
  }

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

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

  getPreventiveCalendar(fecIni: string, fecFin: string, idUser: string, line: number){
    return this.getQuery(`get-preventive-calendar/${fecIni}/${fecFin}/${idUser}/${line}`).pipe(map((data: any) => data))
  }

  getUnprogrammedVisits(status: string, idUser: string, line: number){
    return this.getQuery(`get-unprogrammed-visits/${status}/${idUser}/${line}`).pipe(map((data: any) => data))
  }

  getUnprogrammedVisit(idVisit: string, idUser: string, line: number){
    return this.getQuery(`get-unprogrammed-visit/${idVisit}/${idUser}/${line}`).pipe(map((data: any) => data))
  }

  registerWorkOrder(body: any){
    return this.postQuery("register-work-order", body).pipe(map((data: any) => data))
  }

  executePreventiveWorkOrder(body: any){
    return this.postQuery("execute-preventive-work-order", body).pipe(map((data: any) => data))
  }

  cancelWorkOrder(body: any){
    return this.postQuery("cancel-work-order", body).pipe(map((data: any) => data))
  }

  deleteWorkOrder(body: any){
    return this.postQuery("delete-work-order", body).pipe(map((data: any) => data))
  }

  updateWorkOrder(body: any){
    return this.postQuery("update-work-order", body).pipe(map((data: any) => data))
  }

  endOrderExecution(body: any){
    return this.postQuery("end-order-execution", body).pipe(map((data: any) => data))
  }

  copyWorkOrder(body: any){
    return this.postQuery("copy-work-order", body).pipe(map((data: any) => data))
  }

  savePresetWorkOrder(body: any){
    return this.postQuery("save-preset-work-order", body).pipe(map((data: any) => data))
  }

  newUnprogrammedOrder(body: any){
    return this.postQuery("new-unprogrammed-order", body).pipe(map((data: any) => data))
  }

  updateVisitStatus(body: any){
    return this.postQuery("update-visit-status", body).pipe(map((data: any) => data))
  }

  updateOrderStatus(body: any){
    return this.postQuery("update-order-status", body).pipe(map((data: any) => data))
  }
  
  getQuery(query: string, tk?: string){
    const URL = `${apiTemp}${query}`;
    let token = "";
    if(tk == undefined || tk == null || tk == ""){
      token = `Bearer ${localStorage.getItem('token')!}`;
    }else{
      token = `Bearer ${tk!}`;
    }
    return this.http.get(URL, {
      headers: new HttpHeaders({
        Authorization: token,
      })
    })
  }

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