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 { MessageTemplateListComponent } from './message-template-list/message-template-list.component';
import { MessageTemplatesListResolver } from '@fleio-api/message-templates/message-templates-list-resolver.service';
import { MessageTemplateResolver } from '@fleio-api/message-templates/message-template-resolver.service';
import { MessageTemplateEditComponent } from './message-template-edit/message-template-edit.component';
import { MessageTemplatesPermissionsResolver } from '@fleio-api/message-templates/message-template-permissions.resolver';
import { FilterTypes } from '@shared/ui-api/interfaces/route-config/filter-types';
import {
  MessageTemplateCreateComponent
} from './message-template-create/message-template-create.component';
import {
  MessageTemplateCreateOptionsResolver
} from '@fleio-api/message-templates/message-template-create-options-resolver.service';

const routes: Routes = [
  {
    path: '',
    component: MessageTemplateListComponent,
    resolve: {
      messageTemplates: MessageTemplatesListResolver,
    },
    canActivate: [AuthGuard],
    data: {
      config: {
        feature: 'settings.message_templates',
        search: {
          show: true,
          placeholder: $localize`Search message templates ...`,
        },
        subheader: {
          objectList(data): FleioObjectsList<IBaseFleioObjectModel> {
            return data.messageTemplates;
          },
          objectName: $localize`message template`,
          objectNamePlural: $localize`message templates`,
        },
        ordering: {
          default: {
            display: $localize`Name`,
            field: 'name',
            direction: OrderingDirection.Ascending,
          },
          options: [
            {
              display: $localize`Name`,
              field: 'name',
            },
            {
              display: $localize`Type`,
              field: 'type',
            },
            {
              display: $localize`Category`,
              field: 'category__name',
            }
          ]
        },
        filterConfig: {
          availableOptions: [
            {
              field: 'target',
              display: $localize`Target`,
              type: FilterTypes.Choices
            },
            {
              field: 'type',
              display: $localize`Type`,
              type: FilterTypes.Choices
            },
            {
              field: 'category',
              display: $localize`Category`,
              type: FilterTypes.CustomModel,
              items: 'notificationTemplatesCategories',
              itemsDisplayField: 'name',
              itemsWithoutRelatedRecord: false,
            },
          ]
        },
      } as IRouteConfig,
    },
    runGuardsAndResolvers: 'always'
  },
  {
    path: ':id/edit',
    component: MessageTemplateEditComponent,
    resolve: {
      messageTemplate: MessageTemplateResolver,
      createOptions: MessageTemplateCreateOptionsResolver,
      permissions: MessageTemplatesPermissionsResolver,
    },
    data: {
      config: {
        getBreadCrumbDetail: (data) => $localize`Edit message template ${data.messageTemplate[0].name}`,
      } as IRouteConfig,
    },
    runGuardsAndResolvers: 'always'
  },
  {
    path: 'create',
    component: MessageTemplateCreateComponent,
    resolve: {
      createOptions: MessageTemplateCreateOptionsResolver,
      permissions: MessageTemplatesPermissionsResolver,
    },
    data: {
      config: {
        getBreadCrumbDetail: (data) => $localize`Create message template`,
      } as IRouteConfig,
    },
    runGuardsAndResolvers: 'always'
  },
];

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