import { Component, OnInit } from '@angular/core';
import { DetailsComponentBase } from '@objects-view/details-component-base';
import { IAction } from '@objects-view/interfaces/actions/action';
import { ConfigService } from '@shared/config/config.service';
import { INetworkModel } from '@fleio-api/openstack/model/network.model';
import { ISubnetModel } from '@fleio-api/openstack/model/subnet.model';
import { SubnetsApiService } from '@fleio-api/openstack/subnets/subnets-api.service';
import { ApiCallAction, CallType } from '@objects-view/actions/api-call-action';
import { RouterLinkAction } from '@objects-view/actions/router-link-action';
import { Router } from '@angular/router';

@Component({
  selector: 'app-network-details-overview',
  templateUrl: './network-details-overview.component.html',
  styleUrls: ['./network-details-overview.component.scss']
})
export class NetworkDetailsOverviewComponent extends DetailsComponentBase<INetworkModel> implements OnInit {
  public subnetActions: { [id: number]: Array<IAction> };
  displayedColumns = ['name', 'network_address', 'ip_version', 'gateway_ip', '(actions)'];

  constructor(
    public config: ConfigService,
    private subnetsApiService: SubnetsApiService,
    private router: Router,
  ) {
    super();
  }

  ngOnInit() {
    super.ngOnInit();
    if (this.object && this.object.subnets) {
      this.subnetActions = {};
      for (const subnet of this.object.subnets) {
        this.subnetActions[subnet.id] = this.getSubnetActions(subnet);
      }
    }
  }

  getSubnetActions(subnet: ISubnetModel): IAction[] {
    const actions = [];
    if (subnet.has_edit_permission ||
      this.authSvc.feature('ui.openstack.networks.always-allow-subnet-management')) {
      actions.push(new RouterLinkAction({
          icon: {name: 'edit', class: 'fl-icons'},
          name: $localize`Edit subnet`,
          tooltip: $localize`Edit subnet`,
          routerUrl: this.config.getPanelUrl(`openstack/networks/${this.object.id}/edit-subnet/${subnet.id}`),
          router: this.router,
        }
      ));
    }
    if (subnet.has_delete_permission ||
      this.authSvc.feature('ui.openstack.networks.always-allow-subnet-management')) {
      actions.push(new ApiCallAction(
        {
          object: subnet,
          icon: {name: 'delete'},
          tooltip: $localize`Delete subnet`,
          name: $localize`Delete subnet`,
          confirmOptions: {
            confirm: true,
            title: $localize`Delete subnet`,
            message: $localize`Are you sure you want to delete subnet ${subnet.name}?`
          },
          successMessage: $localize`Subnet delete queued`,
          errorMessage: $localize`Failed to delete subnet`,
          apiService: this.subnetsApiService,
          callType: CallType.Delete,
          refreshAfterExecute: false,
          redirectAfterExecute: true,
          redirectUrl: this.config.getPanelUrl(`openstack/networks/${this.object.id}`),
        }
      ));
    }
    return actions;
  }
}
