import { NgForOf, NgIf } from '@angular/common';
import { Component, HostBinding, Input, OnInit } from '@angular/core';
import { ActivatedRoute, RouterLink, RouterLinkActive } from '@angular/router';
import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker';
import { getConfig, IqserHelpModeModule, IqserPermissionsService, isIqserDevMode } from '@iqser/common-ui';
import { SideNavComponent } from '@iqser/common-ui/lib/shared';
import { TenantPipe } from '@iqser/common-ui/lib/tenants';
import { getCurrentUser } from '@iqser/common-ui/lib/users';
import { TranslateModule } from '@ngx-translate/core';
import { AdminSideNavType, AdminSideNavTypes, AppConfig, DOSSIER_TEMPLATE_ID, ENTITY_TYPE, User, WATERMARK_ID } from '@red/domain';
import { adminSideNavTranslations } from '@translations/admin-side-nav-translations';
import { Roles } from '@users/roles';

interface NavItem {
    readonly label: string;
    readonly screen: string;
    readonly show?: boolean;
    readonly helpModeKey?: string;
}

@Component({
    selector: 'redaction-admin-side-nav [type]',
    templateUrl: './admin-side-nav.component.html',
    styleUrls: ['./admin-side-nav.component.scss'],
    standalone: true,
    imports: [TranslateModule, NgIf, IqserHelpModeModule, RouterLink, RouterLinkActive, NgForOf, SideNavComponent, TenantPipe],
})
export class AdminSideNavComponent implements OnInit {
    readonly #config = getConfig<AppConfig>();
    readonly isIqserDevMode = isIqserDevMode();
    @Input() type: AdminSideNavType;
    @Input() disabledItems: string[] = [];
    readonly translations = adminSideNavTranslations;
    readonly currentUser = getCurrentUser<User>();
    readonly roles = Roles;
    prefix: string;
    readonly isDocumine = this.#config.IS_DOCUMINE;
    readonly canAccessRulesInDocumine = this.isDocumine && !this.#config.RULE_EDITOR_DEV_ONLY;
    readonly items: { readonly [key in AdminSideNavType]: NavItem[] } = {
        settings: [
            {
                screen: 'dossier-templates',
                label: _('admin-side-nav.dossier-templates'),
                show: (this.currentUser.isManager || this.currentUser.isAdmin) && this._permissionsService.has(Roles.templates.read),
                helpModeKey: 'dossier_templates',
            },
            {
                screen: 'digital-signature',
                label: _('admin-side-nav.digital-signature'),
                show: this.currentUser.isAdmin && this._permissionsService.has(Roles.digitalSignature.read) && !this.isDocumine,
                helpModeKey: 'digital_signature',
            },
            {
                screen: 'license-info',
                label: _('admin-side-nav.license-information'),
                show: this.currentUser.isAdmin && this._permissionsService.has(Roles.license.readReport),
                helpModeKey: 'license_information',
            },
            {
                screen: 'audit',
                label: _('admin-side-nav.audit'),
                show: this.currentUser.isAdmin && this._permissionsService.has(Roles.searchAudit),
                helpModeKey: 'audit',
            },
            {
                screen: 'users',
                label: _('admin-side-nav.user-management'),
                show: this.currentUser.isUserAdmin && this._permissionsService.has(Roles.users.read),
                helpModeKey: 'user_management',
            },
            {
                screen: 'dossier-permissions',
                label: _('dossier-permissions'),
                show: this.currentUser.isAdmin && this._permissionsService.has(Roles.manageAclPermissions),
                helpModeKey: 'dossier_permissions',
            },
            {
                screen: 'general-config',
                label: _('admin-side-nav.configurations'),
                show: this.currentUser.isAdmin && this._permissionsService.has([Roles.generalConfiguration.read, Roles.smtp.read]),
                helpModeKey: 'configurations',
            },
        ],
        dossierTemplates: [
            {
                screen: 'info',
                label: _('admin-side-nav.dossier-template-info'),
                helpModeKey: this.currentUser.isAdmin ? 'dossier_templates_info' : 'user_dossier_template_info',
                show: true,
            },
            {
                screen: 'entities',
                label: _('admin-side-nav.entities'),
                helpModeKey: this.currentUser.isAdmin ? 'entities' : 'user_dossier_template_entities',
                show: true,
            },
            {
                screen: 'entity-rules',
                label: _('admin-side-nav.entity-rule-editor'),
                show: (this.isIqserDevMode || this.canAccessRulesInDocumine) && this._permissionsService.has(Roles.rules.read),
            },
            {
                screen: 'component-rules',
                label: _('admin-side-nav.component-rule-editor'),
                show: this.isDocumine && (this.isIqserDevMode || this.canAccessRulesInDocumine) && this._permissionsService.has(Roles.rules.read),
            },
            {
                screen: 'default-colors',
                label: _('admin-side-nav.default-colors'),
                helpModeKey: 'default_colors',
                show: true,
            },
            {
                screen: 'watermarks',
                label: _('admin-side-nav.watermarks'),
                helpModeKey: 'watermarks',
                show: this._permissionsService.has(Roles.watermarks.read) && !this.isDocumine,
            },
            {
                screen: 'file-attributes',
                label: _('admin-side-nav.file-attributes'),
                helpModeKey: 'file_attributes',
                show: true,
            },
            {
                screen: 'dossier-attributes',
                label: _('admin-side-nav.dossier-attributes'),
                helpModeKey: 'dossier_attributes',
                show: true,
            },
            {
                screen: 'dossier-states',
                label: _('admin-side-nav.dossier-states'),
                helpModeKey: this.currentUser.isAdmin ? 'dossier_states' : 'user_dossier_template_dossier_states',
                show: true,
            },
            {
                screen: 'reports',
                label: _('admin-side-nav.reports'),
                show: this._permissionsService.has([Roles.reportTemplates.read]),
                helpModeKey: this.currentUser.isAdmin ? 'admin_reports' : 'user_dossier_template_reports',
            },
            {
                screen: 'justifications',
                label: _('admin-side-nav.justifications'),
                show: this._permissionsService.has([Roles.legalBasis.read]) && !this.isDocumine,
                helpModeKey: 'justifications',
            },
        ],
        entities: [
            {
                screen: 'info',
                label: _('admin-side-nav.entity-info'),
                show: true,
            },
            {
                screen: 'dictionary',
                label: _('admin-side-nav.dictionary'),
                helpModeKey: 'dictionary_entity',
                show: true,
            },
            {
                screen: 'false-positive',
                label: _('admin-side-nav.false-positive'),
                helpModeKey: 'false_recommendations_entity',
                show: true,
            },
            {
                screen: 'false-recommendations',
                label: _('admin-side-nav.false-recommendations'),
                helpModeKey: 'false_recommendations_entity',
                show: true,
            },
        ],
    };

    constructor(
        private readonly _permissionsService: IqserPermissionsService,
        private readonly _route: ActivatedRoute,
    ) {}

    @HostBinding('class.smaller') get isSmaller(): boolean {
        return [AdminSideNavTypes.dossierTemplates, AdminSideNavTypes.entities].includes(this.type);
    }

    ngOnInit(): void {
        const dossierTemplate = this._route.snapshot.paramMap.get(DOSSIER_TEMPLATE_ID);
        const watermark = this._route.snapshot.paramMap.get(WATERMARK_ID);
        const entity = this._route.snapshot.paramMap.get(ENTITY_TYPE);
        if (!dossierTemplate) {
            this.prefix = '/main/admin/';
        } else {
            this.prefix = `/main/admin/dossier-templates/${dossierTemplate}/`;
            if (entity) {
                this.prefix = this.prefix + `entities/${entity}/`;
            }
            if (watermark) {
                this.prefix = this.prefix + `watermarks/${watermark}/`;
            }
        }
    }

    isDisabled(screen: string): boolean {
        return this.disabledItems.includes(screen);
    }
}
