/*
 * Copyright (C) 2020. Robert Bosch GmbH Copyright (C) 2020. Robert Bosch Manufacturing Solutions GmbH, Germany. All rights reserved.
 */

import { Injectable } from '@angular/core';
import { AlertService, AlertType, PrimaryButton, AlertDialogComponent } from '@bci-web-core/core';
import { MatDialogRef } from '@angular/material/dialog';
import { Observable, Subject } from 'rxjs';
import { PinboardI18nService } from './pinboard-i18n.service';
@Injectable({
  providedIn: 'root',
})
export class LeavingApiService {
  private dialogRef: MatDialogRef<AlertDialogComponent>;
  pageRoute$ = new Subject<boolean>();

  constructor(private alertService: AlertService, public pinboardI18n: PinboardI18nService) {}

  showLeavingApiConfirmation(title: any, isFormValid: boolean): Observable<any> {
    this.dialogRef = this.alertService.openAlertBox({
      title: this.pinboardI18n.leavingApiTitle + title + '?',
      description: this.pinboardI18n.leavingApiDescription,
      type: AlertType.None,
      outerLeftButtonDisabled: false,
      outerLeftButtonTitle: this.pinboardI18n.doNotSave,
      leftButtonTitle: this.pinboardI18n.save,
      leftButtonDisabled: !isFormValid,
      rightButtonTitle: this.pinboardI18n.cancel,
      primaryButton: PrimaryButton.LeftButton,
    });

    this.dialogRef.afterClosed().subscribe((res) => {
      if (res === PrimaryButton.OuterLeftButton) {
        this.pageRoute$.next(false);
      } else if (res === PrimaryButton.LeftButton) {
        this.pageRoute$.next(true);
      } else if (res === PrimaryButton.RightButton) {
        this.pageRoute$.next(undefined);
      }
    });
    return this.pageRoute$.asObservable();
  }
}
