import { NgModule } from '@angular/core';
import { 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 { ClientListResolver } from '@fleio-api/client-user/client/client-list.resolver';
import { ClientResolver } from '@fleio-api/client-user/client/client.resolver';
import { ClientCreateOptionsResolver } from '@fleio-api/client-user/client/client-create-options.resolver';
import { FilterTypes } from '@shared/ui-api/interfaces/route-config/filter-types';
import { OrderingDirection } from '@shared/ui-api/interfaces/route-config/ordering-directions';
import { AuthGuard } from '@shared/auth/auth.guard';
import { ClientListComponent } from './client-list/client-list.component';
import { ClientCreateComponent } from './client-create/client-create.component';
import { ClientDetailsComponent } from './client-details/client-details.component';
import { ClientEditComponent } from './client-edit/client-edit.component';
import { ClientPermissionsResolver } from '@fleio-api/client-user/client/client-permissions.resolver';

const routes: Routes = [
  {
    path: '',
    component: ClientListComponent,
    resolve: {
      clients: ClientListResolver
    },
    canActivate: [AuthGuard],
    data: {
      config: {
        feature: 'clients&users.clients',
        search: {
          show: true,
          placeholder: $localize`Search clients ...`,
        },
        subheader: {
          objectName: $localize`client`,
          objectNamePlural: $localize`clients`,
          objectList(data): FleioObjectsList<IBaseFleioObjectModel> {
            return data.clients;
          }
        },
        ordering: {
          default: {
            field: 'date_created',
            direction: OrderingDirection.Descending,
            display: $localize`Date created`,
          },
          options: [
            {display: $localize`Company`, field: 'company'},
            {display: $localize`Country`, field: 'country'},
            {display: $localize`State`, field: 'state'},
            {display: $localize`Date created`, field: 'date_created'},
            {display: $localize`Configuration name`, field: 'configuration_name'},
            {display: $localize`Group name`, field: 'group_name'},
            {display: $localize`Out of credit date`, field: 'outofcredit_datetime'},
          ]
        },
        filterConfig: {
          availableOptions: [
            {
              field: 'date_created',
              display: $localize`Date created`,
              type: FilterTypes.Date
            },
            {
              display: $localize`Configuration`,
              field: 'configuration',
              type: FilterTypes.CustomModel,
              items: 'configurations',
              itemsDisplayField: 'name',
              feature: 'settings.configurations'
            },
            {
              field: 'status',
              display: $localize`Status`,
              type: FilterTypes.Choices
            },
            {
              display: $localize`Tax exempt`,
              field: 'tax_exempt',
              type: FilterTypes.Boolean
            },
            {
              display: $localize`Client group`,
              field: 'groups',
              type: FilterTypes.CustomModel,
              items: 'clientgroups',
              itemsDisplayField: 'name',
            },
            {
              display: $localize`Has instances in region`,
              field: 'services__openstack_project__instance__region',
              type: FilterTypes.CustomModel,
              items: 'regions',
              itemsDisplayField: 'id',
              feature: 'openstack'
            },
            {
              display: $localize`Has instances with flavor`,
              field: 'services__openstack_project__instance__flavor',
              type: FilterTypes.CustomModel,
              items: 'flavors',
              itemsDisplayField: 'display_name',
              feature: 'openstack'
            },
            {
              display: $localize`Has instances on hypervisor`,
              field: 'services__openstack_project__instance__host_name',
              type: FilterTypes.WildCard,
              feature: 'openstack'
            },
            {
              field: 'has_billing_agreement',
              display: $localize`Has billing agreement`,
              type: FilterTypes.Boolean
            },
            {
              field: 'has_services',
              display: $localize`Has services`,
              type: FilterTypes.Boolean
            },
            {
              field: 'has_instances',
              display: $localize`Has instances`,
              type: FilterTypes.Boolean,
              feature: 'openstack.instances'
            },
            {
              field: 'services__product__product_type',
              display: $localize`Has service of type`,
              type: FilterTypes.Choices
            },
            {
              field: 'services__product',
              display: $localize`Has service for product`,
              type: FilterTypes.CustomModel,
              items: 'products',
              itemsDisplayField: 'name'
            },
            {
              field: 'country',
              display: $localize`Country`,
              type: FilterTypes.Choices
            },
          ]
        },
      } as IRouteConfig,
    },
    runGuardsAndResolvers: 'always'
  },
  {
    path: 'create',
    component: ClientCreateComponent,
    resolve: {
      createOptions: ClientCreateOptionsResolver
    },
    data: {
      config: {
        getBreadCrumbDetail: () => $localize`Create client`,
      } as IRouteConfig,
    }
  },
  {
    path: ':id',
    component: ClientDetailsComponent,
    resolve: {
      client: ClientResolver,
      permissions: ClientPermissionsResolver,
    },
    data: {
      config: {
        getBreadCrumbDetail: (data) => data.client.long_name,
      } as IRouteConfig,
    },
    runGuardsAndResolvers: 'always',
  },
  {
    path: ':id/edit',
    component: ClientEditComponent,
    resolve: {
      client: ClientResolver,
      createOptions: ClientCreateOptionsResolver
    },
    data: {
      config: {
        getBreadCrumbDetail: (data) => $localize`Edit ${data.client.long_name}`,
      } as IRouteConfig,
    }
  },
];

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