import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { IRouteConfig } from '@shared/ui-api/interfaces/route-config/route-config';
import { ImageCreateComponent } from './image-create/image-create.component';
import { ImageEditComponent } from './image-edit/image-edit.component';
import { ImageDetailsComponent } from './image-details/image-details.component';
import { ImageListComponent } from './image-list/image-list.component';
import { ImageListResolver } from '@fleio-api/openstack/image/image-list.resolver';
import { ImageResolver } from '@fleio-api/openstack/image/image.resolver';
import { ImageCreateOptionsResolver } from '@fleio-api/openstack/image/image-create-options.resolver';
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 { AuthGuard } from '@shared/auth/auth.guard';
import { FilterTypes } from '@shared/ui-api/interfaces/route-config/filter-types';
import { ImagePermissionsResolver } from '@fleio-api/openstack/image/image-permissions.resolver';

const routes: Routes = [
  {
    path: '',
    component: ImageListComponent,
    resolve: {
      images: ImageListResolver,
    },
    canActivate: [AuthGuard],
    data: {
      config: {
        feature: 'openstack.images',
        search: {
          show: true,
          placeholder: $localize`Search images ...`,
        },
        subheader: {
          objectList(data): FleioObjectsList<IBaseFleioObjectModel> {
            return data.images;
          },
          objectName: $localize`image`,
          objectNamePlural: $localize`images`
        },
        ordering: {
          default: {
            display: $localize`Created at`,
            field: 'created_at',
            direction: OrderingDirection.Descending,
          },
          options: [
            {display: $localize`Name`, field: 'name'},
            {display: $localize`Status`, field: 'status'},
            {display: $localize`Type`, field: 'type'},
            {display: $localize`Created at`, 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`Region`,
              field: 'region',
              type: FilterTypes.CustomModel,
              items: 'regions',
              itemsDisplayField: 'id'
            },
            {
              display: $localize`Status`,
              field: 'status',
              type: FilterTypes.Choices
            },
            {
              display: $localize`Hypervisor type`,
              field: 'hypervisor_type',
              type: FilterTypes.Choices
            },
            {
              display: $localize`Visibility`,
              field: 'visibility',
              type: FilterTypes.Choices,
            },
            {
              display: $localize`Protected`,
              field: 'protected',
              type: FilterTypes.Boolean
            },
            {
              display: $localize`Min disk`,
              field: 'min_disk',
              type: FilterTypes.Decimal
            },
            {
              display: $localize`Min ram`,
              field: 'min_ram',
              type: FilterTypes.Decimal
            }
          ]
        }
      } as IRouteConfig,
    },
    runGuardsAndResolvers: 'always'
  },
  {
    path: 'create',
    component: ImageCreateComponent,
    resolve: {
      createOptions: ImageCreateOptionsResolver,
    },
    data: {
      config: {
        getBreadCrumbDetail: () => $localize`Create image`,
      } as IRouteConfig,
    }
  },
  {
    path: ':id',
    component: ImageDetailsComponent,
    resolve: {
      image: ImageResolver,
      permissions: ImagePermissionsResolver,
    },
    data: {
      config: {
        getBreadCrumbDetail: (data) => data.image.name || data.image.id,
      } as IRouteConfig,
    },
    runGuardsAndResolvers: 'always',
  },
  {
    path: ':id/edit',
    component: ImageEditComponent,
    resolve: {
      image: ImageResolver,
      createOptions: ImageCreateOptionsResolver,
    },
    data: {
      config: {
        getBreadCrumbDetail: (data) => data.image.name || data.image.id,
      } as IRouteConfig,
    },
    runGuardsAndResolvers: 'always'
  },
];

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