import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { IRouteConfig } from '@shared/ui-api/interfaces/route-config/route-config';
import { SshKeyListComponent } from './ssh-key-list/ssh-key-list.component';
import { SshKeyCreateComponent } from './ssh-key-create/ssh-key-create.component';
import { SshKeyEditComponent } from './ssh-key-edit/ssh-key-edit.component';
import { PublicKeyListResolver } from '@fleio-api/public-key/public-key-list.resolver';
import { PublicKeyResolver } from '@fleio-api/public-key/public-key.resolver';
import { PublicKeyPermissionsResolver } from '@fleio-api/public-key/public-key-permissions.resolver';
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 { SshKeyDetailsComponent } from './ssh-key-details/ssh-key-details.component';

const routes: Routes = [
  {
    path: '',
    component: SshKeyListComponent,
    resolve: {
      sshKeys: PublicKeyListResolver,
    },
    canActivate: [AuthGuard],
    data: {
      config: {
        feature: 'openstack.sshkeys',
        getTitleDisplay: () => $localize`SSH keys`,
        search: {
          show: true,
          placeholder: $localize`Search SSH keys ...`,
        },
        subheader: {
          objectList(data): FleioObjectsList<IBaseFleioObjectModel> {
            return data.sshKey;
          },
          objectName: $localize`SSH key`,
          objectNamePlural: $localize`SSH keys`,
        },
        ordering: {
          default: {
            field: 'created_at',
            display: $localize`Created at`,
            direction: OrderingDirection.Descending
          },
          options: [
            {
              display: $localize`Name`,
              field: 'name',
            },
            {
              display: $localize`Date created`,
              field: 'created_at',
            }
          ]
        },
        filterConfig: {
          availableOptions: [
            {
              display: $localize`Created at`,
              field: 'created_at',
              type: FilterTypes.Date
            },
            {
              display: $localize`User`,
              field: 'user',
              type: FilterTypes.CustomModel,
              items: 'users',
              itemsDisplayField: 'display'
            }
          ]
        }
      } as IRouteConfig,
    },
    runGuardsAndResolvers: 'always'
  },
  {
    path: 'create',
    component: SshKeyCreateComponent,
    resolve: {},
    data: {
      config: {
        getBreadCrumbDetail: () => $localize`Create SSH key`,
      } as IRouteConfig,
    }
  },
  {
    path: ':id',
    component: SshKeyDetailsComponent,
    resolve: {
      sshKey: PublicKeyResolver,
      permissions: PublicKeyPermissionsResolver,
    },
    data: {
      config: {
        getBreadCrumbDetail: (data) => data.sshKey.name,
      } as IRouteConfig,
    },
    runGuardsAndResolvers: 'always',
  },
  {
    path: ':id/edit',
    component: SshKeyEditComponent,
    resolve: {
      sshKey: PublicKeyResolver,
      permissions: PublicKeyPermissionsResolver,
    },
    data: {
      config: {
        getBreadCrumbDetail: (data) => data.sshKey.name,
      } as IRouteConfig,
    }
  },
];

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