/*
 * Copyright (C) 2021 Robert Bosch GmbH Copyright (C) 2021 Robert Bosch Manufacturing Solutions GmbH, Germany. All rights reserved.
 */
import { Component, OnInit } from '@angular/core';
import { NotificationBannerService } from '../../notification/notification-banner.service';
import { BannerMessage, NotificationTypes } from '../../notification/notification.models';

@Component({
  selector: 'bci-banner',
  templateUrl: './bci-banner.component.html',
  styleUrls: ['./bci-banner.component.scss'],
})
export class BciBannerComponent implements OnInit {
  private multiBanners: BannerMessage[] = [];

  constructor(public notificationBannerService: NotificationBannerService) {}

  ngOnInit() {
    this.notificationBannerService.showBannerMessage.subscribe((data) => {
      if (!data) {
        return;
      }
      const buttons = !!data.buttons ? data.buttons : [];
      const bannerMessage = {
        text: data.text || '',
        bannerClass: data.bannerClass || NotificationTypes.Information,
        buttons: buttons,
        showDismissButton: !!data.showDismissButton || buttons.length === 0,
        bannerId: data.bannerId || '',
      };

      this.multiBanners.push(bannerMessage);

      if (bannerMessage.buttons.length > 1 && bannerMessage.showDismissButton) {
        bannerMessage.buttons = bannerMessage.buttons.slice(0, 1);
        console.error('The banner component can only display a maximum of two buttons.');
      }
      if (bannerMessage.buttons.length > 2 && !bannerMessage.showDismissButton) {
        bannerMessage.buttons = bannerMessage.buttons.slice(0, 2);
        console.error('The banner component can only display a maximum of two buttons.');
      }
    });
    this.notificationBannerService.hideBannerMessage.subscribe((bannerId) => {
      this.dismiss(bannerId);
    });
  }

  /**
   * return type of class name for Notification Banner messages
   */
  getFontIcon(bannerClass: NotificationTypes): string {
    switch (bannerClass) {
      case NotificationTypes.Error:
        return 'Bosch-Ic-notification-error-outline';
      case NotificationTypes.Success:
        return 'Bosch-Ic-notification-success-outline';
      case NotificationTypes.Warning:
        return 'Bosch-Ic-notification-outline';
      case NotificationTypes.Information:
        return 'Bosch-Ic-notification-info';
      default:
        return '';
    }
  }

  dismiss(bannerId: string) {
    this.multiBanners = this.multiBanners.filter((item) => item.bannerId !== bannerId);
  }

  get hasBanner(): boolean {
    return this.multiBanners.length > 0;
  }

  get banner(): BannerMessage {
    return this.multiBanners[this.multiBanners.length - 1];
  }
}
