import { inject, Injectable } from '@angular/core';
import { Observable, BehaviorSubject, map } from 'rxjs';
import { IActivityProgress, IActivityInfo, IActivityInvoke, IActivityInvokeResponse } from '../resilient-activity.model';
import { createRequestOption } from 'app/core/request/request-util';
import { ApplicationConfigService } from 'app/core/config/application-config.service';
import { HttpClient, HttpResponse } from '@angular/common/http';

export type EntityResponseType = HttpResponse<IActivityProgress>;

export type EntityArrayResponseType = HttpResponse<IActivityInfo[]>;

export type ActivityInvokeResponseType = HttpResponse<IActivityInvokeResponse>;

@Injectable({
  providedIn: 'root'
})
export class ResilientActivityService {
  protected http = inject(HttpClient);
  protected applicationConfigService = inject(ApplicationConfigService);
  protected resourceUrl = this.applicationConfigService.getEndpointFor('api/activity-engines');

  progress(key: string): Observable<EntityResponseType> {
    return this.http
      .get<IActivityProgress>(`${this.resourceUrl}/progress/${key}`, { observe: 'response' });
  }
  
  activityInfo(activityDomainClass: string, activityDomainState?: string): Observable<EntityArrayResponseType> {
    return this.http
      .get<IActivityInfo[]>(`${this.resourceUrl}/activity/${activityDomainClass}/${activityDomainState}`, { observe: 'response' });
  }
  
  doActivity(activityInvoke: IActivityInvoke): Observable<IActivityInvokeResponse | null> {
    return this.http
      .put<IActivityInvokeResponse>(`${this.resourceUrl}/activity`, activityInvoke, { observe: 'response' })
      .pipe(map((res: HttpResponse<IActivityInvokeResponse>) => res.body));
  }
}
