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 { DomainEditComponent } from './domain-edit/domain-edit.component';
import { DomainResolver } from '@fleio-api/plugins/domains/domain.resolver';
import { DomainCreateOptionsResolver } from '@fleio-api/plugins/domains/domain-create-options.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 { ServiceDetailsComponent } from '../../billing/services/service-details/service-details.component';
import { ServiceResolver } from '@fleio-api/billing/services/service.resolver';
import { ServicePermissionsResolver } from '@fleio-api/billing/services/service-permissions.resolver';
import { DomainRegistrarListResolver } from '@fleio-api/plugins/domains/domain-registrar-list.resolver';

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: '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: 'register-domain/:client/:domain_name',
    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: 'transfer-domain/:client/:domain_name',
    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: 'tlds',
    loadChildren: () => import('./tlds/tlds.module').then(mod => mod.TldsModule),
    canActivate: [AuthGuard],
  },
  {
    path: 'registrars',
    loadChildren: () => import('./registrars/registrars.module').then(mod => mod.RegistrarsModule),
    canActivate: [AuthGuard],
  },
  {
    path: ':id',
    component: ServiceDetailsComponent,
    resolve: {
      service: ServiceResolver,
      permissions: ServicePermissionsResolver,
      registrars: DomainRegistrarListResolver,
    },
    data: {
      config: {
        getBreadCrumbDetail: (data) => $localize`Domain ${data.service.resources.service_resources.name}`,
      } as IRouteConfig,
    },
    runGuardsAndResolvers: 'always'
  },
  {
    path: ':id/edit',
    component: DomainEditComponent,
    resolve: {
      domain: DomainResolver,
      createOptions: DomainCreateOptionsResolver,
    },
    data: {
      config: {
        getBreadCrumbDetail: (data) => $localize`Edit domain ${data.domain.name}`,
      } as IRouteConfig,
    }
  },
];

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