import { ChangeDetectorRef, Component, NgZone, OnInit } from '@angular/core';
import { DetailsComponentBase } from '@objects-view/details-component-base';
import { IRouterModel } from '@fleio-api/openstack/model/router.model';
import { IAction } from '@objects-view/interfaces/actions/action';
import { CallbackAction } from '@objects-view/actions/callback-action';
import { map } from 'rxjs/operators';
import { INetworkInterfaceModel } from '@fleio-api/openstack/model/network-interface.model';
import { RoutersApiService } from '@fleio-api/openstack/routers/routers-api.service';
import { RefreshService } from '@shared/ui-api/refresh.service';
import { ConfigService } from '@shared/config/config.service';
import { NotificationService } from '@shared/ui-api/notification.service';

@Component({
  selector: 'app-router-details-overview',
  templateUrl: './router-details-overview.component.html',
  styleUrls: ['./router-details-overview.component.scss']
})
export class RouterDetailsOverviewComponent extends DetailsComponentBase<IRouterModel> implements OnInit {
  interfaceActions: { [id: string]: IAction[] };

  constructor(
    private routersApiService: RoutersApiService,
    private refreshService: RefreshService,
    public configService: ConfigService,
    private notificationService: NotificationService,
    ngZone: NgZone,
    changeDetectorRef: ChangeDetectorRef,
  ) {
    super(ngZone, changeDetectorRef, configService);
  }

  ngOnInit() {
    super.ngOnInit();
    if (this.object && this.object.interfaces) {
      this.updateInterfaceActions();
    }

    if (this.objectController) {
      this.objectController.dataChanged$.subscribe(() => {
        this.updateInterfaceActions();
      });
    }
  }

  private updateInterfaceActions() {
    this.interfaceActions = {};
    for (const netInt of this.object.interfaces) {
      this.interfaceActions[netInt.id] = this.getInterfaceActions(netInt);
    }
  }

  getInterfaceActions(netInt: INetworkInterfaceModel): IAction[] {
    return [
      new CallbackAction({
        icon: {name: 'delete'},
        tooltip: $localize`Remove interface`,
        name: $localize`Remove interface`,
        displayMessages: false,
        confirmOptions: {
          confirm: true,
          title: $localize`Remove interface`,
          message: $localize`Are you sure you want to remove interface ${netInt.network_name} from router?`,
        },
        refreshAfterExecute: true,
        noPermissions: !this.objectController.permissions['routers.update'],
        callback: () => this.routersApiService.removeInterface(netInt.id, this.object.id).pipe(map(() => {
            this.notificationService.showMessage($localize`Interface removed`);
            this.refreshService.refresh();
            return null;
          }))
      }),
    ];
  }
}
