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

import java.io.File;
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.iqser.red.commons.jackson.ObjectMapperFactory;
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.visualization.LayoutGridService;
import com.knecon.fforesight.service.layoutparser.server.utils.BuildDocumentTest;
import com.knecon.fforesight.service.viewerdoc.service.ViewerDocumentService;

import lombok.SneakyThrows;

public class ViewerDocumentTest extends BuildDocumentTest {

    ViewerDocumentService viewerDocumentService = new ViewerDocumentService(null);
    LayoutGridService layoutGridService = new LayoutGridService(viewerDocumentService);


    @Test
    @SneakyThrows
    public void testViewerDocument() {

        String fileName = "files/syngenta/CustomerFiles/S-Metolachlor_RAR_01_Volume_1_2018-09-06.pdf";
        String tmpFileName = "/tmp/" + Path.of(fileName).getFileName() + "_VIEWER.pdf";

        var documentFile = new ClassPathResource(fileName).getFile();

        long start = System.currentTimeMillis();
        Document document = buildGraph(fileName, LayoutParsingType.REDACT_MANAGER);
        layoutGridService.addLayoutGrid(documentFile, document, new File(tmpFileName), true, false);
        System.out.printf("Total time: %.2fs%n", ((float) (System.currentTimeMillis() - start)) / 1000);
    }


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

        String fileName = "files/cv_tables/brokenTablesOnOcr_ocred.pdf";
        String tableFileName = "files/cv_tables/brokenTablesOnOcr_ocred.TABLES.json";
        String tmpFileName = "/tmp/" + Path.of(fileName).getFileName() + "_VIEWER.pdf";

        var mapper = ObjectMapperFactory.create();
        var tableResponse = mapper.readValue(new ClassPathResource(tableFileName).getInputStream(), TableServiceResponse.class);
        var documentFile = new ClassPathResource(fileName).getFile();

        var classificationDocument = layoutParsingPipeline.parseLayout(LayoutParsingType.DOCUMINE_OLD,
                                                                       documentFile,
                                                                       new ImageServiceResponse(),
                                                                       tableResponse,
                                                                       new VisualLayoutParsingResponse(),
                                                                       Map.of("file", Path.of(fileName).getFileName().toFile().toString()));
        ViewerDocumentService viewerDocumentService = new ViewerDocumentService(null);
        LayoutGridService layoutGridService = new LayoutGridService(viewerDocumentService);
        Document document = DocumentGraphFactory.buildDocumentGraph(LayoutParsingType.DOCUMINE_OLD, classificationDocument);

        layoutGridService.addLayoutGrid(documentFile, document, new File(tmpFileName), true, false);
    }

}

