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 { apiUriWorkflow } from 'src/environments/environment';
import { TaskInterface } from '../../interfaces/samwf/task-interface';
import { TasksJSONInterface } from '../../interfaces/samwf/tasks-json-interface';

@Injectable({
  providedIn: 'root',
})
export class WorkflowService {
  /*  jwt = localStorage.getItem('token'); */
  jwt =
    'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJFZERTQSJ9.eyJpc3MiOiJhbGFuLmdAaXR0ZWMubXgiLCJhdWQiOiJkb21pbmlvLnN5cC5teCIsImlhdCI6MTY1MzA1MzEzOSwiY2FkIjoxNjUzMTM5NTM5fQ.IoygQG6LyvdK_-yyVbXVxZIaVF0BH95wd_3xIywkqk27l8ToLLidEM2D-_paXa5F_0zTMyWsbz3DJ8_plbE0AA';

  headers = new HttpHeaders({ Authorization: 'Bearer ' + this.jwt });

  public arrayDelService: Array<any> = new Array<any>();
  token =
    'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJFZERTQSJ9.eyJpc3MiOiJhbGFuLmdAaXR0ZWMubXgiLCJhdWQiOiJkb21pbmlvLnN5cC5teCIsImlhdCI6MTY1MzA1MzEzOSwiY2FkIjoxNjUzMTM5NTM5fQ.IoygQG6LyvdK_-yyVbXVxZIaVF0BH95wd_3xIywkqk27l8ToLLidEM2D-_paXa5F_0zTMyWsbz3DJ8_plbE0AA';

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

  ngOnInit() {}

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

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

  getWorkflowTasks(idW: any) {
    return this.getQuery(`/workflows/${idW}/tasks`).pipe(
      map((data: any) => data)
    );
  }

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

  getWorkflow(idW: any) {
    return this.getQuery(`/workflows/${idW}`).pipe(map((data: any) => data));
  }

  getRequestUser(idUser: any) {
    return this.getQuery(`/requests_workflow/user/${idUser}`).pipe(
      map((data: any) => data)
    );
  }

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

  getVersionsWorkflow(idw: number) {
    return this.getQuery(`/workflowsHistory/workflow/${idw}`).pipe(
      map((data: any) => data)
    );
  }

  getAllRequests() {
    return this.getQuery(`/requests`).pipe(map((data: any) => data));
  }

  getRequestsAdvanceByUser(idUser: any) {
    return this.getQuery(`/requests/user/${idUser}`).pipe(
      map((data: any) => data)
    );
  }

  getRequestsAdvance(idRequest: number) {
    return this.getQuery(`/requests/${idRequest}`).pipe(
      map((data: any) => data)
    );
  }

  getRequestsAdvanceFieldsByOrder(idOrder: any) {
    return this.getQuery(`/orders`).pipe(map((data: any) => data));
  }

  getNotificationsByUser(idUser: number) {
    return this.getQuery(`/notifications/user/${idUser}`).pipe(
      map((data: any) => data)
    );
  }

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

  registerTask(task: TasksJSONInterface) {
    return this.postQuery('/tasks', task).pipe(map((data: any) => data));
  }

  updateTask(idTask: any, task: TasksJSONInterface) {
    return this.putQuery(`/tasks/${idTask}`, task).pipe(
      map((data: any) => data)
    );
  }

  completeField(idField: any, field: any) {
    return this.putQuery(`/requests_forms/complete/${idField}`, field).pipe(
      map((data: any) => data)
    );
  }

  registerWorkflow(workflow: WorkflowInterface) {
    return this.postQuery('/workflows', workflow).pipe(
      map((data: any) => data)
    );
  }

  disableTaskWorklfow(task: any) {
    return this.putQuery(`/workflows/activate/task`, task).pipe(
      map((data: any) => data)
    );
  }

  disableTaskModule(idTask: any) {
    return this.putQueryWithoutBody(`/tasks/activate/${idTask}`).pipe(
      map((data: any) => data)
    );
  }

  updateWorkflow(workflow: WorkflowInterface, idW: number) {
    return this.putQuery(`/workflows/${idW}`, workflow).pipe(
      map((data: any) => data)
    );
  }

  updateStateWorkflow(idW: number) {
    return this.putQuery(`/workflows/${idW}/activate`, null).pipe(
      map((data: any) => data)
    );
  }

  updateTypeWorkflow(idW: number) {
    return this.putQuery(`/workflows/${idW}/automate`, null).pipe(
      map((data: any) => data)
    );
  }

  setArray(array: any) {
    this.arrayDelService = array;
  }

  getArray() {
    return this.arrayDelService;
  }

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

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

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

  putQueryWithoutBody(query: string) {
    const URL = `${apiUriWorkflow}${query}`;
    return this._http.put(URL, null, { headers: this.headers });
  }
}
