import {ChangeDetectionStrategy, ChangeDetectorRef, Component, Input} from "@angular/core";
import {Process, TaskService, ProcessDefinitionResource, ProcessService} from "@webbpm/base-package";
import {SharedModule} from "shared";

@Component({
    selector: 'process',
    templateUrl: './process_list.html',
    changeDetection: ChangeDetectionStrategy.OnPush,
    imports: [SharedModule]
})
export class ProcessListComponent {

  @Input()
  public processList: Process[];

  constructor(private processDefinitionResource: ProcessDefinitionResource,
              private taskService: TaskService,
              private cd: ChangeDetectorRef,
              private processService: ProcessService) {
    this.processList = [];
    this.loadProcessList();
  }

  loadProcessList() {
    this.processDefinitionResource.list().then(
        (processList) => {
          this.processList = processList;
          this.cd.markForCheck();
        });
  }

  startProcess(processDefinitionId: string) {
    this.processService.start(processDefinitionId, null).then(
        (processInstanceId: number) => {
          this.taskService.startAndOpenNextTask(processInstanceId);
        }
    );
  }
}
