import {Injectable} from "@angular/core";
import {BehaviorSubject} from "rxjs";
import {HttpClient} from "@angular/common/http";

enum ApplicationStatus {
  AGREED = 'Согласована',
  ACCEPTED = 'Исполнена'
}

@Injectable({providedIn: 'root'})
export class StatusUpdateService {

  constructor(private httpClient: HttpClient) {
  }

  public statusMessage = new BehaviorSubject<any>(null);

  public update(responseObj: any): void {
    this.httpClient.put<number>('status', responseObj)
        .toPromise()
        .then(appNumber => this.publishStatus(appNumber, responseObj.className === 'update'))
        .catch(err => console.log('failed to update application status', err));
  }

  public publishStatus(appNumber: number, accepted: boolean) {
    this.statusMessage.next(
        {
          appNumber: appNumber,
          status: accepted ? ApplicationStatus.ACCEPTED : ApplicationStatus.AGREED
        });
  }
}
