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

import { AfterViewInit, ChangeDetectorRef, Component, Inject, OnDestroy, OnInit } from '@angular/core';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { AlertBoxOptions } from '../models/alert-box-options';
import { AlertClosingType, AlertType, PrimaryButton } from '../models/alert.enums';
import{ BOSCH_DDS_COLORS } from '../../core-configuration/bosch-dds-colors';
import { BciAlertDialogIntl } from '../bci-alert-dialog-intl';
import { takeUntil } from 'rxjs/operators';
import { Subject } from 'rxjs';

@Component({
  selector: 'bci-alert',
  templateUrl: './alert-dialog.component.html',
  styleUrls: ['./alert-dialog.component.scss'],
})
export class AlertDialogComponent implements OnInit, AfterViewInit, OnDestroy {
  AlertType = AlertType;
  AlertClosingType = AlertClosingType;
  PrimaryButton = PrimaryButton;

  private ngUnsubscribe = new Subject<void>();

  additionalTitle: string;

  constructor(
    @Inject(MAT_DIALOG_DATA) public data: AlertBoxOptions,
    public dialogRef: MatDialogRef<AlertDialogComponent>,
    public bciAlertDialogIntl: BciAlertDialogIntl,
    private changeDetector: ChangeDetectorRef
  ) {
  }

  ngOnInit() {
    this.bciAlertDialogIntl.changes
      .pipe(takeUntil(this.ngUnsubscribe))
      .subscribe(() => this.changeDetector.detectChanges());
    
    this.setAdditionalTitle();
  }

  ngAfterViewInit(): void {
    /*This adds a colored border at the top for the alert dialog. If 2 or more dialogs are open only the last dialog should get a colored border*/
    const dialogContainer = document.getElementsByClassName('mat-mdc-dialog-container');
    const dialogLength = dialogContainer.length;
    if(dialogContainer[dialogLength-1] !== undefined){
      switch (this.data.type) {
        case AlertType.Info:
          dialogContainer[dialogLength-1].setAttribute("style", `border-top: ${BOSCH_DDS_COLORS.boschBlue50} 6px solid;`);
          break;
        case AlertType.Success:
          dialogContainer[dialogLength-1].setAttribute("style", `border-top: ${BOSCH_DDS_COLORS.boschGreen50} 6px solid;`);
          break;
        case AlertType.Warning:
          dialogContainer[dialogLength-1].setAttribute("style", `border-top: ${BOSCH_DDS_COLORS.boschYellow85} 6px solid;`);
          break;
        case AlertType.Error:
          dialogContainer[dialogLength-1].setAttribute("style", `border-top: ${BOSCH_DDS_COLORS.boschRed50} 6px solid;`);
          break;
      }
    }
  }

  ngOnDestroy(): void {
    this.ngUnsubscribe.next();
    this.ngUnsubscribe.complete();
  }

  close(type: AlertClosingType): void {
    this.dialogRef.close(type);
  }
  /**
   * Sets the additional title of the alert dialog
   * A custom additional title can only be applied if the alert type is None. Otherwise the actual value of the alert type is the additional title.
   */
  setAdditionalTitle(): void {
    if (this.data.type === AlertType.None) {
      this.additionalTitle = this.data.additionalTitle;
    }
    if (this.data.type === AlertType.Info) {
      this.additionalTitle = this.bciAlertDialogIntl.info;
    }
    if (this.data.type === AlertType.Success) {
      this.additionalTitle = this.bciAlertDialogIntl.success;
    }
    if (this.data.type === AlertType.Warning) {
      this.additionalTitle = this.bciAlertDialogIntl.warning;
    }
    if (this.data.type === AlertType.Error) {
      this.additionalTitle = this.bciAlertDialogIntl.error;
    }
  }
}
