import { Component, Input, OnInit, ViewChild } from '@angular/core';
import { Dictionary, Dossier } from '@red/domain';
import { EditDossierSaveResult, EditDossierSectionInterface } from '../edit-dossier-section.interface';
import { PermissionsService } from '@services/permissions.service';
import { DictionaryManagerComponent } from '@shared/components/dictionary-manager/dictionary-manager.component';
import { DictionaryService } from '@services/entity-services/dictionary.service';
import { CircleButtonTypes, LoadingService } from '@iqser/common-ui';
import { firstValueFrom } from 'rxjs';
import { DossiersDialogService } from '../../../services/dossiers-dialog.service';

@Component({
    selector: 'redaction-edit-dossier-dictionary',
    templateUrl: './edit-dossier-dictionary.component.html',
    styleUrls: ['./edit-dossier-dictionary.component.scss'],
})
export class EditDossierDictionaryComponent implements EditDossierSectionInterface, OnInit {
    @Input() dossier: Dossier;

    canEdit = false;
    dossierDictionary: Dictionary;

    readonly circleButtonTypes = CircleButtonTypes;
    @ViewChild(DictionaryManagerComponent, { static: false }) private readonly _dictionaryManager: DictionaryManagerComponent;

    constructor(
        private readonly _dictionaryService: DictionaryService,
        private readonly _permissionsService: PermissionsService,
        private readonly _loadingService: LoadingService,
        private readonly _dialogService: DossiersDialogService,
    ) {}

    get changed(): boolean {
        return this._dictionaryManager.editor.hasChanges;
    }

    get disabled(): boolean {
        return !this.canEdit;
    }

    get valid(): boolean {
        return this._dictionaryManager.editor.hasChanges;
    }

    async ngOnInit() {
        this._loadingService.start();
        this.canEdit = this._permissionsService.canEditDossierDictionary(this.dossier);
        await this.#updateDossierDictionary();
        this._loadingService.stop();
    }

    async save(): EditDossierSaveResult {
        try {
            await this._dictionaryService.saveEntries(
                this._dictionaryManager.editor.currentEntries,
                this._dictionaryManager.initialEntries,
                this.dossier.dossierTemplateId,
                this.dossierDictionary.type,
                this.dossier.id,
                false,
            );

            await this.#updateDossierDictionary();
            return { success: true };
        } catch (error) {
            return { success: false };
        }
    }

    revert() {
        this._dictionaryManager.revert();
    }

    openEditDetailsDialog(): void {
        const data = { dictionary: this.dossierDictionary, dossierId: this.dossier.id, readOnly: !this.canEdit };
        this._dialogService.openDialog('editDictionaryDetails', data, async () => {
            await this.#updateDossierDictionary();
        });
    }

    async #updateDossierDictionary() {
        const { dossierId, dossierTemplateId } = this.dossier;
        this.dossierDictionary = await this._dictionaryService.loadDossierDictionary(dossierTemplateId, dossierId);
    }
}
