import { Injectable } from '@angular/core';
import { MatSnackBar } from '@angular/material/snack-bar';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { map } from 'rxjs';
import { WorkflowInterface } from '../../interfaces/samwf/workflow-interface';
import { ResponseData } from '../../interfaces/response-data';
import { apiUriJean } from 'src/environments/environment';
import { TaskInterface } from '../../interfaces/samwf/task-interface';
import { TasksJSONInterface } from '../../interfaces/samwf/tasks-json-interface';

@Injectable({
  providedIn: 'root',
})
export class MgadService {
  jwt = localStorage.getItem('token');
  headers = new HttpHeaders({ Authorization: 'Bearer ' + this.jwt });

  public arrayDelService: Array<any> = new Array<any>();
  token = 'Bearer ' + this.jwt;

  constructor(private _http: HttpClient, private _snackBar: MatSnackBar) {}

  ngOnInit() {}

  getModules() {
    return this.getQuery('/modules').pipe(map((data: any) => data));
  }

  getFormsForWorkflow() {
    return this.getQuery('form/consulta-form-workflow').pipe(
      map((data: any) => data)
    );
  }

  getFormsByID(idForm: any) {
    return this.getQuery(`generic/generate-form/${idForm}`).pipe(
      map((data: any) => data)
    );
  }

  getRequestFieldByUser(id_user: any) {
    return this.getQuery(`requests_forms/user/${id_user}`).pipe(
      map((data: any) => data)
    );
  }

  updateRequests(idw: any, request: any) {
    return this.putQuery(`/requests_workflow/validate/${idw}`, request).pipe(
      map((data: any) => data)
    );
  }

  registerTable(table: any) {
    return this.postQuery('/tabla-db/registra', table).pipe(
      map((data: any) => data)
    );
  }

  getQuery(query: string) {
    const URL = `${apiUriJean}${query}`;
    return this._http.get(URL, {
      headers: new HttpHeaders({
        Authorization: this.token,
      }),
    });
  }

  postQuery(query: string, body: any) {
    const URL = `${apiUriJean}${query}`;
    return this._http.post(URL, body, {
      headers: new HttpHeaders({
        Authorization: this.token,
      }),
    });
  }

  putQuery(query: string, body: any) {
    const URL = `${apiUriJean}${query}`;
    return this._http.put(URL, body, {
      headers: new HttpHeaders({
        Authorization: this.token,
      }),
    });
  }

  putQueryWithoutBody(query: string) {
    const URL = `${apiUriJean}${query}`;
    return this._http.put(URL, null, {
      headers: new HttpHeaders({
        Authorization: this.token,
      }),
    });
  }
}
