import { inject } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivateFn, Router } from '@angular/router';
import { DOSSIER_TEMPLATE_ID } from '@red/domain';
import { DashboardStatsService } from '@services/dossier-templates/dashboard-stats.service';
import { DossierTemplatesService } from '@services/dossier-templates/dossier-templates.service';
import { DossierTemplateStatsService } from '@services/entity-services/dossier-template-stats.service';
import { NGXLogger } from 'ngx-logger';
import { firstValueFrom } from 'rxjs';
import { UserPreferenceService } from '@users/user-preference.service';
import { DictionaryService } from '@services/entity-services/dictionary.service';
import { DefaultColorsService } from '@services/entity-services/default-colors.service';
import { WatermarkService } from '@services/entity-services/watermark.service';
import { FileAttributesService } from '@services/entity-services/file-attributes.service';
import { getConfig } from '@iqser/common-ui';

export function templateExistsWhenEnteringAdmin(): CanActivateFn {
    return async function (route: ActivatedRouteSnapshot): Promise<boolean> {
        const dossierTemplateId: string = route.paramMap.get(DOSSIER_TEMPLATE_ID);
        const fileAttributesService = inject(FileAttributesService);
        const dictionaryService = inject(DictionaryService);
        const defaultColorsService = inject(DefaultColorsService);
        const watermarksService = inject(WatermarkService);
        const router = inject(Router);
        const isDocumine = getConfig().IS_DOCUMINE;

        const dossierTemplate = inject(DossierTemplateStatsService).get(dossierTemplateId);
        await firstValueFrom(fileAttributesService.loadFileAttributesConfig(dossierTemplateId));
        await firstValueFrom(dictionaryService.loadDictionaryDataForDossierTemplate(dossierTemplateId));
        await firstValueFrom(defaultColorsService.loadForDossierTemplate(dossierTemplateId));
        if (!isDocumine) {
            await firstValueFrom(watermarksService.loadForDossierTemplate(dossierTemplateId));
        }
        if (!dossierTemplate) {
            await router.navigate(['main', 'admin', 'dossier-templates']);
            return false;
        }
        return true;
    };
}

export function templateExistsWhenEnteringDossierList(): CanActivateFn {
    return async function (route: ActivatedRouteSnapshot) {
        const dossierTemplateId: string = route.paramMap.get(DOSSIER_TEMPLATE_ID);
        const dashboardStatsService = inject(DashboardStatsService);
        const dossierTemplatesService = inject(DossierTemplatesService);
        const logger = inject(NGXLogger);
        const router = inject(Router);
        const userPreferencesService = inject(UserPreferenceService);
        const fileAttributesService = inject(FileAttributesService);
        const dictionaryService = inject(DictionaryService);
        const defaultColorsService = inject(DefaultColorsService);
        const watermarksService = inject(WatermarkService);
        const isDocumine = getConfig().IS_DOCUMINE;

        await firstValueFrom(dashboardStatsService.loadForTemplate(dossierTemplateId));
        await firstValueFrom(dossierTemplatesService.loadDossierTemplate(dossierTemplateId));
        const dossierTemplateStats = dashboardStatsService.find(dossierTemplateId);
        if (!dossierTemplateStats || dossierTemplateStats.isEmpty) {
            logger.warn(`[ROUTES] Dossier template ${dossierTemplateId} not found, redirecting to main`);
            await userPreferencesService.saveLastDossierTemplate(null);
            await router.navigate(['main']);
            return false;
        }
        await firstValueFrom(fileAttributesService.loadFileAttributesConfig(dossierTemplateId));
        await firstValueFrom(dictionaryService.loadDictionaryDataForDossierTemplate(dossierTemplateId));
        await firstValueFrom(defaultColorsService.loadForDossierTemplate(dossierTemplateId));
        if (!isDocumine) {
            await firstValueFrom(watermarksService.loadForDossierTemplate(dossierTemplateId));
        }
        return true;
    };
}
