import { inject, Injectable, NgZone } from '@angular/core';
import { AnnotationWrapper } from '@models/file/annotation.wrapper';
import { AnnotationPermissions } from '@models/file/annotation.permissions';
import { PermissionsService } from '@services/permissions.service';
import { FilePreviewStateService } from './file-preview-state.service';
import { TranslateService } from '@ngx-translate/core';
import { AnnotationActionsService } from './annotation-actions.service';
import { IqserPermissionsService } from '@iqser/common-ui';
import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker';
import { IHeaderElement } from '@red/domain';
import { Roles } from '@users/roles';
import { REDAnnotationManager } from '../../pdf-viewer/services/annotation-manager.service';
import { BASE_HREF_FN } from '@iqser/common-ui/lib/utils';

@Injectable()
export class PdfAnnotationActionsService {
    readonly #permissionsService = inject(PermissionsService);
    readonly #state = inject(FilePreviewStateService);
    readonly #translateService = inject(TranslateService);
    readonly #ngZone = inject(NgZone);
    readonly #convertPath = inject(BASE_HREF_FN);
    readonly #annotationActionsService = inject(AnnotationActionsService);
    readonly #iqserPermissionsService = inject(IqserPermissionsService);
    readonly #annotationManager = inject(REDAnnotationManager);

    get(annotations: AnnotationWrapper[]): IHeaderElement[] {
        const availableActions: IHeaderElement[] = [];
        const permissions = this.#getAnnotationsPermissions(annotations);

        // you can only resize one annotation at a time
        if (permissions.canResize) {
            const firstAnnotation = annotations[0];
            // if we already entered resize-mode previously
            if (firstAnnotation.id === this.#annotationManager.resizingAnnotationId) {
                const acceptResizeButton = this.#getButton('check', _('annotation-actions.resize-accept.label'), () =>
                    this.#annotationActionsService.acceptResize(firstAnnotation),
                );
                const cancelResizeButton = this.#getButton('close', _('annotation-actions.resize-cancel.label'), () =>
                    this.#annotationActionsService.cancelResize(firstAnnotation),
                );

                return [acceptResizeButton, cancelResizeButton];
            }

            const resizeButton = this.#getButton('resize', _('annotation-actions.resize.label'), () =>
                this.#annotationActionsService.resize(firstAnnotation),
            );

            availableActions.push(resizeButton);
        }

        if (permissions.canChangeLegalBasis) {
            const editButton = this.#getButton('edit', _('annotation-actions.edit-reason.label'), () =>
                this.#annotationActionsService.changeLegalBasis(annotations),
            );
            availableActions.push(editButton);
        }

        if (permissions.canRecategorizeImage) {
            const recategorizeButton = this.#getButton('thumb-down', _('annotation-actions.recategorize-image'), () =>
                this.#annotationActionsService.recategorizeImages(annotations),
            );
            availableActions.push(recategorizeButton);
        }

        // if (permissions.canRemoveOrSuggestToRemoveFromDictionary) {
        //     const removeFromDictButton = this.#getButton(
        //         'remove-from-dict',
        //         _('annotation-actions.remove-annotation.remove-from-dict'),
        //         () => this.#annotationActionsService.removeOrSuggestRemoveAnnotation(annotations, true),
        //     );
        //     availableActions.push(removeFromDictButton);
        // }

        if (permissions.canAcceptRecommendation) {
            const acceptRecommendationButton = this.#getButton('check', _('annotation-actions.accept-recommendation.label'), () =>
                this.#annotationActionsService.convertRecommendationToAnnotation(annotations),
            );
            availableActions.push(acceptRecommendationButton);
        }

        if (permissions.canAcceptSuggestion) {
            const acceptSuggestionButton = this.#getButton('check', _('annotation-actions.accept-suggestion.label'), () =>
                this.#annotationActionsService.acceptSuggestion(annotations),
            );
            availableActions.push(acceptSuggestionButton);
        }

        if (permissions.canUndo) {
            const undoButton = this.#getButton('undo', _('annotation-actions.undo'), () =>
                this.#annotationActionsService.undoDirectAction(annotations),
            );
            availableActions.push(undoButton);
        }

        // if (permissions.canMarkAsFalsePositive) {
        //     const markAsFalsePositiveButton = this.#getButton('thumb-down', _('annotation-actions.remove-annotation.false-positive'), () =>
        //         this.#annotationActionsService.markAsFalsePositive(annotations),
        //     );
        //     availableActions.push(markAsFalsePositiveButton);
        // }

        if (permissions.canForceRedaction) {
            const forceRedactionButton = this.#getButton('thumb-up', _('annotation-actions.force-redaction.label'), () =>
                this.#annotationActionsService.forceAnnotation(annotations),
            );
            availableActions.push(forceRedactionButton);
        }

        if (permissions.canForceHint) {
            const forceHintButton = this.#getButton('thumb-up', _('annotation-actions.force-hint.label'), () =>
                this.#annotationActionsService.forceAnnotation(annotations, true),
            );
            availableActions.push(forceHintButton);
        }

        if (permissions.canRejectSuggestion) {
            const rejectSuggestionButton = this.#getButton('close', _('annotation-actions.reject-suggestion'), () =>
                this.#annotationActionsService.rejectSuggestion(annotations),
            );
            availableActions.push(rejectSuggestionButton);
        }

        // if (permissions.canRemoveOrSuggestToRemoveOnlyHere) {
        //     const removeOrSuggestToRemoveOnlyHereButton = this.#getButton(
        //         'trash',
        //         _('annotation-actions.remove-annotation.only-here'),
        //         () => this.#annotationActionsService.removeOrSuggestRemoveAnnotation(annotations, false),
        //     );
        //     availableActions.push(removeOrSuggestToRemoveOnlyHereButton);
        // }

        if (permissions.canRemoveOrSuggestToRemoveRedaction) {
            const removeOrSuggestToRemoveButton = this.#getButton('trash', _('annotation-actions.remove-annotation.remove-redaction'), () =>
                this.#annotationActionsService.removeOrSuggestRemoveRedaction(annotations[0], permissions),
            );
            availableActions.push(removeOrSuggestToRemoveButton);
        }

        return availableActions;
    }

    #getButton(icon: string, title: string, action: () => void | Promise<void>): IHeaderElement {
        return {
            type: 'actionButton',
            img: this.#convertPath(`/assets/icons/general/${icon}.svg`),
            title: this.#translateService.instant(title),
            onClick: () => this.#ngZone.run(async () => action()),
        };
    }

    #getAnnotationsPermissions(annotations: AnnotationWrapper[]) {
        const dossier = this.#state.dossier();
        const isApprover = this.#permissionsService.isApprover(dossier);
        const dictionaries = this.#state.dictionaries;
        const permissions = annotations.map(a => AnnotationPermissions.forUser(isApprover, a, dictionaries, this.#iqserPermissionsService));
        return {
            canResize: permissions.length === 1 && permissions[0].canResizeAnnotation,
            canChangeLegalBasis: permissions.reduce((acc, next) => acc && next.canChangeLegalBasis, true),
            canRecategorizeImage: permissions.reduce((acc, next) => acc && next.canRecategorizeImage, true),
            canRemoveOrSuggestToRemoveFromDictionary: permissions.reduce(
                (acc, next) => acc && next.canRemoveOrSuggestToRemoveFromDictionary,
                true,
            ),
            canAcceptRecommendation: permissions.reduce((acc, next) => acc && next.canAcceptRecommendation, true),
            canAcceptSuggestion: permissions.reduce((acc, next) => acc && next.canAcceptSuggestion, true),
            canUndo:
                this.#iqserPermissionsService.has(Roles.redactions.deleteManual) &&
                permissions.reduce((acc, next) => acc && next.canUndo, true),
            canMarkAsFalsePositive: permissions.reduce((acc, next) => acc && next.canMarkAsFalsePositive, true),
            canForceRedaction: permissions.reduce((acc, next) => acc && next.canForceRedaction, true),
            canForceHint: permissions.reduce((acc, next) => acc && next.canForceHint, true),
            canRejectSuggestion: permissions.reduce((acc, next) => acc && next.canRejectSuggestion, true),
            canRemoveOrSuggestToRemoveOnlyHere: permissions.reduce((acc, next) => acc && next.canRemoveOrSuggestToRemoveOnlyHere, true),
            canRemoveOrSuggestToRemoveRedaction: permissions.reduce((acc, next) => acc && next.canRemoveOrSuggestToRemoveRedaction, true),
        };
    }
}
