import { NgModule } from '@angular/core';
import { ActivatedRouteSnapshot, RouterModule, Routes } from '@angular/router';
import { IRouteConfig } from '@shared/ui-api/interfaces/route-config/route-config';
import { FleioObjectsList } from '@fleio-api/fleio-objects-list';
import { IBaseFleioObjectModel } from '@fleio-api/base-model/base-fleio-object.model';
import { AuthGuard } from '@shared/auth/auth.guard';
import { LoadBalancerListComponent } from './load-balancer-list/load-balancer-list.component';
import { LoadBalancerCreateComponent } from './load-balancer-create/load-balancer-create.component';
import { LoadBalancerDetailsComponent } from './load-balancer-details/load-balancer-details.component';
import { LoadBalancerListResolver } from '@fleio-api/openstack/load-balancing/load-balancers/load-balancer-list.resolver';
import { LoadBalancerResolver } from '@fleio-api/openstack/load-balancing/load-balancers/load-balancer.resolver';
import { LoadBalancerEditComponent } from './load-balancer-edit/load-balancer-edit.component';
import { LoadBalancerAssociateFloatingIpComponent } from './load-balancer-associate-floating-ip/load-balancer-associate-floating-ip.component';
import { LbAssociateFloatingIpOptionsResolver } from '@fleio-api/openstack/load-balancing/load-balancers/lb-associate-floating-ip-options.resolver';
import { LbCreateOptionsResolver } from '@fleio-api/openstack/load-balancing/load-balancers/lb-create-options.resolver';

const routes: Routes = [
  {
    path: '',
    component: LoadBalancerListComponent,
    resolve: {
      loadBalancers: LoadBalancerListResolver
    },
    canActivate: [AuthGuard],
    data: {
      config: {
        feature: 'openstack.lbaas',
        getTitleDisplay: () => $localize`Load balancers`,
        subheader: {
          objectNamePlural: $localize`load balancers`,
          objectName: $localize`load balancer`,
          showRegionSelector: true,
          regionSelectorFeature: 'openstack.lbaas.show-region-select',
          regionSelectorDefaultRegionKey: 'is_lbaas_default',
          objectList(data): FleioObjectsList<IBaseFleioObjectModel> {
            return data.loadBalancers;
          }
        },
      } as IRouteConfig,
    },
    runGuardsAndResolvers: 'always',
  },
  {
    path: 'create',
    component: LoadBalancerCreateComponent,
    resolve: {
      createOptions: LbCreateOptionsResolver,
    },
    data: {
      config: {
        getBreadCrumbDetail: () => $localize`Create load balancer`,
      } as IRouteConfig,
    }
  },
  {
    path: ':loadBalancerId/listeners',
    resolve: {
      loadBalancer: LoadBalancerResolver
    },
    data: {
      config: {
        getBreadCrumbDetail: (data) => `${data.loadBalancer.name || data.loadBalancer.id}`,
        getBreadCrumbQueryParams(snapshot: ActivatedRouteSnapshot): { [key: string]: any } {
          const queryParams: { [key: string]: any } = {};
          if (snapshot.queryParams?.region) {
            queryParams.region = snapshot.queryParams?.region;
          }
          return queryParams;
        },
      } as IRouteConfig,
    },
    loadChildren: () => import('./lb-listeners/lb-listeners.module').then(mod => mod.LbListenersModule),
    runGuardsAndResolvers: 'always',
  },
  {
    path: ':loadBalancerId/pools',
    resolve: {
      loadBalancer: LoadBalancerResolver
    },
    data: {
      config: {
        getBreadCrumbDetail: (data) => `${data.loadBalancer.name || data.loadBalancer.id}`,
        getBreadCrumbQueryParams(snapshot: ActivatedRouteSnapshot): { [key: string]: any } {
          const queryParams: { [key: string]: any } = {};
          if (snapshot.queryParams?.region) {
            queryParams.region = snapshot.queryParams?.region;
          }
          return queryParams;
        },
      } as IRouteConfig,
    },
    loadChildren: () => import('./lb-pools/lb-pools.module').then(mod => mod.LbPoolsModule),
    runGuardsAndResolvers: 'always',
  },
  {
    path: ':id/edit',
    component: LoadBalancerEditComponent,
    resolve: {
      loadBalancer: LoadBalancerResolver
    },
    data: {
      config: {
        getBreadCrumbDetail: (data) => $localize`Edit ${data.loadBalancer.name || data.loadBalancer.id}`,
      } as IRouteConfig,
    },
    runGuardsAndResolvers: 'always',
  },
  {
    path: ':id/associate-floating-ip',
    component: LoadBalancerAssociateFloatingIpComponent,
    resolve: {
      loadBalancer: LoadBalancerResolver,
      associateFloatingIpOptions: LbAssociateFloatingIpOptionsResolver,
    },
    data: {
      config: {
        getBreadCrumbDetail: (data) => $localize`Associate floating ip with ${data.loadBalancer.name || data.loadBalancer.id}`,
      } as IRouteConfig,
    },
    runGuardsAndResolvers: 'always',
  },
  {
    path: ':id',
    component: LoadBalancerDetailsComponent,
    resolve: {
      loadBalancer: LoadBalancerResolver
    },
    data: {
      config: {
        getBreadCrumbDetail: (data) => `${data.loadBalancer.name || data.loadBalancer.id}`,
      } as IRouteConfig,
    },
    runGuardsAndResolvers: 'always',
  },
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule],
})
export class LoadBalancersRoutingModule {
}
