package com.iqser.red.service.persistence.management.v1.processor.service.layoutparsing;

import java.util.Optional;

import org.springframework.stereotype.Service;

import com.iqser.red.service.persistence.management.v1.processor.service.FileManagementStorageService;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.DossierTemplatePersistenceService;
import com.iqser.red.service.persistence.management.v1.processor.settings.FileManagementServiceSettings;
import com.iqser.red.service.persistence.management.v1.processor.utils.StorageIdUtils;
import com.iqser.red.service.persistence.service.v1.api.shared.model.dossiertemplate.dossier.file.FileType;
import com.knecon.fforesight.service.layoutparser.internal.api.queue.LayoutParsingRequest;
import com.knecon.fforesight.service.layoutparser.internal.api.queue.LayoutParsingType;

import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class LayoutParsingRequestFactory {

    private final FileManagementStorageService fileManagementStorageService;

    private final FileManagementServiceSettings fileManagementServiceSettings;

    private final DossierTemplatePersistenceService dossierTemplatePersistenceService;


    public LayoutParsingRequest build(String dossierTemplateId, String dossierId, String fileId, boolean priority) {

        LayoutParsingType layoutParsingType = dossierTemplatePersistenceService.getDossierTemplate(
                dossierTemplateId).getLayoutParsingType();

        Optional<String> optionalImageFileId = fileManagementStorageService.objectExists(dossierId, fileId, FileType.IMAGE_INFO) //
                ? Optional.of(StorageIdUtils.getStorageId(dossierId, fileId, FileType.IMAGE_INFO)) : Optional.empty();

        Optional<String> optionalTableFileId = fileManagementStorageService.objectExists(dossierId, fileId, FileType.TABLES) //
                ? Optional.of(StorageIdUtils.getStorageId(dossierId, fileId, FileType.TABLES)) : Optional.empty();

        Optional<String> optionalVisualLayoutParsingFileId = fileManagementStorageService.objectExists(dossierId, fileId, FileType.VISUAL_LAYOUT) //
                ? Optional.of(StorageIdUtils.getStorageId(dossierId, fileId, FileType.VISUAL_LAYOUT)) : Optional.empty();

        return LayoutParsingRequest.builder()
                .layoutParsingType(layoutParsingType)
                .identifier(QueueMessageIdentifierService.buildIdentifier(dossierId, fileId, priority))
                .originFileStorageId(StorageIdUtils.getStorageId(dossierId, fileId, FileType.ORIGIN))
                .imagesFileStorageId(optionalImageFileId)
                .tablesFileStorageId(optionalTableFileId)
                .visualLayoutParsingFileId(optionalVisualLayoutParsingFileId)
                .pageFileStorageId(StorageIdUtils.getStorageId(dossierId, fileId, FileType.DOCUMENT_PAGES))
                .structureFileStorageId(StorageIdUtils.getStorageId(dossierId, fileId, FileType.DOCUMENT_STRUCTURE))
                .textBlockFileStorageId(StorageIdUtils.getStorageId(dossierId, fileId, FileType.DOCUMENT_TEXT))
                .positionBlockFileStorageId(StorageIdUtils.getStorageId(dossierId, fileId, FileType.DOCUMENT_POSITION))
                .simplifiedTextStorageId(StorageIdUtils.getStorageId(dossierId, fileId, FileType.SIMPLIFIED_TEXT))
                .viewerDocumentStorageId(StorageIdUtils.getStorageId(dossierId, fileId, FileType.VIEWER_DOCUMENT))
                .documentMarkdownFileStorageId(fileManagementServiceSettings.isStoreMarkdown() ? Optional.of(StorageIdUtils.getStorageId(dossierId,
                                                                                                                                         fileId,
                                                                                                                                         FileType.MARKDOWN)) : Optional.empty())
                .build();
    }

}
