import { NgForOf, NgIf } from '@angular/common';
import { Component, OnInit } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { FormBuilder, ReactiveFormsModule, UntypedFormGroup } from '@angular/forms';
import { MatDialogClose } from '@angular/material/dialog';
import { MatFormField } from '@angular/material/form-field';
import { MatOption, MatSelect, MatSelectTrigger } from '@angular/material/select';
import { MatTooltip } from '@angular/material/tooltip';
import { DetailsRadioOption } from '@common-ui/inputs/details-radio/details-radio-option';
import { DetailsRadioComponent } from '@common-ui/inputs/details-radio/details-radio.component';
import {
    CircleButtonComponent,
    HasScrollbarDirective,
    IconButtonComponent,
    IconButtonTypes,
    IqserDenyDirective,
    IqserDialogComponent,
    IqserPermissionsService,
} from '@iqser/common-ui';
import { TranslateModule } from '@ngx-translate/core';
import { Dictionary, Dossier, IAddRedactionRequest } from '@red/domain';
import { ActiveDossiersService } from '@services/dossiers/active-dossiers.service';
import { DictionaryService } from '@services/entity-services/dictionary.service';
import { Roles } from '@users/roles';
import { UserPreferenceService } from '@users/user-preference.service';
import { stringToBoolean } from '@utils/functions';
import { tap } from 'rxjs/operators';
import { SystemDefaultOption, SystemDefaults } from '../../../account/utils/dialog-defaults';
import { getRedactOrHintOptions } from '../../utils/dialog-options';
import { AddHintData, AddHintResult, RedactOrHintOption, RedactOrHintOptions } from '../../utils/dialog-types';

@Component({
    templateUrl: './add-hint-dialog.component.html',
    styleUrls: ['./add-hint-dialog.component.scss'],
    standalone: true,
    imports: [
        ReactiveFormsModule,
        DetailsRadioComponent,
        IqserDenyDirective,
        MatFormField,
        MatSelectTrigger,
        MatSelect,
        MatOption,
        MatTooltip,
        NgIf,
        HasScrollbarDirective,
        IconButtonComponent,
        TranslateModule,
        CircleButtonComponent,
        MatDialogClose,
        NgForOf,
    ],
})
export class AddHintDialogComponent extends IqserDialogComponent<AddHintDialogComponent, AddHintData, AddHintResult> implements OnInit {
    #applyToAllDossiers: boolean;
    readonly #dossier: Dossier;
    readonly #isRss = this._iqserPermissionsService.has(Roles.getRss);
    readonly roles = Roles;
    readonly iconButtonTypes = IconButtonTypes;
    readonly options: DetailsRadioOption<RedactOrHintOption>[];
    dictionaryRequest = false;
    dictionaries: Dictionary[] = [];
    form!: UntypedFormGroup;

    constructor(
        private readonly _activeDossiersService: ActiveDossiersService,
        private readonly _dictionaryService: DictionaryService,
        private readonly _iqserPermissionsService: IqserPermissionsService,
        private readonly _formBuilder: FormBuilder,
        private readonly _userPreferences: UserPreferenceService,
    ) {
        super();
        this.#dossier = _activeDossiersService.find(this.data.dossierId);
        this.#applyToAllDossiers = this.applyToAll;
        this.options = getRedactOrHintOptions(
            this.#dossier,
            this.#applyToAllDossiers,
            this.data.isApprover,
            this.data.isPageExcluded,
            false,
            true,
            this.#isRss,
        );

        this.form = this.#getForm();

        this.form
            .get('option')
            .valueChanges.pipe(
                tap((option: DetailsRadioOption<RedactOrHintOption>) => {
                    this.dictionaryRequest = option.value === RedactOrHintOptions.IN_DOSSIER;
                    this.#setDictionaries();
                    this.#resetValues();
                }),
                takeUntilDestroyed(),
            )
            .subscribe();
    }

    get displayedDictionaryLabel() {
        const dictType = this.form.get('dictionary').value;
        if (dictType) {
            return this.dictionaries.find(d => d.type === dictType)?.label ?? null;
        }
        return null;
    }

    get applyToAll() {
        return this.isSystemDefault || this._userPreferences.getAddHintDefaultExtraOption() === 'undefined'
            ? this.data.applyToAllDossiers ?? true
            : stringToBoolean(this._userPreferences.getAddHintDefaultExtraOption());
    }

    get isSystemDefault(): boolean {
        return this._userPreferences.getAddHintDefaultOption() === SystemDefaultOption.SYSTEM_DEFAULT;
    }

    get defaultOption() {
        const defaultOption = this.isSystemDefault
            ? this.#getOption(SystemDefaults.ADD_HINT_DEFAULT)
            : this.#getOption(this._userPreferences.getAddHintDefaultOption() as RedactOrHintOption);
        this.dictionaryRequest = defaultOption.value === RedactOrHintOptions.IN_DOSSIER;
        if (this.dictionaryRequest) {
            this.#setDictionaries();
            return defaultOption;
        }
        return defaultOption ?? this.options[0];
    }

    get disabled() {
        return this.#isRss || !this.form.get('dictionary').value;
    }

    async ngOnInit(): Promise<void> {
        this.#setDictionaries();
        this.#resetValues();
    }

    extraOptionChanged(option: DetailsRadioOption<RedactOrHintOption>): void {
        this.#applyToAllDossiers = option.extraOption.checked;

        this.#setDictionaries();
        if (this.#applyToAllDossiers && this.form.get('dictionary').value) {
            const selectedDictionaryLabel = this.form.get('dictionary').value;
            const selectedDictionary = this.dictionaries.find(d => d.type === selectedDictionaryLabel);
            if (!selectedDictionary) {
                this.form.get('dictionary').setValue(null);
            }
        }
    }

    typeChanged() {
        if (!this.#applyToAllDossiers) {
            const selectedDictionaryType = this.form.get('dictionary').value;
            const selectedDictionary = this.dictionaries.find(d => d.type === selectedDictionaryType);
            this.options[1].extraOption.disabled = selectedDictionary.dossierDictionaryOnly;
        }
    }

    save(): void {
        this.#enhanceManualRedaction(this.data.manualRedactionEntryWrapper.manualRedactionEntry);
        const redaction = this.data.manualRedactionEntryWrapper.manualRedactionEntry;
        this.dialogRef.close({
            redaction,
            dictionary: this.dictionaries.find(d => d.type === this.form.get('dictionary').value),
        });
    }

    #setDictionaries() {
        this.dictionaries = this._dictionaryService.getAddHintDictionaries(
            this.#dossier.dossierId,
            !this.#applyToAllDossiers,
            this.dictionaryRequest,
        );
    }

    #getForm(): UntypedFormGroup {
        return this._formBuilder.group({
            selectedText: this.data?.manualRedactionEntryWrapper?.manualRedactionEntry?.value,
            comment: [null],
            dictionary: [null],
            option: [this.defaultOption],
        });
    }

    #enhanceManualRedaction(addRedactionRequest: IAddRedactionRequest) {
        addRedactionRequest.reason = 'Dictionary Request';
        addRedactionRequest.type = this.form.get('dictionary').value;
        addRedactionRequest.section = null;
        addRedactionRequest.value = this.form.get('selectedText').value;

        const selectedType = this.dictionaries.find(d => d.type === addRedactionRequest.type);
        addRedactionRequest.addToDictionary = this.dictionaryRequest && !!selectedType?.hasDictionary;

        const commentValue = this.form.get('comment').value;
        addRedactionRequest.comment = commentValue ? { text: commentValue } : null;
        addRedactionRequest.addToAllDossiers = this.data.isApprover && this.dictionaryRequest && this.#applyToAllDossiers;
    }

    #resetValues() {
        this.#applyToAllDossiers = this.applyToAll;
        if (!this.#isRss) {
            this.options[1].extraOption.checked = this.#applyToAllDossiers;
        }
        this.form.get('dictionary').setValue(null);
    }

    #getOption(option: RedactOrHintOption): DetailsRadioOption<RedactOrHintOption> {
        return this.options.find(o => o.value === option);
    }
}
