import { Injectable } from '@angular/core';
import { IqserUserPreferenceService, ListingMode } from '@iqser/common-ui';
import { SystemDefaultOption, SystemDefaultType } from '../modules/account/utils/dialog-defaults';
import { RedactOrHintOption, RemoveRedactionOption } from '../modules/file-preview/utils/dialog-types';
import { OverwriteFileOption } from '@red/domain';

export const PreferencesKeys = {
    dossierRecent: 'Dossier-Recent',
    filePreviewTooltips: 'File-Preview-Tooltips',
    lastDossierTemplate: 'Last-Dossier-Template',
    filesListingMode: 'Files-Listing-Mode',
    autoExpandFiltersOnActions: 'Auto-Expand-Filters-On-Actions',
    loadAllAnnotationsWarning: 'Load-All-Annotations-Warning',
    tableExtractionType: 'Table-Extraction-Type',
    addRedactionDefaultOption: 'Add-Redaction-Default',
    addHintDefaultOption: 'Add-Hint-Default',
    removeRedactionDefaultOption: 'Remove-Redaction-Default',
    removeRecommendationDefaultOption: 'Remove-Recommendation-Default',
    removeHintDefaultOption: 'Remove-Hint-Default',
    addRedactionDefaultExtraOption: 'Add-Redaction-Default-Extra',
    addHintDefaultExtraOption: 'Add-Hint-Default-Extra',
    removeRedactionDefaultExtraOption: 'Remove-Redaction-Default-Extra',
    removeRecommendationDefaultExtraOption: 'Remove-Recommendation-Default-Extra',
    removeHintDefaultExtraOption: 'Remove-Hint-Default-Extra',
    rememberOverwriteFileOption: 'Remember-Overwrite-File',
} 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);
    }

    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');
    }

    getAddRedactionDefaultOption(): RedactOrHintOption | SystemDefaultType {
        return this._getAttribute(PreferencesKeys.addRedactionDefaultOption, SystemDefaultOption.SYSTEM_DEFAULT) as
            | RedactOrHintOption
            | SystemDefaultType;
    }

    async saveAddRedactionDefaultOption(defaultOption: RedactOrHintOption | SystemDefaultType): Promise<void> {
        await this.save(PreferencesKeys.addRedactionDefaultOption, defaultOption.toString());
    }

    getAddHintDefaultOption(): RedactOrHintOption | SystemDefaultType {
        return this._getAttribute(PreferencesKeys.addHintDefaultOption, SystemDefaultOption.SYSTEM_DEFAULT) as
            | RedactOrHintOption
            | SystemDefaultType;
    }

    async saveAddHintDefaultOption(defaultOption: RedactOrHintOption | SystemDefaultType): Promise<void> {
        await this.save(PreferencesKeys.addHintDefaultOption, defaultOption.toString());
    }

    getRemoveRedactionDefaultOption(): RemoveRedactionOption | SystemDefaultType {
        return this._getAttribute(PreferencesKeys.removeRedactionDefaultOption, SystemDefaultOption.SYSTEM_DEFAULT) as
            | RemoveRedactionOption
            | SystemDefaultType;
    }

    async saveRemoveRedactionDefaultOption(defaultOption: RemoveRedactionOption | SystemDefaultType): Promise<void> {
        await this.save(PreferencesKeys.removeRedactionDefaultOption, defaultOption.toString());
    }

    getRemoveRecommendationDefaultOption(): RemoveRedactionOption | SystemDefaultType {
        return this._getAttribute(PreferencesKeys.removeRecommendationDefaultOption, SystemDefaultOption.SYSTEM_DEFAULT) as
            | RemoveRedactionOption
            | SystemDefaultType;
    }

    async saveRemoveRecommendationDefaultOption(defaultOption: RemoveRedactionOption | SystemDefaultType): Promise<void> {
        await this.save(PreferencesKeys.removeRecommendationDefaultOption, defaultOption.toString());
    }

    getRemoveHintDefaultOption(): RemoveRedactionOption | SystemDefaultType {
        return this._getAttribute(PreferencesKeys.removeHintDefaultOption, SystemDefaultOption.SYSTEM_DEFAULT) as
            | RemoveRedactionOption
            | SystemDefaultType;
    }

    async saveRemoveHintDefaultOption(defaultOption: RemoveRedactionOption | SystemDefaultType): Promise<void> {
        await this.save(PreferencesKeys.removeHintDefaultOption, defaultOption.toString());
    }

    getAddRedactionDefaultExtraOption(): string {
        return this._getAttribute(PreferencesKeys.addRedactionDefaultExtraOption, 'undefined');
    }

    async saveAddRedactionDefaultExtraOption(defaultOption: boolean | string): Promise<void> {
        await this.save(PreferencesKeys.addRedactionDefaultExtraOption, defaultOption.toString());
    }

    getAddHintDefaultExtraOption(): string {
        return this._getAttribute(PreferencesKeys.addHintDefaultExtraOption, 'undefined');
    }

    async saveAddHintDefaultExtraOption(defaultOption: boolean | string): Promise<void> {
        await this.save(PreferencesKeys.addHintDefaultExtraOption, defaultOption.toString());
    }

    getRemoveRedactionDefaultExtraOption(): string {
        return this._getAttribute(PreferencesKeys.removeRedactionDefaultExtraOption, 'undefined');
    }

    async saveRemoveRedactionDefaultExtraOption(defaultOption: boolean | string): Promise<void> {
        await this.save(PreferencesKeys.removeRedactionDefaultExtraOption, defaultOption.toString());
    }

    getRemoveHintDefaultExtraOption(): string {
        return this._getAttribute(PreferencesKeys.removeHintDefaultExtraOption, 'undefined');
    }

    async saveRemoveHintDefaultExtraOption(defaultOption: boolean | string): Promise<void> {
        await this.save(PreferencesKeys.removeHintDefaultExtraOption, defaultOption.toString());
    }

    getRemoveRecommendationDefaultExtraOption(): string {
        return this._getAttribute(PreferencesKeys.removeRecommendationDefaultExtraOption, 'undefined');
    }

    async saveRemoveRecommendationDefaultExtraOption(defaultOption: boolean | string): Promise<void> {
        await this.save(PreferencesKeys.removeRecommendationDefaultExtraOption, defaultOption.toString());
    }

    getOverwriteFileOption(): OverwriteFileOption | 'undefined' {
        return this._getAttribute(PreferencesKeys.rememberOverwriteFileOption, 'undefined') as OverwriteFileOption | 'undefined';
    }

    async saveOverwriteFileOption(preference: OverwriteFileOption | 'undefined') {
        await this.save(PreferencesKeys.rememberOverwriteFileOption, preference);
    }
}
