import { computed, Injectable, Signal, signal } from '@angular/core';
import { Observable } from 'rxjs';
import { ViewMode, ViewModes } from '@red/domain';
import { toObservable } from '@angular/core/rxjs-interop';

@Injectable()
export class ViewModeService {
    readonly viewMode$: Observable<ViewMode>;
    readonly viewMode: Signal<ViewMode>;
    readonly isEarmarks = computed(() => this.viewMode() === ViewModes.TEXT_HIGHLIGHTS);
    readonly isRedacted = computed(() => this.viewMode() === ViewModes.REDACTED);
    readonly isStandard = computed(() => this.viewMode() === ViewModes.STANDARD);
    readonly isDelta = computed(() => this.viewMode() === ViewModes.DELTA);
    readonly #viewMode = signal<ViewMode>(ViewModes.STANDARD);
    readonly onlyPagesWithAnnotations = computed(() =>
        ([ViewModes.DELTA, ViewModes.TEXT_HIGHLIGHTS] as ViewMode[]).includes(this.#viewMode()),
    );

    constructor() {
        this.viewMode$ = toObservable(this.#viewMode);
        this.viewMode = this.#viewMode.asReadonly();
    }

    is(viewMode: ViewMode) {
        return this.viewMode() === viewMode;
    }

    switchTo(viewMode: ViewMode) {
        this.#viewMode.set(viewMode);
    }

    switchToStandard() {
        this.switchTo(ViewModes.STANDARD);
    }

    switchToDelta() {
        this.switchTo(ViewModes.DELTA);
    }

    switchToRedacted() {
        this.switchTo(ViewModes.REDACTED);
    }

    switchToHighlights() {
        this.switchTo(ViewModes.TEXT_HIGHLIGHTS);
    }
}
