import { Component, computed } from '@angular/core';
import { ViewMode, ViewModes } from '@red/domain';
import { Roles } from '@users/roles';
import { FileDataService } from '../../services/file-data.service';
import { FilePreviewStateService } from '../../services/file-preview-state.service';
import { ViewModeService } from '../../services/view-mode.service';
import { getConfig, IqserDenyDirective } from '@iqser/common-ui';
import { MatTooltip } from '@angular/material/tooltip';
import { TranslateModule } from '@ngx-translate/core';

@Component({
    selector: 'redaction-view-switch',
    templateUrl: './view-switch.component.html',
    styleUrls: ['./view-switch.component.scss'],
    standalone: true,
    imports: [MatTooltip, TranslateModule, IqserDenyDirective],
})
export class ViewSwitchComponent {
    protected readonly isDocumine = getConfig().IS_DOCUMINE;
    protected readonly roles = Roles;
    protected readonly viewModes = ViewModes;
    protected readonly canSwitchToDeltaView = computed(() => {
        const annotations = this._fileDataService.annotations();
        const file = this._state.file();
        const hasChangeLog = annotations.some(a => a.isChangeLogEntry);
        return hasChangeLog && !file.isApproved;
    });
    protected readonly canSwitchToRedactedView = computed(() => {
        const file = this._state.file();
        return !file.analysisRequired && !file.excluded && !file.isError;
    });
    protected readonly canSwitchToEarmarksView = computed(() => {
        const file = this._state.file();
        return file.hasHighlights && !file.analysisRequired && !file.excluded;
    });

    constructor(
        readonly viewModeService: ViewModeService,
        private readonly _state: FilePreviewStateService,
        private readonly _fileDataService: FileDataService,
    ) {}

    switchView(viewMode: ViewMode) {
        if (viewMode === ViewModes.REDACTED) {
            return this.viewModeService.switchToRedacted();
        }
        this.viewModeService.switchTo(viewMode);
    }
}
