import { Component, Inject, OnInit } from '@angular/core';
import { IBaseDialog } from '@shared/common-dialogs/base-dialog';
import { IAction } from '@objects-view/interfaces/actions/action';
import { DialogCallbackAction } from '@objects-view/actions/dialog-callback-action';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { SingleSignOnSettingsApiService } from '@fleio-api/core/single-sign-on-settings-api.service';
import { Clipboard } from '@angular/cdk/clipboard';

@Component({
  selector: 'app-get-saml2-metadata-dialog',
  templateUrl: './get-saml2-metadata-dialog.component.html',
  styleUrls: ['./get-saml2-metadata-dialog.component.scss']
})
export class GetSaml2MetadataDialogComponent implements IBaseDialog, OnInit {
  dialogTitle = $localize`Saml2 metadata`;
  metadata: string;
  dialogActions: Array<IAction> = [
    new DialogCallbackAction({
      name: $localize`Close`,
      primary: true,
      callback: () => this.close(),
    }),
    new DialogCallbackAction({
      name: $localize`Copy to clipboard`,
      primary: true,
      callback: () => this.copyToClipboard(),
    }),
  ];

  constructor(
    private ssoSettingsApi: SingleSignOnSettingsApiService,
    private clipboard: Clipboard,
    public dialogRef: MatDialogRef<GetSaml2MetadataDialogComponent>,
    @Inject(MAT_DIALOG_DATA) public data: any,
  ) {

  }

  ngOnInit(): void {
    this.ssoSettingsApi.getSaml2Metadata().subscribe(metadata => {
      this.metadata = metadata;
    }).add(() => {
    });
  }

  close() {
    this.dialogRef.close();
  }

  private copyToClipboard() {
    this.clipboard.copy(this.metadata);
  }
}
