import { ChangeDetectorRef, Component, NgZone, OnInit } from '@angular/core';
import { DetailsComponentBase } from '@objects-view/details-component-base';
import { IInstanceModel } from '@fleio-api/openstack/model/instance.model';
import { InstancesApiService } from '@fleio-api/openstack/instance/instances-api.service';
import { ISecurityGroupModel } from '@fleio-api/openstack/model/security-group.model';
import { NotificationService } from '@shared/ui-api/notification.service';
import { map } from 'rxjs/operators';
import { IActionResult } from '@objects-view/interfaces/actions/action-result';
import { MatDialog } from '@angular/material/dialog';
import { InstanceAssociateSecurityGroupDialogComponent } from '@shared/common-dialogs/openstack/instances/instance-associate-security-group-dialog/instance-associate-security-group-dialog.component';
import { ConfigService } from '@shared/config/config.service';

@Component({
  selector: 'app-instance-details-security-groups',
  templateUrl: './instance-details-security-groups.component.html',
  styleUrls: ['./instance-details-security-groups.component.scss']
})
export class InstanceDetailsSecurityGroupsComponent extends DetailsComponentBase<IInstanceModel> implements OnInit {
  securityGroups: Array<ISecurityGroupModel> = null;
  permissions: Record<string, unknown> = {};
  dissociateSecurityGroupTxt = $localize`Dissociate security group`;

  constructor(
    private instancesApiService: InstancesApiService,
    private notificationService: NotificationService,
    private readonly matDialog: MatDialog,
    public config: ConfigService,
    ngZone: NgZone,
    changeDetectorRef: ChangeDetectorRef,
  ) {
    super(ngZone, changeDetectorRef, config);
  }

  protected refreshData() {
    this.instancesApiService.getSecurityGroups(this.object.id).subscribe(response => {
      this.securityGroups = response.security_groups;
    }, error => {
      this.notificationService.showMessage($localize`Could not load security groups.`);
    });
  }

  public dissociateGroup(group: ISecurityGroupModel) {
    const dialogResult$ = this.notificationService.confirmDialog({
      title: $localize`Dissociate security group ${group.name || group.id}?`,
      message: $localize`Are you sure?`,
    });
    dialogResult$.subscribe(dialogResult => {
      if (dialogResult?.button === 'yes') {
        this.instancesApiService.dissociateSecurityGroup(
          this.object.id,
          group.id,
        ).subscribe(response => {
          this.notificationService.showMessage($localize`Security group dissociation scheduled.`);
          this.boostRefreshTimer();
        }, error => {
          this.notificationService.showMessage($localize`Failed to dissociate security group.`);
        });
      }
    });
  }

  public openAssociateSecurityGroupDialog() {
    return this.matDialog.open(
      InstanceAssociateSecurityGroupDialogComponent, {
        data: {
          instance: this.object
        },
        autoFocus: false,
    }).afterClosed().pipe(map(result => {
      if (result === true || result === 'true') {
        this.boostRefreshTimer();
        this.notificationService.showMessage($localize`Associate group scheduled`);
      }
      return {message: result} as IActionResult;
    })).subscribe();
  }

  ngOnInit() {
    super.ngOnInit();
    if (this.objectController) {
      this.permissions = this.objectController.additionalObjects.permissions;
    }
  }

}
