import { Injectable } from '@angular/core';
import { AnnotationWrapper } from '@models/file/annotation.wrapper';
import { bool } from '@iqser/common-ui/lib/utils';
import { Core } from '@pdftron/webviewer';
import { REDAnnotationManager } from '../../pdf-viewer/services/annotation-manager.service';
import { UserPreferenceService } from '@users/user-preference.service';
import { ReadableRedactionsService } from '../../pdf-viewer/services/readable-redactions.service';
import Annotation = Core.Annotations.Annotation;

@Injectable()
export class SuggestionsService {
    #removedRedactions: AnnotationWrapper[] = [];

    constructor(
        private readonly _annotationManager: REDAnnotationManager,
        private readonly _userPreferenceService: UserPreferenceService,
        private readonly _readableRedactionsService: ReadableRedactionsService,
    ) {}

    set removedRedactions(removedRedactions: AnnotationWrapper[]) {
        this.#removedRedactions = removedRedactions;
    }

    hideSuggestionsInPreview(annotations: Annotation[]): void {
        if (this._readableRedactionsService.active) {
            if (this._userPreferenceService.getDisplaySuggestionsInPreview()) {
                const suggestionsRemove = annotations.filter(
                    a => bool(a.getCustomData('suggestionRemove')) || bool(a.getCustomData('suggestionForceHint')),
                );
                this._annotationManager.hide(suggestionsRemove);
                return;
            }
        }
        const suggestionsToHide = annotations.filter(
            a =>
                (bool(a.getCustomData('suggestionAdd')) && !bool(a.getCustomData('suggestionAddToFalsePositive'))) ||
                bool(a.getCustomData('notSignatureImage')),
        );
        annotations.forEach(a => {
            if (bool(a.getCustomData('suggestionRemove'))) {
                const foundRedaction = this.#removedRedactions.find(r => r.id === a.Id);
                if (!foundRedaction) {
                    suggestionsToHide.push(a);
                }
            }
        });
        this._annotationManager.hide(suggestionsToHide);
    }

    filterWorkloadSuggestionsInPreview(annotations: AnnotationWrapper[]): AnnotationWrapper[] {
        if (this._readableRedactionsService.active) {
            if (this._userPreferenceService.getDisplaySuggestionsInPreview()) {
                return annotations.filter(a => !a.isSuggestionRemove && !a.isSuggestionForceHint);
            }
        }

        annotations = annotations.filter(a => (!a.isSuggestionAdd || a.isSuggestionAddToFalsePositive) && !a.isNotSignatureImage);
        for (let i = annotations.length - 1; i >= 0; i--) {
            const foundRemovedRedaction = this.#removedRedactions.find(r => r.id === annotations[i].id);
            if (foundRemovedRedaction) {
                annotations[i] = foundRemovedRedaction;
            } else if (annotations[i].isSuggestionRemove) {
                annotations.splice(i, 1);
            }
        }
        return annotations;
    }
}
