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

  constructor(private _http: HttpClient) { }

  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: string, 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(idUser: string, line: number){
    return this.getQuery(`get-unprogrammed-visits/${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))
  }

  getMaintenanceSimulation(idOrder: string, idUser: string, line: number){
    return this.getQuery(`get-maintenance-simulation/${idOrder}/${idUser}/${line}`).pipe(map((data: any) => data))
  }

  getOrderWithActivator(idOrder: string, idUser: string, line: number){
    return this.getQuery(`get-order-with-activator/${idOrder}/${idUser}/${line}`).pipe(map((data: any) => data))
  }

  printOrderSimulation(idOrder: string, idUser: string, line: number){
    return this.getQuery(`print-order-simulation/${idOrder}/${idUser}/${line}`).pipe(map((data: any) => data))
  }

  printOrderDetails(idOrder: string, idUser: string, line: number){
    return this.getQuery(`print-order-details/${idOrder}/${idUser}/${line}`).pipe(map((data: any) => data))
  }

  extractMaintenancePlan(idOrder: string, idUser: string, line: number){
    return this.getQuery(`extract-maintenance-plan/${idOrder}/${idUser}/${line}`).pipe(map((data: any) => data))
  }

  getMaintenancePlanAnalysis(idFile: string, idUser: string, line: number){
    return this.getQuery(`get-maintenance-plan-analysis/${idFile}/${idUser}/${line}`).pipe(map((data: any) => data))
  }

  getFileToMSProject(idOrder: string, idUser: string, line: number){
    return this.getQuery(`get-file-to-ms-project/${idOrder}/${idUser}/${line}`).pipe(map((data: any) => data))
  }

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

  getOrderExecutionDetails(idExecution: string, idUser: string, line: number){
    return this.getQuery(`get-order-execution-details/${idExecution}/${idUser}/${line}`).pipe(map((data: any) => data))
  }

  getWorkOrdersByEquipment(equipmentCode: string, idUser: string, line: number){
    return this.getQuery(`get-work-orders-by-equipment/${equipmentCode}/${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))
  }

  setBudgetAnalysis(body: any){
    return this.postQuery("set-budget-analysis", body).pipe(map((data: any) => data))
  }

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

  saveOrderSimulation(body: any){
    return this.postQuery("save-order-simulation", 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!
      })
    });
  }
}
