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

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

  constructor(private http: HttpClient) { }

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

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

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

  getResponsibleUsers(idUser: string, line: number){
    return this.getQuery(`corrective-maintenance/get-responsibe-users/${idUser}/${line}`).pipe(map((data: any) => data))
  }

  getSecurityManagements(idUser: string, line: number){
    return this.getQuery(`corrective-maintenance/get-security-managements/${idUser}/${line}`).pipe(map((data: any) => data))
  }

  getSecurityManagement(idManagement: string, idUser: string, line: number){
    return this.getQuery(`corrective-maintenance/get-security-management/${idManagement}/${idUser}/${line}`).pipe(map((data: any) => data))
  }

  getBlockRegisters(idUser: string, line: number){
    return this.getQuery(`corrective-maintenance/get-block-registers/${idUser}/${line}`).pipe(map((data: any) => data))
  }

  getWorkOrderStatusHistory(idOrder: string, idUser: string, line: number){
    return this.getQuery(`corrective-maintenance/get-work-order-status-history/${idOrder}/${idUser}/${line}`).pipe(map((data: any) => data))
  }

  getReports(type: string, startDate: string, endDate: string, idUser: string, line: number){
    return this.getQuery(`corrective-maintenance/get-reports/${type}/${startDate}/${endDate}/${idUser}/${line}`).pipe(map((data: any) => data))
  }

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

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

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

  approveWorkOrder(body: any){
    return this.postQuery("corrective-maintenance/approve-work-order", body).pipe(map((data: any) => data))
  }

  startWorkOrder(body: any){
    return this.postQuery("corrective-maintenance/start-work-order", body).pipe(map((data: any) => data))
  }

  updateWorkOrderStatus(body: any){
    return this.postQuery("corrective-maintenance/update-work-order-status", body).pipe(map((data: any) => data))
  }

  transferWorkOrder(body: any){
    return this.postQuery("corrective-maintenance/transfer-work-order", body).pipe(map((data: any) => data))
  }

  registerSecurityManagement(body: any){
    return this.postQuery("corrective-maintenance/register-security-management", body).pipe(map((data: any) => data))
  }

  updateSecurityManagement(body: any){
    return this.postQuery("corrective-maintenance/update-security-management", body).pipe(map((data: any) => data))
  }

  deleteSecurityManagement(body: any){
    return this.postQuery("corrective-maintenance/delete-security-management", body).pipe(map((data: any) => data))
  }

  registerBlock(body: any){
    return this.postQuery("corrective-maintenance/register-block", body).pipe(map((data: any) => data))
  }

  unblockRegister(body: any){
    return this.postQuery("corrective-maintenance/unblock-register", body).pipe(map((data: any) => data))
  }

  generateReport(body: any){
    return this.postQuery("corrective-maintenance/generate-report", 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!
      })
    });
  }
}
