import { Component, computed, Inject } from '@angular/core';
import { BASE_HREF } from '@iqser/common-ui/lib/utils';
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';

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

    constructor(
        @Inject(BASE_HREF) private readonly _baseHref: string,
        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);
    }
}
