import { NgModule } from '@angular/core';
import { CompositeRouteGuard, IqserPermissionsGuard, IqserRoutes } from '@iqser/common-ui';
import { RedRoleGuard } from '@users/red-role.guard';
import { EntitiesListingScreenComponent } from './screens/entities-listing/entities-listing-screen.component';
import { PendingChangesGuard } from '@guards/can-deactivate.guard';
import { DefaultColorsScreenComponent } from './screens/default-colors/default-colors-screen.component';
import { UserListingScreenComponent } from './screens/user-listing/user-listing-screen.component';
import { DigitalSignatureScreenComponent } from './screens/digital-signature/digital-signature-screen.component';
import { AuditScreenComponent } from './screens/audit/audit-screen.component';
import { RouterModule } from '@angular/router';
import { GeneralConfigScreenComponent } from './screens/general-config/general-config-screen.component';
import { BaseAdminScreenComponent } from './base-admin-screen/base-admin-screen.component';
import { BaseDossierTemplateScreenComponent } from './base-dossier-templates-screen/base-dossier-template-screen.component';
import { DossierTemplatesGuard } from '@guards/dossier-templates.guard';
import { DOSSIER_TEMPLATE_ID, ENTITY_TYPE } from '@red/domain';
import { templateExistsWhenEnteringAdmin } from '@guards/dossier-template-exists.guard';
import { entityExistsGuard } from '@guards/entity-exists-guard.service';
import { BaseEntityScreenComponent } from './base-entity-screen/base-entity-screen.component';
import { PermissionsGuard } from '@guards/permissions-guard';
import { Roles } from '@users/roles';
import { IqserAuthGuard } from '@iqser/common-ui/lib/users';

const dossierTemplateIdRoutes: IqserRoutes = [
    {
        path: 'info',
        component: BaseDossierTemplateScreenComponent,
        loadChildren: () => import('./screens/info/dossier-template-info.module').then(m => m.DossierTemplateInfoModule),
    },
    {
        path: 'entities',
        children: [
            {
                path: '',
                component: EntitiesListingScreenComponent,
                canActivate: [CompositeRouteGuard],
                data: {
                    routeGuards: [IqserAuthGuard, RedRoleGuard],
                },
            },
            {
                path: `:${ENTITY_TYPE}`,
                component: BaseEntityScreenComponent,
                canActivate: [CompositeRouteGuard, entityExistsGuard()],
                loadChildren: () => import('./screens/entities/entities.module').then(m => m.EntitiesModule),
                data: {
                    routeGuards: [IqserAuthGuard, RedRoleGuard],
                },
            },
        ],
    },
    {
        path: 'entity-rules',
        component: BaseDossierTemplateScreenComponent,
        canActivate: [CompositeRouteGuard, IqserPermissionsGuard],
        data: {
            routeGuards: [IqserAuthGuard, RedRoleGuard],
            permissions: {
                allow: [Roles.rules.read],
                redirectTo: 'info',
            },
            type: 'ENTITY',
        },
        loadChildren: () => import('./screens/rules/rules.module').then(m => m.RulesModule),
    },
    {
        path: 'component-rules',
        component: BaseDossierTemplateScreenComponent,
        canActivate: [CompositeRouteGuard, IqserPermissionsGuard],
        data: {
            routeGuards: [IqserAuthGuard, RedRoleGuard],
            permissions: {
                allow: [Roles.rules.read],
                redirectTo: 'info',
            },
            type: 'COMPONENT',
        },
        loadChildren: () => import('./screens/rules/rules.module').then(m => m.RulesModule),
    },
    {
        path: 'file-attributes',
        component: BaseDossierTemplateScreenComponent,
        loadChildren: () =>
            import('./screens/file-attributes-listing/file-attributes-listing.module').then(m => m.FileAttributesListingModule),
        canActivate: [CompositeRouteGuard],
        data: {
            routeGuards: [IqserAuthGuard, RedRoleGuard],
        },
    },
    {
        path: 'watermarks',
        component: BaseDossierTemplateScreenComponent,
        loadChildren: () => import('./screens/watermark/watermark.module').then(m => m.WatermarkModule),
    },
    {
        path: 'reports',
        component: BaseDossierTemplateScreenComponent,
        loadChildren: () => import('./screens/reports/reports.module').then(m => m.ReportsModule),
        canActivate: [IqserAuthGuard, IqserPermissionsGuard],
        data: {
            permissions: {
                allow: [Roles.reportTemplates.read],
                redirectTo: '/auth-error',
            },
        },
    },
    {
        path: 'dossier-attributes',
        component: BaseDossierTemplateScreenComponent,
        loadChildren: () =>
            import('./screens/dossier-attributes-listing/dossier-attributes-listing.module').then(m => m.DossierAttributesListingModule),
        canActivate: [CompositeRouteGuard],
        data: {
            routeGuards: [IqserAuthGuard, RedRoleGuard],
        },
    },
    {
        path: 'dossier-states',
        component: BaseDossierTemplateScreenComponent,
        loadChildren: () =>
            import('./screens/dossier-states-listing/dossier-states-listing.module').then(m => m.DossierStatesListingModule),
        canActivate: [CompositeRouteGuard],
        data: {
            routeGuards: [IqserAuthGuard, RedRoleGuard],
        },
    },
    {
        path: 'default-colors',
        component: DefaultColorsScreenComponent,
        canActivate: [CompositeRouteGuard],
        data: {
            routeGuards: [IqserAuthGuard, RedRoleGuard],
        },
    },
    {
        path: 'justifications',
        component: BaseDossierTemplateScreenComponent,
        loadChildren: () => import('./screens/justifications/justifications.module').then(m => m.JustificationsModule),
        canActivate: [IqserPermissionsGuard],
        data: {
            permissions: {
                allow: [Roles.legalBasis.read],
                redirectTo: '/auth-error',
            },
        },
    },
    { path: '', redirectTo: 'info', pathMatch: 'full' },
];

const dossierTemplatesRoutes: IqserRoutes = [
    {
        path: '',
        component: BaseAdminScreenComponent,
        canActivate: [IqserAuthGuard],
        loadChildren: () =>
            import('./screens/dossier-templates-listing/dossier-templates-listing.module').then(m => m.DossierTemplatesListingModule),
    },
    {
        path: `:${DOSSIER_TEMPLATE_ID}`,
        children: dossierTemplateIdRoutes,
        canActivate: [templateExistsWhenEnteringAdmin()],
    },
];

const routes: IqserRoutes = [
    { path: '', redirectTo: 'dossier-templates', pathMatch: 'full' },
    {
        path: 'dossier-templates',
        children: dossierTemplatesRoutes,
        canActivate: [CompositeRouteGuard, IqserPermissionsGuard],
        data: {
            routeGuards: [IqserAuthGuard, RedRoleGuard, DossierTemplatesGuard],
            requiredRoles: ['RED_MANAGER', 'RED_ADMIN'],
            permissions: {
                allow: [Roles.templates.read],
                redirectTo: '/',
            },
        },
    },
    {
        path: 'users',
        component: BaseAdminScreenComponent,
        children: [
            {
                path: '',
                component: UserListingScreenComponent,
            },
        ],
        canActivate: [CompositeRouteGuard, IqserPermissionsGuard],
        data: {
            routeGuards: [IqserAuthGuard, RedRoleGuard],
            permissions: {
                allow: [Roles.users.read, 'RED_USER_ADMIN'],
                redirectTo: '/',
            },
        },
    },
    {
        path: 'dossier-permissions',
        component: BaseAdminScreenComponent,
        canActivate: [CompositeRouteGuard, IqserPermissionsGuard],
        data: {
            routeGuards: [IqserAuthGuard, RedRoleGuard, PermissionsGuard],
            permissionsObject: 'Dossier',
            permissions: {
                allow: [Roles.manageAclPermissions, 'RED_ADMIN'],
                redirectTo: '/',
            },
        },
        loadChildren: () => import('./screens/permissions/permissions.module').then(m => m.PermissionsModule),
    },
    {
        path: 'license-info',
        component: BaseAdminScreenComponent,
        canActivate: [IqserAuthGuard, IqserPermissionsGuard, RedRoleGuard],
        data: {
            permissions: {
                allow: [Roles.license.readReport, 'RED_ADMIN'],
                redirectTo: '/',
            },
        },
        loadChildren: () => import('./screens/license/license.module').then(m => m.LicenseModule),
    },
    {
        path: 'digital-signature',
        component: BaseAdminScreenComponent,
        children: [
            {
                path: '',
                component: DigitalSignatureScreenComponent,
            },
        ],
        canActivate: [IqserAuthGuard, IqserPermissionsGuard, RedRoleGuard],
        data: {
            permissions: {
                allow: [Roles.digitalSignature.read, 'RED_ADMIN'],
                redirectTo: '/',
            },
        },
    },
    {
        path: 'audit',
        component: BaseAdminScreenComponent,
        children: [
            {
                path: '',
                component: AuditScreenComponent,
            },
        ],
        canActivate: [IqserAuthGuard, IqserPermissionsGuard, RedRoleGuard],
        data: {
            permissions: {
                allow: [Roles.searchAudit, 'RED_ADMIN'],
                redirectTo: '/',
            },
        },
    },
    {
        path: 'general-config',
        component: BaseAdminScreenComponent,
        children: [
            {
                path: '',
                component: GeneralConfigScreenComponent,
            },
        ],
        canActivate: [CompositeRouteGuard, IqserPermissionsGuard],
        canDeactivate: [PendingChangesGuard],
        data: {
            routeGuards: [IqserAuthGuard, RedRoleGuard],
            permissions: {
                allow: [Roles.generalConfiguration.read, Roles.smtp.read, 'RED_ADMIN'],
                redirectTo: '/',
            },
        },
    },
];

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