/*
 * Copyright (C) 2021 Robert Bosch GmbH Copyright (C) 2021 Robert Bosch Manufacturing Solutions GmbH, Germany. All rights reserved.
 */
import { Injectable } from '@angular/core';
import { BannerButton, BannerMessage, NotificationTypes } from './notification.models';
import { Subject } from 'rxjs';

@Injectable({
  providedIn: 'root',
})
export class NotificationBannerService {
  private _showBannerMessage = new Subject<BannerMessage>();
  readonly showBannerMessage = this._showBannerMessage.asObservable();
  private _hideBannerMessage = new Subject<string>();
  readonly hideBannerMessage = this._hideBannerMessage.asObservable();

  /**
   * @param text Text to be shown as notification banner.
   * @param bannerClass Type of notification.
   * @param buttons Buttons with associated links. Maximum are 2 buttons including the optinal dismiss button.
   * @param showDismissButton Whether to show a dismiss button. Hint: It will always be shown if there are not buttons.
   * @return Returns Id of the created banner.
   */
  public showBanner(
    text: string,
    bannerClass: NotificationTypes,
    buttons: BannerButton[],
    showDismissButton: boolean
  ): string {
    const multiply = 99;
    const bannerId = 'bannerID_' + Date.now() + Math.floor(Math.random() * multiply);
    const detail = {
      text,
      bannerClass,
      buttons,
      showDismissButton,
      bannerId,
    };
    this._showBannerMessage.next(detail);
    return bannerId;
  }

  /**
   * @param bannerId Id of the banner that shall be hidden (it is returned when showing the banner).
   */
  public hideBanner(bannerId: string): void {
    this._hideBannerMessage.next(bannerId);
  }
}
