import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { AuthGuard } from '@shared/auth/auth.guard';
import { OrderingDirection } from '@shared/ui-api/interfaces/route-config/ordering-directions';
import { FleioObjectsList } from '@fleio-api/fleio-objects-list';
import { IBaseFleioObjectModel } from '@fleio-api/base-model/base-fleio-object.model';
import { IRouteConfig } from '@shared/ui-api/interfaces/route-config/route-config';
import { TicketsListComponent } from './tickets-list/tickets-list.component';
import { TicketsListResolver } from '@fleio-api/plugins/tickets/tickets-list.resolver';
import { TicketsOpenNewComponent } from './tickets-open-new/tickets-open-new.component';
import { TicketsCreateOptionsResolver } from '@fleio-api/plugins/tickets/tickets-create-options.resolver';
import { TicketDetailsComponent } from './ticket-details/ticket-details.component';
import { TicketResolver } from '@fleio-api/plugins/tickets/ticket.resolver';
import { TicketCreateOptionsResolver } from '@fleio-api/plugins/tickets/ticket-create-options.resolver';
import { FilterTypes } from '@shared/ui-api/interfaces/route-config/filter-types';
import { TicketEditComponent } from './ticket-edit/ticket-edit.component';
import { UserSignaturesComponent } from './user-signatures/user-signatures.component';
import { TicketUserSignaturesResolver } from '@fleio-api/plugins/tickets/ticket-user-signatures.resolver';
import {
  TicketNotificationsSettingsComponent
} from './ticket-notifications-settings/ticket-notifications-settings.component';
import {
  TicketStaffNotificationsSettingsResolver
} from '@fleio-api/plugins/tickets/ticket-staff-notifications-settings.resolver';

const routes: Routes = [
  {
    path: '',
    component: TicketsListComponent,
    resolve: {
      tickets: TicketsListResolver,
    },
    canActivate: [AuthGuard],
    data: {
      config: {
        feature: 'plugins.tickets',
        search: {
          show: true,
          placeholder: $localize`Search tickets ...`,
        },
        ordering: {
          default: {
            field: 'last_reply_at',
            display: $localize`Last reply`,
            direction: OrderingDirection.Descending
          },
          options: [
            {
              field: 'last_reply_at',
              display: $localize`Last reply`
            },
            {
              field: 'assigned_to',
              display: $localize`Assigned to`
            },
            {
              field: 'created_at',
              display: $localize`Created at`
            },
            {
              field: 'created_by',
              display: $localize`Created by`
            },
            {
              field: 'status',
              display: $localize`Status`
            },
            {
              field: 'title',
              display: $localize`Title`
            },
            {
              field: 'department',
              display: $localize`Department`
            },
            {
              field: 'priority',
              display: $localize`Priority`
            },
          ],
        },
        subheader: {
          objectName: $localize`ticket`,
          objectNamePlural: $localize`tickets`,
          objectList(data): FleioObjectsList<IBaseFleioObjectModel> {
            return data.tickets;
          }
        },
        getBreadCrumbDetail: () => $localize`Tickets`,
        filterConfig: {
          availableOptions: [
            {
              display: $localize`Assigned to`,
              field: 'assigned_to',
              type: FilterTypes.CustomModel,
              items: 'users',
              itemsDisplayField: 'display',
              itemsFilter: {
                is_staff: 'True'
              }
            },
            {
              display: $localize`Created at`,
              field: 'created_at',
              type: FilterTypes.Date
            },
            {
              display: $localize`Status`,
              field: 'status',
              type: FilterTypes.Choices
            },
            {
              display: $localize`Priority`,
              field: 'priority',
              type: FilterTypes.Choices
            },
            {
              display: $localize`Client`,
              field: 'client',
              type: FilterTypes.CustomModel,
              items: 'clients',
              itemsDisplayField: 'display',
            },
            {
              display: $localize`Department`,
              field: 'department',
              type: FilterTypes.CustomModel,
              items: 'departments',
              itemsDisplayField: 'name',
            },
            {
              display: $localize`Has attachments`,
              field: 'has_attachments',
              type: FilterTypes.Boolean,
            },
          ],
          defaultFilters: [
            {
              field: 'status__ne',
              value: 'merged'
            }
          ]
        },
      } as IRouteConfig,
    },
    runGuardsAndResolvers: 'always'
  },
  {
    path: 'open-new-ticket',
    component: TicketsOpenNewComponent,
    resolve: {
      createOptions: TicketsCreateOptionsResolver,
    },
    data: {
      config: {
        getBreadCrumbDetail: () => $localize`Open a new ticket`,
      } as IRouteConfig,
    },
  },
  {
    path: 'open-new-ticket/:forclient',
    component: TicketsOpenNewComponent,
    resolve: {
      createOptions: TicketsCreateOptionsResolver,
    },
    data: {
      config: {
        getBreadCrumbDetail: () => $localize`Open a new ticket`,
      } as IRouteConfig,
    },
  },
  {
    path: 'departments',
    loadChildren: () => import('./departments/departments-routing.module').then(mod => mod.DepartmentsRoutingModule),
    canActivate: [AuthGuard],
  },
  {
    path: 'predefined-replies',
    loadChildren: () => import('./predefined-replies/predefined-replies-routing.module').then(
      mod => mod.PredefinedRepliesRoutingModule
    ),
    canActivate: [AuthGuard],
  },
  {
    path: 'user-signatures',
    component: UserSignaturesComponent,
    resolve: {
      signatures: TicketUserSignaturesResolver,
    },
    data: {
      config: {
        getBreadCrumbDetail: () => $localize`Edit signatures`,
      } as IRouteConfig,
    },
    runGuardsAndResolvers: 'always'
  },
  {
    path: 'notifications-settings',
    component: TicketNotificationsSettingsComponent,
    resolve: {
      settings: TicketStaffNotificationsSettingsResolver,
    },
    data: {
      config: {
        getBreadCrumbDetail: () => $localize`Edit ticket notifications settings`,
      } as IRouteConfig,
    },
    runGuardsAndResolvers: 'always'
  },
  {
    path: ':id',
    component: TicketDetailsComponent,
    resolve: {
      ticket: TicketResolver,
      createOptions: TicketCreateOptionsResolver,
    },
    data: {
      config: {
        getBreadCrumbDetail: (data) => `#${data.ticket.id}`,
        removeWidthLimit: true,
      } as IRouteConfig,
    },
    runGuardsAndResolvers: 'always'
  },
  {
    path: ':id/edit',
    component: TicketEditComponent,
    resolve: {
      ticket: TicketResolver,
      createOptions: TicketCreateOptionsResolver,
    },
    data: {
      config: {
        getBreadCrumbDetail: () => $localize`Edit ticket`,
      } as IRouteConfig,
    },
    runGuardsAndResolvers: 'always'
  }
];

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