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

import { Component, ElementRef, EventEmitter, Input, OnChanges, OnInit, Output, Renderer2, SimpleChanges } from '@angular/core';
import { MatDialogRef } from '@angular/material/dialog';

@Component({
  selector: 'bci-modal-component',
  templateUrl: './modal.component.html',
  styleUrls: ['./modal.component.scss'],
})
export class ModalComponent implements OnInit, OnChanges {
  @Input() title = '';
  /** Additional title that is separated from normal title with a underline */
  @Input() additionalTitle = '';
  /** Optional parameter to hide the close icon on the right top corner */
  @Input() closeIcon = true;
  /** Optional parameter to deactivate the close icon on the right top corner */
  @Input() closeIconDisabled = false;
  /** Optional parameter to display an optional Button on the outer left with setting the title */
  @Input() optionalButtonTitle: string;
  /** Optional parameter to select whether the optionalButton is primary or secondary */
  @Input() optionalButtonAsPrimary = false;
  /** Optional parameter to disable optional Button */
  @Input() optionalButtonDisabled = true;
  /** Optional parameter to set whether or not to close the modal when clicking on backdrop or hitting the esc key */
  @Input() closeOnBackdropClick = true;
  /** Optional parameter to set the Dialog in a closeable or non-closeable state */
  @Input() isDialogCloseable = true;

  /** Optional close handler to intercept close mechanism */
  @Output() closeHandler: EventEmitter<any> = new EventEmitter();
    /** Optional handler to intercept the optional button mechanism */
  @Output() optionalButtonHandler: EventEmitter<any> = new EventEmitter();


  constructor(public dialogRef: MatDialogRef<ModalComponent>, private renderer: Renderer2,private elementRef: ElementRef) {
    this.dialogRef.disableClose = true;
    this.dialogRef._containerInstance._config.disableClose = true;

    this.dialogRef.backdropClick().subscribe(() => {
      if (this.closeOnBackdropClick) {
        this.close();
      }
    });
    this.dialogRef.keydownEvents().subscribe(event => {
      if (event.key === "Escape" && this.closeOnBackdropClick) {
          this.close();
      }
  });
  }
  ngOnChanges(changes: SimpleChanges): void {
    if(changes.isDialogCloseable){
      if(changes.isDialogCloseable.currentValue){
        this.closeIconDisabled = false;
        this.closeOnBackdropClick = true;
      } else if (!changes.isDialogCloseable.currentValue){
        this.closeIconDisabled = true;
        this.closeOnBackdropClick = false;
      } 
    }
  }

  ngOnInit() {
    // Added class to differentiate from other overlays ex. chiplist overlay, dropdown overlay etc
    if(this.elementRef.nativeElement.offsetParent?.offsetParent?.parentElement?.parentElement?.classList.contains('cdk-overlay-pane')) {
      this.renderer.addClass(this.elementRef.nativeElement.offsetParent.offsetParent.parentElement.parentElement, 'bci-modal-dialog');
    }

    // Added class if the additional title is passed as input instead as config
    if(this.additionalTitle && !this.elementRef.nativeElement.offsetParent?.offsetParent?.parentElement?.parentElement?.classList.contains('additional-title')) {
      this.renderer.addClass(this.elementRef.nativeElement.offsetParent.offsetParent.parentElement.parentElement, 'additional-title');
    }
  }

  close() {
    this.closeHandler.observers.length > 0 ? this.closeHandler.emit() : this.dialogRef.close();
  }
  optional() {
    this.optionalButtonHandler.observers.length > 0 ? this.optionalButtonHandler.emit() : this.dialogRef.close();
  }
}
