import { Component, HostBinding, inject, Input, OnChanges } from '@angular/core';
import { getConfig, StopPropagationDirective } from '@iqser/common-ui';
import { AnnotationWrapper } from '@models/file/annotation.wrapper';
import { ListItem } from '@models/file/list-item';
import { MultiSelectService } from '../../services/multi-select.service';
import { PdfProxyService } from '../../services/pdf-proxy.service';
import { ActionsHelpModeKeys } from '../../utils/constants';
import { AnnotationCardComponent } from '../annotation-card/annotation-card.component';
import { NgIf } from '@angular/common';
import { MatTooltip } from '@angular/material/tooltip';
import { TranslateModule } from '@ngx-translate/core';
import { MatIcon } from '@angular/material/icon';
import { AnnotationActionsComponent } from '../annotation-actions/annotation-actions.component';
import { CommentsComponent } from '../comments/comments.component';
import { AnnotationDetailsComponent } from '../annotation-details/annotation-details.component';

@Component({
    selector: 'redaction-annotation-wrapper',
    templateUrl: './annotation-wrapper.component.html',
    styleUrls: ['./annotation-wrapper.component.scss'],
    standalone: true,
    imports: [
        AnnotationCardComponent,
        NgIf,
        MatTooltip,
        TranslateModule,
        StopPropagationDirective,
        MatIcon,
        AnnotationActionsComponent,
        CommentsComponent,
        AnnotationDetailsComponent,
    ],
})
export class AnnotationWrapperComponent implements OnChanges {
    readonly #isDocumine = getConfig().IS_DOCUMINE;
    protected readonly pdfProxyService = inject(PdfProxyService);
    protected readonly multiSelectService = inject(MultiSelectService);
    @Input({ required: true }) annotation!: ListItem<AnnotationWrapper>;
    @HostBinding('attr.annotation-id') annotationId: string;
    @HostBinding('class.active') active = false;
    actionsHelpModeKey: string;
    showComments = false;

    ngOnChanges() {
        this.annotationId = this.annotation.item.id;
        this.active = this.annotation.isSelected;
        this.actionsHelpModeKey = this.#getActionsHelpModeKey();
    }

    #getActionsHelpModeKey(): string {
        if (!this.#isDocumine) {
            const superType = this.annotation.item.superTypeLabel?.split('.')[1];
            const type = this.annotation.item.type;
            if (superType === 'hint' && (type === 'ocr' || type === 'formula' || type === 'image')) {
                return ActionsHelpModeKeys[`${superType}-${type}`];
            }
            return ActionsHelpModeKeys[superType];
        }
        return '';
    }
}
