import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { IRouteConfig } from '@shared/ui-api/interfaces/route-config/route-config';
import { VolumeListComponent } from './volume-list/volume-list.component';
import { VolumeCreateComponent } from './volume-create/volume-create.component';
import { VolumeDetailsComponent } from './volume-details/volume-details.component';
import { VolumeResolver } from '@fleio-api/openstack/volume/volume.resolver';
import { VolumePermissionsResolver } from '@fleio-api/openstack/volume/volume-permissions.resolver';
import { VolumeListResolver } from '@fleio-api/openstack/volume/volume-list.resolver';
import { VolumeCreateOptionsResolver } from '@fleio-api/openstack/volume/volume-create-options.resolver';
import { FleioObjectsList } from '@fleio-api/fleio-objects-list';
import { IBaseFleioObjectModel } from '@fleio-api/base-model/base-fleio-object.model';
import { OrderingDirection } from '@shared/ui-api/interfaces/route-config/ordering-directions';
import { AuthGuard } from '@shared/auth/auth.guard';
import { FilterTypes } from '@shared/ui-api/interfaces/route-config/filter-types';
import { VolumeBackupsResolver } from '@fleio-api/openstack/volume/volume-backups.resolver';
import { VolumeSnapshotsResolver } from '@fleio-api/openstack/volume/volume-snapshots.resolver';
import { ImageCreateOptionsResolver } from '@fleio-api/openstack/image/image-create-options.resolver';
import { VolumeUploadToImageComponent } from './volume-upload-to-image/volume-upload-to-image.component';

const routes: Routes = [
  {
    path: '',
    component: VolumeListComponent,
    resolve: {
      volumes: VolumeListResolver,
    },
    canActivate: [AuthGuard],
    data: {
      config: {
        feature: 'openstack.volumes',
        search: {
          show: true,
          placeholder: $localize`Search volumes ...`,
        },
        subheader: {
          objectNamePlural: $localize`volumes`,
          objectName: $localize`volume`,
          objectList(data): FleioObjectsList<IBaseFleioObjectModel> {
            return data.volumes;
          }
        },
        ordering: {
          default: {
            display: $localize`Date created`,
            field: 'created_at',
            direction: OrderingDirection.Descending,
          },
          options: [
            {display: $localize`Name`, field: 'name'},
            {display: $localize`Status`, field: 'status'},
            {display: $localize`Availability zone`, field: 'availability_zone'},
            {display: $localize`Size`, field: 'size'},
            {display: $localize`Date created`, field: 'created_at'},
          ]
        },
        filterConfig: {
          availableOptions: [
            {
              display: $localize`Created at`,
              field: 'created_at',
              type: FilterTypes.Date
            },
            {
              display: $localize`Updated at`,
              field: 'updated_at',
              type: FilterTypes.Date
            },
            {
              display: $localize`Status`,
              field: 'status',
              type: FilterTypes.Choices
            },
            {
              display: $localize`Availability zone`,
              field: 'availability_zone',
              type: FilterTypes.Choices
            },
            {
              display: $localize`Size`,
              field: 'size',
              type: FilterTypes.Decimal
            },
            {
              display: $localize`Region`,
              field: 'region',
              type: FilterTypes.CustomModel,
              items: 'regions',
              itemsDisplayField: 'id'
            }
          ]
        }
      } as IRouteConfig,
    },
    runGuardsAndResolvers: 'always',
  },
  {
    path: 'create',
    component: VolumeCreateComponent,
    resolve: {
      createOptions: VolumeCreateOptionsResolver,
    },
    data: {
      config: {
        getBreadCrumbDetail: () => $localize`Create volume`,
      } as IRouteConfig,
    }
  },
  {
    path: ':id/upload-to-image',
    component: VolumeUploadToImageComponent,
    resolve: {
      volume: VolumeResolver,
      permissions: VolumePermissionsResolver,
      imageCreateOptions: ImageCreateOptionsResolver,
    },
    data: {
      config: {
        getBreadCrumbDetail: (data) => $localize`Upload volume ${data.volume.name || data.volume.id} to image`,
      } as IRouteConfig,
    },
    runGuardsAndResolvers: 'always',
  },
  {
    path: ':id',
    component: VolumeDetailsComponent,
    resolve: {
      volume: VolumeResolver,
      permissions: VolumePermissionsResolver,
      backups: VolumeBackupsResolver,
      snapshots: VolumeSnapshotsResolver,
    },
    data: {
      config: {
        getBreadCrumbDetail: (data) => $localize`Volume ${data.volume.name || data.volume.id}`,
      } as IRouteConfig,
    },
    runGuardsAndResolvers: 'always',
  },
];

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