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

import java.util.Collection;
import java.util.List;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import com.knecon.fforesight.service.layoutparser.processor.model.PageInformation;
import com.knecon.fforesight.service.layoutparser.processor.services.PageInformationService;
import com.knecon.fforesight.service.layoutparser.processor.services.PageContentExtractor;
import com.knecon.fforesight.service.layoutparser.server.utils.visualizations.PdfDraw;

import lombok.SneakyThrows;

class PageInformationServiceTest {

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

        String filename = "files/basf/CustomerFiles/invisible_tables_test-two-pages_ocred.pdf";
        var tmpFileName = "/tmp/" + filename.split("/")[2] + "_GAPS.pdf";
        System.out.println("start TextPosition extraction");
        long start = System.currentTimeMillis();
        List<PageInformation> pageInformations = PageContentExtractor.getSortedPageContents(filename).stream().map(PageInformationService::build).toList();
        System.out.printf("Finished TextPosition Extraction in %d ms%n", System.currentTimeMillis() - start);
        System.out.println("start gap detection");
        start = System.currentTimeMillis();
        System.out.printf("Finished gap detection in %d ms%n", System.currentTimeMillis() - start);
        System.out.println("start draw rectangles");
        start = System.currentTimeMillis();
        PdfDraw.drawRectanglesAndLinesPerPage(filename,
                pageInformations.stream().map(PageInformation::getGapInformation).map(gaps -> gaps.getYGaps().stream().flatMap(Collection::stream).toList()).toList(),
                pageInformations.stream().map(PageInformation::getGapInformation).map(gaps -> gaps.getXGaps().stream().flatMap(Collection::stream).toList()).toList(),
                tmpFileName);
        System.out.printf("Finished drawing rectangles in %d ms%n", System.currentTimeMillis() - start);
    }

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

        String filename = "files/basf/CustomerFiles/invisible_tables_test-two-pages_ocred.pdf";
        var tmpFileName = "/tmp/" + filename.split("/")[2] + "_GAPS.pdf";
        System.out.println("start TextPosition extraction");
        long start = System.currentTimeMillis();
        List<PageInformation> pageInformations = PageContentExtractor.getSortedPageContents(filename).stream().map(PageInformationService::build).toList();
        System.out.printf("Finished TextPosition Extraction in %d ms%n", System.currentTimeMillis() - start);
        System.out.println("start gap detection");
        start = System.currentTimeMillis();
        System.out.printf("Finished gap detection in %d ms%n", System.currentTimeMillis() - start);
        System.out.println("start draw rectangles");
        start = System.currentTimeMillis();
        PdfDraw.drawRectanglesPerPageNumberedByLine(filename,
                pageInformations.stream().map(PageInformation::getLineInformation).map(gaps -> gaps.getBBoxWithGapsByLines().stream().toList()).toList(),
                tmpFileName);
        System.out.printf("Finished drawing rectangles in %d ms%n", System.currentTimeMillis() - start);
    }

}