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

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

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

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

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

  getNoMeasuresFormFields(idUser: string, line: number){
    return this.getQuery(`corrective-maintenance/get-no-measures-form-fields/${idUser}/${line}`).pipe(map((data: any) => data))
  }

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

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

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

  getWorkOrderAttendance(idOrder: string, idUser: string, line: number){
    return this.getQuery(`corrective-maintenance/get-work-order-attendance/${idOrder}/${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))
  }

  updateApprovedWorkOrder(body: any){
    return this.postQuery("corrective-maintenance/update-approved-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))
  }

  closeWorkOrder(body: any){
    return this.postQuery("corrective-maintenance/close-work-order", 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))
  }

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

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

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

  createWorkGroup(body: any){
    return this.postQuery("corrective-maintenance/create-work-group", body).pipe(map((data: any) => data))
  }

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

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

  saveNoMeasuresFormFields(body: any){
    return this.postQuery("corrective-maintenance/save-no-measures-form-field", body).pipe(map((data: any) => data))
  }

  saveNoMeasuresOrder(body: any){
    return this.postQuery("corrective-maintenance/save-no-measures-order", body).pipe(map((data: any) => data))
  }

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

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

  noMeasuresOrderValidation(body: any){
    return this.postQuery("corrective-maintenance/no-measures-order-validation", body).pipe(map((data: any) => data))
  }

  closeNoMeasuresOrder(body: any){
    return this.postQuery("corrective-maintenance/close-no-measures-order", body).pipe(map((data: any) => data))
  }

  closedNoMeasuresOrderValidation(body: any){
    return this.postQuery("corrective-maintenance/closed-no-measures-order-validation", body).pipe(map((data: any) => data))
  }

  generateNoMeasuresReport(body: any){
    return this.postQuery("corrective-maintenance/generate-no-measures-report", body).pipe(map((data: any) => data))
  }

  setWorkOrderAttendance(body: any){
    return this.postQuery("corrective-maintenance/set-work-order-attendance", 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!
      })
    });
  }
}
