import {AnalyticalScope, Behavior, GridV2, NotNull} from "@webbpm/base-package";
import {StatusUpdateService} from "../../../modules/app/service/status-update.service";
import {Subscription} from "rxjs";
import {OnDestroy} from "@angular/core";

@AnalyticalScope(GridV2)
export class RowUpdateService extends Behavior implements OnDestroy {

  @NotNull()
  public statusColId: string;

  private updateService: StatusUpdateService;
  private subscription: Subscription;

  initialize() {
    super.initialize();
    let grid = this.getScript(GridV2);
    this.updateService = this.injector.get(StatusUpdateService);
    this.subscription = this.updateService.statusMessage.subscribe(value => {
      if (!value) {
        return;
      }
      grid.gridApi.forEachNode(rowNode => {
        if (rowNode.id == value.appNumber) {
          let rowData = rowNode.data;
          rowData[this.statusColId] = value.status;
          rowNode.updateData(rowData);
        }
      });
    });
  }

  ngOnDestroy(): void {
    this.subscription.unsubscribe();
  }
}
