import { Injectable } from '@angular/core';
import { IqserUserPreferenceService, ListingMode } from '@iqser/common-ui';

export const PreferencesKeys = {
    dossierRecent: 'Dossier-Recent',
    filePreviewTooltips: 'File-Preview-Tooltips',
    lastDossierTemplate: 'Last-Dossier-Template',
    filesListingMode: 'Files-Listing-Mode',
    autoExpandFiltersOnActions: 'Auto-Expand-Filters-On-Actions',
    displaySuggestionsInPreview: 'Display-Suggestions-In-Preview',
    loadAllAnnotationsWarning: 'Load-All-Annotations-Warning',
    openScmDialogByDefault: 'Open-Structured-Component-Management-By-Default',
    tableExtractionType: 'Table-Extraction-Type',
} as const;

@Injectable({
    providedIn: 'root',
})
export class UserPreferenceService extends IqserUserPreferenceService {
    protected readonly _defaultModelPath = 'attributes';
    protected readonly _devFeaturesEnabledKey = 'redaction.enable-dev-features';

    getLastOpenedFileForDossier(dossierId: string): string {
        return this._getAttribute(`${PreferencesKeys.dossierRecent}-${dossierId}`);
    }

    async saveLastOpenedFileForDossier(dossierId: string, fileId: string): Promise<void> {
        await this.save(`${PreferencesKeys.dossierRecent}-${dossierId}`, fileId);
    }

    getLastDossierTemplate(): string {
        return this._getAttribute(PreferencesKeys.lastDossierTemplate);
    }

    async saveLastDossierTemplate(dossierTemplateId: string): Promise<void> {
        await this.save(PreferencesKeys.lastDossierTemplate, dossierTemplateId);
    }

    getFilePreviewTooltipsPreference(): boolean {
        return this._getAttribute(PreferencesKeys.filePreviewTooltips, 'false') === 'true';
    }

    async toggleFilePreviewTooltipsPreference(): Promise<void> {
        const nextValue = (!this.getFilePreviewTooltipsPreference()).toString();
        await this.save(PreferencesKeys.filePreviewTooltips, nextValue);
    }

    getAutoExpandFiltersOnActions(): boolean {
        return this._getAttribute(PreferencesKeys.autoExpandFiltersOnActions, 'false') === 'true';
    }

    async toggleAutoExpandFiltersOnActions(): Promise<void> {
        const nextValue = (!this.getAutoExpandFiltersOnActions()).toString();
        await this.save(PreferencesKeys.autoExpandFiltersOnActions, nextValue);
    }

    getOpenScmDialogByDefault(): boolean {
        return this._getAttribute(PreferencesKeys.openScmDialogByDefault, 'false') === 'true';
    }

    async toggleOpenScmDialogByDefault(): Promise<void> {
        const nextValue = (!this.getOpenScmDialogByDefault()).toString();
        await this.save(PreferencesKeys.openScmDialogByDefault, nextValue);
    }

    getBool(key: string, defaultValue = 'false') {
        return this._getAttribute(key, defaultValue) === 'true';
    }

    getFilesListingMode(): ListingMode {
        return this._getAttribute(PreferencesKeys.filesListingMode) as ListingMode;
    }

    async saveFilesListingMode(listingMode: ListingMode): Promise<void> {
        await this.save(PreferencesKeys.filesListingMode, listingMode);
    }

    getTableExtractionType() {
        return this._getAttribute(PreferencesKeys.tableExtractionType, 'EXPERIMENTAL_IF_MISSING');
    }
}
