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 { apiUriQA } 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 {
  private _url = 'http://192.168.100.148:8003/api';

  jwt = localStorage.getItem('jwt');
  headers = new HttpHeaders({ Authorization: 'Bearer ' + this.jwt });
  token =
    'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJFZERTQSJ9.eyJpc3MiOiJqb3NlLmJAaXR0ZWMubXgiLCJhdWQiOiJkb21pbmlvLnN5cC5teCIsImlhdCI6MTY0NzYxMjc0NCwiY2FkIjoxNjQ3Njk5MTQ0fQ.BkpH8BMrx0hExtdU0EsNGxQxcbx_RqvTpGq12DqfbS0IEej5enf6V6q2DbqPsueK_0DMR_CWU9kMMYQCN2jrCg';
  public arrayDelService: Array<any>;

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

  getVersionsWorkflow(idw: number) {
    return this._http.get<any>(
      `${this._url}/workflowsHistory/workflow/${idw}`,
      {
        headers: this.headers,
      }
    );
  }

  getAllRequests() {
    return this._http.get<any>(`${this._url}/requests`, {
      headers: this.headers,
    });
  }

  getRequestsAdvanceByUser(idUser: number) {
    return this._http.get<any>(`${this._url}/requests/user/${idUser}`, {
      headers: this.headers,
    });
  }

  getRequestsAdvance(idRequest: number) {
    return this._http.get<any>(`${this._url}/requests/${idRequest}`, {
      headers: this.headers,
    });
  }

  getNotificationsByUser(idUser: number) {
    return this._http.get<any>(`${this._url}/notifications/user/${idUser}`, {
      headers: this.headers,
    });
  }

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

  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 = `${apiUriQA}${query}`;
    return this._http.get(URL, {
      headers: new HttpHeaders({
        Authorization: this.token,
      }),
    });
  }

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

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

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