import { Injectable } from '@angular/core';
import { HttpRequestService } from '../http-request.service';
import { map, Observable } from 'rxjs';
import {
  ChangeWorkflowStatusRequest,
  RegisterWorkflowRequest,
  RequestFormRequestWorkflow,
  RequestFormTask,
  RequestFormWorkflow,
  RequestNotificate,
  RequestSearch,
  ResponseDataActiveWorkflows,
  ResponseDataDetailsNotificate,
  ResponseDataDetailsRequest,
  ResponseDataFilterNotificate,
  ResponseDataFilterRequest,
  ResponseDataGetExecuteTask,
  ResponseDataGetExecuteWorkflow,
  ResponseDataGetHistoryNotificateTask,
  ResponseDataGetTaskByWorkflow,
  ResponseDataGetTasks,
  ResponseDataGetWorkflows,
  ResponseDataProcessWorkflow,
  ResponseDataTask,
  ResponseDataWorkflow,
} from '../../interfaces/process-managementv/workflow-management.interface';
import { ResponseData } from '../resources.service';
import { RequestUserNumberLine } from '../../interfaces/response-data';

@Injectable({
  providedIn: 'root',
})
export class ProcessManagementService {
  private _url: string;

  constructor(private _httpRequestService: HttpRequestService) {
    this._url = 'process-management';
  }

  public getWorkflows(
    user: string,
    line: number
  ): Observable<ResponseDataGetWorkflows> {
    return this._httpRequestService
      .getQuery(`${this._url}/get-workflows/${user}/${line}`)
      .pipe(map((data: any) => data));
  }
  public getWorkflowById(
    idWorkflow: string,
    user: string,
    line: number
  ): Observable<ResponseDataWorkflow> {
    return this._httpRequestService
      .getQuery(`${this._url}/get-workflow-by-id/${idWorkflow}/${user}/${line}`)
      .pipe(map((data: any) => data));
  }
  public getWorkflowsActives(
    user: string,
    line: number
  ): Observable<ResponseDataGetWorkflows> {
    return this._httpRequestService
      .getQuery(`${this._url}/get-workflows-actives/${user}/${line}`)
      .pipe(map((data: any) => data));
  }
  public registerWorkflow(
    requestFormWorkflow: RequestFormWorkflow
  ): Observable<ResponseData> {
    return this._httpRequestService
      .postQuery(`${this._url}/register-workflows`, requestFormWorkflow)
      .pipe(map((data: any) => data));
  }

  public registerNewWorkflow(
    registerRequest: RegisterWorkflowRequest
  ): Observable<ResponseData> {
    return this._httpRequestService
      .postQuery(`${this._url}/register-workflow`, registerRequest)
      .pipe(map((data: any) => data));
  }
  public updateWorkflow(
    idWorkflow: string,
    requestFormWorkflow: RequestFormWorkflow
  ): Observable<ResponseData> {
    return this._httpRequestService
      .postQuery(
        `${this._url}/update-workflows/${idWorkflow}`,
        requestFormWorkflow
      )
      .pipe(map((data: any) => data));
  }
  public deleteWorkflow(
    idWorkflow: string,
    requestDeleteWorkflow: RequestUserNumberLine
  ): Observable<ResponseData> {
    return this._httpRequestService
      .postQuery(
        `${this._url}/delete-workflows/${idWorkflow}`,
        requestDeleteWorkflow
      )
      .pipe(map((data: any) => data));
  }
  public restoreWorkflows(
    requestRestoreWorkflows: RequestUserNumberLine
  ): Observable<ResponseData> {
    return this._httpRequestService
      .postQuery(`${this._url}/restore-workflows`, requestRestoreWorkflows)
      .pipe(map((data: any) => data));
  }

  public getProcessWorkflow(
    user: string,
    line: number
  ): Observable<ResponseDataProcessWorkflow> {
    return this._httpRequestService
      .getQuery(`${this._url}/get-process-workflow/${user}/${line}`)
      .pipe(map((data: any) => data));
  }

  public getTasks(
    user: string,
    line: number
  ): Observable<ResponseDataGetTasks> {
    return this._httpRequestService
      .getQuery(`${this._url}/get-tasks/${user}/${line}`)
      .pipe(map((data: any) => data));
  }

  public getTaskByWorkflow(
    idWorkflow: string,
    user: string,
    line: number
  ): Observable<ResponseDataGetTaskByWorkflow> {
    return this._httpRequestService
      .getQuery(
        `${this._url}/get-task-by-workflow/${idWorkflow}/${user}/${line}`
      )
      .pipe(map((data: any) => data));
  }

  public getTaskById(
    idTask: string,
    user: string,
    line: number
  ): Observable<ResponseDataTask> {
    return this._httpRequestService
      .getQuery(`${this._url}/get-task-by-id/${idTask}/${user}/${line}`)
      .pipe(map((data: any) => data));
  }
  public registerTask(
    requestFormTask: RequestFormTask
  ): Observable<ResponseData> {
    return this._httpRequestService
      .postQuery(`${this._url}/register-task`, requestFormTask)
      .pipe(map((data: any) => data));
  }
  public updateTask(
    idTask: string,
    requestFormTask: RequestFormTask
  ): Observable<ResponseData> {
    return this._httpRequestService
      .postQuery(`${this._url}/update-task/${idTask}`, requestFormTask)
      .pipe(map((data: any) => data));
  }
  public deleteTask(
    idTask: string,
    requestFormTask: RequestUserNumberLine
  ): Observable<ResponseData> {
    return this._httpRequestService
      .postQuery(`${this._url}/delete-task/${idTask}`, requestFormTask)
      .pipe(map((data: any) => data));
  }

  public getExecuteWorkflow(
    user: string,
    line: number
  ): Observable<ResponseDataGetExecuteWorkflow> {
    return this._httpRequestService
      .getQuery(`${this._url}/get-execute-workflow/${user}/${line}`)
      .pipe(map((data: any) => data));
  }
  public getExecuteTask(
    idExecute: string,
    user: string,
    line: number
  ): Observable<ResponseDataGetExecuteTask> {
    return this._httpRequestService
      .getQuery(`${this._url}/get-execute-task/${idExecute}/${user}/${line}`)
      .pipe(map((data: any) => data));
  }
  public getHistoryNotificateTask(
    idExecuteTask: string,
    user: string,
    line: number
  ): Observable<ResponseDataGetHistoryNotificateTask> {
    return this._httpRequestService
      .getQuery(
        `${this._url}/get-history-notificate-task/${idExecuteTask}/${user}/${line}`
      )
      .pipe(map((data: any) => data));
  }
  public updateRequestWorkflow(
    idExecuteTask: string,
    requestFormTask: RequestFormRequestWorkflow
  ): Observable<ResponseData> {
    return this._httpRequestService
      .postQuery(
        `${this._url}/update-request-workflow/${idExecuteTask}`,
        requestFormTask
      )
      .pipe(map((data: any) => data));
  }

  public searchRequestWorkflow(
    requestSearch: RequestSearch
  ): Observable<ResponseDataFilterRequest> {
    return this._httpRequestService
      .postQuery(`${this._url}/search-request-workflow`, requestSearch)
      .pipe(map((data: any) => data));
  }
  public getDetailsExecuteRequest(
    idExecute: string,
    user: string,
    line: number
  ): Observable<ResponseDataDetailsRequest> {
    return this._httpRequestService
      .getQuery(
        `${this._url}/get-details-execute-request/${idExecute}/${user}/${line}`
      )
      .pipe(map((data: any) => data));
  }

  public searchNotificate(
    requestNotificate: RequestNotificate
  ): Observable<ResponseDataFilterNotificate> {
    return this._httpRequestService
      .postQuery(`${this._url}/search-notificate`, requestNotificate)
      .pipe(map((data: any) => data));
  }
  public getDetailsNotificate(
    idNotificate: string,
    user: string,
    line: number
  ): Observable<ResponseDataDetailsNotificate> {
    return this._httpRequestService
      .getQuery(
        `${this._url}/get-details-notificate/${idNotificate}/${user}/${line}`
      )
      .pipe(map((data: any) => data));
  }

  public changeWorkflowStatus(
    changeStatusRequest: ChangeWorkflowStatusRequest
  ): Observable<ResponseData> {
    return this._httpRequestService
      .postQuery(`${this._url}/change-workflow-status`, changeStatusRequest)
      .pipe(map((data: any) => data));
  }

  public getActiveWorkflows(
    user: string,
    line: number
  ): Observable<ResponseDataActiveWorkflows> {
    return this._httpRequestService
      .getQuery(`${this._url}/get-active-workflows/${user}/${line}`)
      .pipe(map((data: any) => data));
  }
}
