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

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 {
    @Input() type: AdminSideNavType;
    @Input() disabledItems: string[] = [];
    readonly translations = adminSideNavTranslations;
    readonly currentUser = getCurrentUser<User>();
    readonly roles = Roles;
    prefix: string;

    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),
                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: 'dossier_templates_info',
                show: true,
            },
            {
                screen: 'entities',
                label: _('admin-side-nav.entities'),
                helpModeKey: 'entities',
                show: true,
            },
            {
                screen: 'rules',
                label: _('admin-side-nav.rule-editor'),
                show: this.userPreferenceService.areDevFeaturesEnabled && 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: true,
            },
            {
                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: 'dossier_states',
                show: true,
            },
            {
                screen: 'reports',
                label: _('admin-side-nav.reports'),
                show: this._permissionsService.has([Roles.reportTemplates.read]),
                helpModeKey: 'reports',
            },
            {
                screen: 'justifications',
                label: _('admin-side-nav.justifications'),
                show: this._permissionsService.has([Roles.legalBasis.read]),
                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,
        readonly userPreferenceService: UserPreferenceService,
    ) {}

    @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);
    }
}
