import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { IRouteConfig } from '@shared/ui-api/interfaces/route-config/route-config';
import { AuthGuard } from '@shared/auth/auth.guard';
import { IBaseFleioObjectModel } from '@fleio-api/base-model/base-fleio-object.model';
import { FleioObjectsList } from '@fleio-api/fleio-objects-list';
import { OrderingDirection } from '@shared/ui-api/interfaces/route-config/ordering-directions';
import { FilterTypes } from '@shared/ui-api/interfaces/route-config/filter-types';
import { HypervisorsListComponent } from './hypervisors-list/hypervisors-list.component';
import { HypervisorDetailsComponent } from './hypervisor-details/hypervisor-details.component';
import { HypervisorsListResolver } from '@fleio-api/openstack/hypervisor/hypervisors-list.resolver';
import { HypervisorResolver } from '@fleio-api/openstack/hypervisor/hypervisor.resolver';

const routes: Routes = [
  {
    path: '',
    component: HypervisorsListComponent,
    resolve: {
      hypervisors: HypervisorsListResolver,
    },
    canActivate: [AuthGuard],
    data: {
      config: {
        feature: 'openstack.hypervisors',
        search: {
          show: true,
          placeholder: $localize`Search hypervisors ...`,
        },
        subheader: {
          objectList(data): FleioObjectsList<IBaseFleioObjectModel> {
            return data.hypervisors;
          },
          objectName: $localize`hypervisor`,
          objectNamePlural: $localize`hypervisors`,
        },
        ordering: {
          default: {
            field: 'host_name',
            display: $localize`Host name`,
            direction: OrderingDirection.Descending
          },
          options: [
            {
              display: $localize`Host name`,
              field: 'host_name',
            },
            {
              display: $localize`Host IP`,
              field: 'host_ip',
            },
            {
              display: $localize`Region`,
              field: 'region',
            },
            {
              display: $localize`Status`,
              field: 'status',
            },
            {
              display: $localize`State`,
              field: 'state',
            }
          ]
        },
        filterConfig: {
          availableOptions: [
            {
              field: 'status',
              display: $localize`Status`,
              type: FilterTypes.Choices
            },
            {
              field: 'state',
              display: $localize`State`,
              type: FilterTypes.Choices
            },
            {
              display: $localize`Region`,
              field: 'region',
              type: FilterTypes.CustomModel,
              items: 'regions',
              itemsDisplayField: 'id',
            },
          ]
        }
      } as IRouteConfig,
    },
    runGuardsAndResolvers: 'always'
  },
  {
    path: ':id',
    component: HypervisorDetailsComponent,
    resolve: {
      hypervisor: HypervisorResolver,
    },
    data: {
      config: {
        getBreadCrumbDetail: (data) => data.hypervisor.host_name || data.hypervisor.id,
      } as IRouteConfig,
    }
  },
];

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