import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { AuthGuard } from '@shared/auth/auth.guard';
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 { DomainListComponent } from './domain-list/domain-list.component';
import { DomainListResolver } from '@fleio-api/plugins/domains/domain-list.resolver';
import { FilterTypes } from '@shared/ui-api/interfaces/route-config/filter-types';
import { DomainRegisterComponent } from './domain-register/domain-register.component';
import { DomainTransferComponent } from './domain-transfer/domain-transfer.component';
import { OrderingDirection } from '@shared/ui-api/interfaces/route-config/ordering-directions';
import { DomainContactCreateOptionsResolver } from '@fleio-api/plugins/domains/domain-contact-create-options.resolver';
import { ClientCreateOptionsResolver } from '@fleio-api/client-user/client/client-create-options.resolver';
import { ServiceResolver } from '@fleio-api/billing/services/service.resolver';
import { ServicePermissionsResolver } from '@fleio-api/billing/services/service-permissions.resolver';
import { ServiceDetailsComponent } from '../../billing/services/service-details/service-details.component';

const routes: Routes = [
  {
    path: '',
    component: DomainListComponent,
    resolve: {
      domains: DomainListResolver,
    },
    canActivate: [AuthGuard],
    data: {
      config: {
        feature: 'plugins.domains',
        search: {
          show: true,
          placeholder: $localize`Search domains ...`,
        },
        subheader: {
          objectList(data): FleioObjectsList<IBaseFleioObjectModel> {
            return data.domains;
          },
          objectName: $localize`domain`,
          objectNamePlural: $localize`domains`,
        },
        ordering: {
          default: {
            display: $localize`Created at`,
            field: 'created_at',
            direction: OrderingDirection.Descending,
          },
          options: [
            {
              display: $localize`Created at`,
              field: 'created_at',
            },
            {
              display: $localize`Status`,
              field: $localize`status`,
            },
            {
              display: $localize`Name`,
              field: 'name',
            },
          ]
        },
        filterConfig: {
          availableOptions: [
            {
              field: 'created_at',
              display: $localize`Created at`,
              type: FilterTypes.Date,
            },
            {
              field: 'registration_date',
              display: $localize`Registration date`,
              type: FilterTypes.Date,
            },
            {
              field: 'expiry_date',
              display: $localize`Expiry date`,
              type: FilterTypes.Date,
            },
            {
              field: 'registration_period',
              display: $localize`Registration period`,
              type: FilterTypes.Decimal,
            },
            {
              field: 'status',
              display: $localize`Status`,
              type: FilterTypes.Choices
            },
            {
              display: $localize`Top level domain`,
              field: 'tld',
              type: FilterTypes.CustomModel,
              items: 'tlds',
              itemsDisplayField: 'name'
            },
          ],
        }
      } as IRouteConfig,
    },
    runGuardsAndResolvers: 'always'
  },
  {
    path: 'register-domain',
    component: DomainRegisterComponent,
    resolve: {
      contactCreateOptions: DomainContactCreateOptionsResolver,
      clientCreateOptions: ClientCreateOptionsResolver,
    },
    data: {
      config: {
        getBreadCrumbDetail: () => $localize`Register domain`,
      } as IRouteConfig,
    }
  },
  {
    path: 'transfer-domain',
    component: DomainTransferComponent,
    resolve: {},
    data: {
      config: {
        getBreadCrumbDetail: () => $localize`Transfer domain`,
      } as IRouteConfig,
    }
  },
  {
    path: 'contacts',
    loadChildren: () => import('./contacts/contacts.module').then(mod => mod.ContactsModule),
    canActivate: [AuthGuard],
  },
  {
    path: ':id',
    component: ServiceDetailsComponent,
    resolve: {
      service: ServiceResolver,
      permissions: ServicePermissionsResolver,
    },
    data: {
      config: {
        getBreadCrumbDetail: (data) => $localize`Domain ${data.service.resources.service_resources.name}`,
      } as IRouteConfig,
    },
    runGuardsAndResolvers: 'always'
  },
];

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