import { ChangeDetectorRef, Component, NgZone, OnInit } from '@angular/core';
import { DetailsComponentBase } from '@objects-view/details-component-base';
import { IOpenstackBackupSchedule } from '@fleio-api/openstack/model/openstack-backup-schedule.model';
import { NotificationService } from '@shared/ui-api/notification.service';
import { MatDialog } from '@angular/material/dialog';
import { ConfigService } from '@shared/config/config.service';
import { map } from 'rxjs/operators';
import { IActionResult } from '@objects-view/interfaces/actions/action-result';
import { IDbaasInstanceModel } from '@fleio-api/openstack/model/dbaas-instance.model';
import { DbaasInstancesApiService } from '@fleio-api/openstack/dbaas/dbaas-instances-api.service';
import {
  CreateDbaasBackupScheduleDialogComponent
} from '@shared/common-dialogs/openstack/dbaas/create-dbaas-backup-schedule-dialog/create-dbaas-backup-schedule-dialog.component';
import { DBaaSBackupSchedulesApiService } from '@fleio-api/openstack/dbaas/dbaas-backup-schedules-api.service';

@Component({
  selector: 'app-dbaas-instance-backup-schedules',
  templateUrl: './dbaas-instance-backup-schedules.component.html',
  styleUrl: './dbaas-instance-backup-schedules.component.scss'
})
export class DbaasInstanceBackupSchedulesComponent extends DetailsComponentBase<IDbaasInstanceModel> implements OnInit {
  schedules: Array<IOpenstackBackupSchedule> = null;
  schedulesDisplayedColumns = [
    'backup_name', 'backup_type', 'swift_container', 'incremental', 'run_at', 'actions'
  ];
  scheduleDateFormat: string;
  deleteScheduleText = $localize`Delete schedule`;

  constructor(
    private dbaasInstancesApiService: DbaasInstancesApiService,
    private dbaasBackupSchedulesApiService: DBaaSBackupSchedulesApiService,
    private notificationService: NotificationService,
    private matDialog: MatDialog,
    public config: ConfigService,
    ngZone: NgZone,
    changeDetectorRef: ChangeDetectorRef,
  ) {
    super(ngZone, changeDetectorRef, config);
  }

  deleteSchedule(schedule: IOpenstackBackupSchedule) {
    const dialogResult$ = this.notificationService.confirmDialog({
      title: 'Delete backup schedule?',
      message: 'Are you sure?',
    });
    dialogResult$.subscribe(dialogResult => {
      if (dialogResult?.button === 'yes') {
        this.dbaasBackupSchedulesApiService.delete(schedule.id).subscribe(response => {
          this.notificationService.showMessage($localize`Successfully deleted backup schedule.`);
          this.boostRefreshTimer();
        }, error => {
          this.notificationService.showMessage($localize`Failed to delete backup schedule.`);
        });
      }
    });
  }

  protected refreshData() {
    this.dbaasInstancesApiService.getBackupSchedules(this.object.id).subscribe({
      next: response => {
        this.schedules = response.schedules;
      }, error: error => {
        this.notificationService.showMessage($localize`Failed to load backup schedules.`);
      }
    });
  }

  public openCreateBackupScheduleDialog() {
    return this.matDialog.open(
      CreateDbaasBackupScheduleDialogComponent, {
        data: {
          database_instance: this.object
        },
        autoFocus: false,
      }).afterClosed().pipe(map(result => {
      if (result === true || result === 'true') {
        this.boostRefreshTimer();
        this.notificationService.showMessage($localize`Added a new backup schedule`);
      }
      return {message: result} as IActionResult;
    })).subscribe();
  }

  ngOnInit(): void {
    super.ngOnInit();
    if (!this.config.authService.feature('ui.openstack.instances.backup_schedules.select_hour')) {
      this.scheduleDateFormat = this.authSvc.dateFormat;
    } else {
      this.scheduleDateFormat = this.authSvc.dateTimeFormat;
    }
  }

}
