import { Component, computed, inject } from '@angular/core';
import { DossierTemplatesService } from '@services/dossier-templates/dossier-templates.service';
import { DocumentInfoService } from '../../services/document-info.service';
import { PermissionsService } from '@services/permissions.service';
import { FilePreviewStateService } from '../../services/file-preview-state.service';
import { type FileAttributeConfigType, FileAttributeConfigTypes } from '@red/domain';
import { FilePreviewDialogService } from '../../services/file-preview-dialog.service';
import { FileAttributesService } from '@services/entity-services/file-attributes.service';
import { ContextComponent } from '@iqser/common-ui/lib/utils';
import { toSignal } from '@angular/core/rxjs-interop';
import { setLocalStorageDataByFileId } from '@utils/local-storage';

interface FileAttribute {
    label: string;
    value: string;
    type: FileAttributeConfigType;
}

interface Context {
    readonly fileAttributes: FileAttribute[];
}

@Component({
    selector: 'redaction-document-info',
    templateUrl: './document-info.component.html',
    styleUrls: ['./document-info.component.scss'],
})
export class DocumentInfoComponent extends ContextComponent<Context> {
    protected readonly _state = inject(FilePreviewStateService);
    protected readonly _dossierTemplate = toSignal(inject(DossierTemplatesService).getEntityChanged$(this._state.dossierTemplateId));
    protected readonly _fileAttributesConfig = toSignal(inject(FileAttributesService).fileAttributesConfig$);
    protected readonly _attributes = toSignal(
        this._documentInfoService.fileAttributes$(this._state.fileId, this._state.dossierId, this._state.dossierTemplateId),
    );
    protected readonly _fileAttributes = computed(() => {
        this._fileAttributesConfig();
        return this._attributes();
    });

    constructor(
        readonly permissionsService: PermissionsService,
        private readonly _documentInfoService: DocumentInfoService,
        private readonly _dialogService: FilePreviewDialogService,
    ) {
        super();
    }

    edit() {
        this._dialogService.openDialog('documentInfo', this._state.file());
    }

    isDate(attribute: FileAttribute) {
        return attribute.type === FileAttributeConfigTypes.DATE;
    }

    closeView() {
        this._documentInfoService.hide();
        setLocalStorageDataByFileId(this._state.fileId, 'show-document-info', false);
    }
}
