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

import { Component, Inject, ViewContainerRef } from '@angular/core';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { AlertService, AlertType, PrimaryButton } from '@bci-web-core/core';

@Component({
  selector: 'bci-modal-window-demo-all-features-modal',
  templateUrl: './modal-window-demo-all-features-modal.component.html',
  styleUrls: ['./modal-window-demo-all-features-modal.component.scss'],
})
export class ModalWindowDemoAllFeaturesModalComponent {
  constructor(
    private dialogRef: MatDialogRef<ModalWindowDemoAllFeaturesModalComponent>,
    private alertService: AlertService,
    @Inject(MAT_DIALOG_DATA) public data: any,
    private viewContainerRef: ViewContainerRef,
  ) {}

  close(): void {
    console.log('Closed by backdrop or icon');
    this.dialogRef.close();
  }

  delete(): void {
    const ref = this.alertService.openAlertBox({
      title: 'Delete Item?',
      description: 'This action will delete this item.',
      type: AlertType.Warning,
      leftButtonTitle: 'Delete',
      rightButtonTitle: 'Cancel',
      primaryButton: PrimaryButton.RightButton,
    }, this.viewContainerRef);

    ref.afterClosed().subscribe((res) => {
      if (res === 'left') {
        console.log('Delete Item');
        this.dialogRef.close();
      }
    });
  }

  save(): void {
    this.dialogRef.close(this.data.postalCode);
  }
  
  cancel(): void {
    const ref = this.alertService.openAlertBox({
      title: 'Discard the draft?',
      description: 'Discarding the draft will delete any changes on this item.',
      type: AlertType.Warning,
      leftButtonTitle: 'Discard',
      rightButtonTitle: 'Cancel',
      primaryButton: PrimaryButton.RightButton,
    }, this.viewContainerRef);

    ref.afterClosed().subscribe((res) => {
      if (res === 'left') {
        this.dialogRef.close();
      }
    });
  }
}
