import { Component, Inject, OnInit } from '@angular/core';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { IOpenstackNotificationsModel } from '@fleio-api/core/model/openstack-notifications.model';
import { IOpenstackNotificationsTestResultsModel } from '@fleio-api/core/model/openstack-notifications-test-results.model';
import { SettingsOpenstackApiService } from '@fleio-api/core/settings-openstack-api.service';
import { IAction } from '@objects-view/interfaces/actions/action';
import { DialogCallbackAction } from '@objects-view/actions/dialog-callback-action';
import { IBaseDialog } from '@shared/common-dialogs/base-dialog';

@Component({
  selector: 'app-test-notifications-dialog',
  templateUrl: './test-notifications-dialog.component.html',
  styleUrls: ['./test-notifications-dialog.component.scss']
})
export class TestNotificationsDialogComponent implements IBaseDialog, OnInit {
  testResults: IOpenstackNotificationsTestResultsModel;
  testError: string;
  loading: boolean;
  connectedTxt = $localize`Successfully connected`;
  notConnectedTxt = $localize`Connection was not successful`;
  dialogTitle = $localize`Test results`;
  dialogActions: Array<IAction> = [
    new DialogCallbackAction({
      name: $localize`Ok`,
      primary: true,
      callback: () => this.dialogRef.close(false),
    }),
  ];

  constructor(
    public dialogRef: MatDialogRef<TestNotificationsDialogComponent>,
    @Inject(MAT_DIALOG_DATA) public data: { notifications: IOpenstackNotificationsModel },
    private settingsOpenstackApi: SettingsOpenstackApiService,
  ) {
  }

  ngOnInit(): void {
    this.loading = true;
    this.settingsOpenstackApi.testNotifications(this.data.notifications).subscribe(
      testResults => {
        this.testResults = testResults;
      },
      error => {
        this.testError = error;
      },
    ).add(()=>{
      this.loading = false;
    });
  }
}
