import { Component, Input } from '@angular/core';
import { AnnotationWrapper } from '@models/file/annotation.wrapper';
import { EarmarkGroup, EarmarkOperation } from '@red/domain';
import { Roles } from '@users/roles';
import { FileDataService } from '../../services/file-data.service';
import { FilePreviewDialogService } from '../../services/file-preview-dialog.service';
import { FilePreviewStateService } from '../../services/file-preview-state.service';
import { MultiSelectService } from '../../services/multi-select.service';

@Component({
    selector: 'redaction-highlights-separator',
    templateUrl: './highlights-separator.component.html',
    styleUrls: ['./highlights-separator.component.scss'],
})
export class HighlightsSeparatorComponent {
    @Input({ required: true }) highlightGroup: EarmarkGroup;
    @Input({ required: true }) annotation: AnnotationWrapper;

    readonly roles = Roles;

    constructor(
        private readonly _dialogService: FilePreviewDialogService,
        protected readonly _state: FilePreviewStateService,
        private readonly _fileDataService: FileDataService,
        protected readonly _multiSelectService: MultiSelectService,
    ) {}

    convertHighlights(highlightGroup: EarmarkGroup): void {
        const data = this._getActionData(highlightGroup, EarmarkOperation.CONVERT);
        this._dialogService.openDialog('highlightAction', data);
    }

    removeHighlights(highlightGroup: EarmarkGroup): void {
        const data = this._getActionData(highlightGroup, EarmarkOperation.REMOVE);
        this._dialogService.openDialog('highlightAction', data);
    }

    private _getActionData(highlightGroup: EarmarkGroup, operation: EarmarkOperation) {
        const highlights = this._fileDataService.all.filter(a => a.color === highlightGroup.color);
        return {
            dossierId: this._state.dossierId,
            fileId: this._state.fileId,
            color: highlightGroup.color,
            operation,
            highlights,
            pageNumber: this.annotation.pageNumber,
            annotationsChanged: async () => {
                await this._fileDataService.loadEarmarks();
                await this._fileDataService.annotationsChanged();
            },
        };
    }
}
