package com.knecon.fforesight.service.layoutparser.server.graph;

import java.io.File;
import java.io.FileOutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.core.io.ClassPathResource;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.iqser.red.commons.jackson.ObjectMapperFactory;
import com.knecon.fforesight.service.layoutparser.internal.api.data.redaction.DocumentData;
import com.knecon.fforesight.service.layoutparser.internal.api.queue.LayoutParsingType;
import com.knecon.fforesight.service.layoutparser.processor.model.graph.nodes.Document;
import com.knecon.fforesight.service.layoutparser.processor.python_api.model.image.ImageServiceResponse;
import com.knecon.fforesight.service.layoutparser.processor.python_api.model.table.TableServiceResponse;
import com.knecon.fforesight.service.layoutparser.processor.python_api.model.table.VisualLayoutParsingResponse;
import com.knecon.fforesight.service.layoutparser.processor.services.factory.DocumentGraphFactory;
import com.knecon.fforesight.service.layoutparser.processor.services.mapper.DocumentDataMapper;
import com.knecon.fforesight.service.layoutparser.server.utils.BuildDocumentTest;

import lombok.SneakyThrows;

public class DocumentGraphJsonWritingTest extends BuildDocumentTest {

    @Test
    @SneakyThrows
    @Disabled
    public void writeJsonForAllFilesTest() {

        Path path = Path.of("/tmp/test_files");

        Files.walk(path)
                .map(Path::toFile)
                .filter(File::isFile)
                .filter(file -> file.getName().endsWith(".pdf"))
                .peek(System.out::println)
                .map(File::toPath)
                .forEach(this::writeJsons);
    }


    @Test
    @SneakyThrows
    @Disabled
    public void writeJsonForFileTest() {

        var resource = new ClassPathResource("files/syngenta/CustomerFiles/1 Abamectin_prr.pdf");
        writeJsons(resource.getFile().toPath());
    }


    @SneakyThrows
    private void writeJsons(Path filename) {

        Document documentGraph = DocumentGraphFactory.buildDocumentGraph(LayoutParsingType.REDACT_MANAGER_OLD,
                layoutParsingPipeline.parseLayout(LayoutParsingType.REDACT_MANAGER_OLD,
                        filename.toFile(),
                        new ImageServiceResponse(),
                        new TableServiceResponse(),
                        new VisualLayoutParsingResponse(),
                        Map.of("file",filename.toFile().toString())));

        DocumentData documentData = DocumentDataMapper.toDocumentData(documentGraph);
        ObjectMapper mapper = ObjectMapperFactory.create();

        var stem = Path.of("/tmp/DocumentGraphJsonWritingTest");
        stem.toFile().mkdirs();
        var tmpFilePath = stem.resolve(filename.getFileName());
        mapper.writeValue(new FileOutputStream(new File(tmpFilePath + "_structure" + ".json")), documentData.getDocumentStructure());
        mapper.writeValue(new FileOutputStream(new File(tmpFilePath + "_text" + ".json")), documentData.getDocumentTextData());
        mapper.writeValue(new FileOutputStream(new File(tmpFilePath + "_positions" + ".json")), documentData.getDocumentPositions());
        mapper.writeValue(new FileOutputStream(new File(tmpFilePath + "_pages" + ".json")), documentData.getDocumentPages());
    }

}
