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

import java.awt.geom.Rectangle2D;
import java.util.LinkedList;
import java.util.List;

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

import com.knecon.fforesight.service.layoutparser.processor.model.GapInformation;
import com.knecon.fforesight.service.layoutparser.processor.model.PageContents;
import com.knecon.fforesight.service.layoutparser.processor.model.PageInformation;
import com.knecon.fforesight.service.layoutparser.processor.services.DividingColumnDetectionService;
import com.knecon.fforesight.service.layoutparser.processor.services.GapDetectionService;
import com.knecon.fforesight.service.layoutparser.processor.services.GapsAcrossLinesService;
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 GapAcrossLinesDetectionServiceTest {

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

        String filename = "files/basf/CustomerFiles/invisible_tables_test-two-pages_ocred.pdf";
        var tmpFileName = "/tmp/" + filename.split("/")[2] + "_COLUMNS.pdf";
        System.out.println("start TextPosition extraction");
        long start = System.currentTimeMillis();
        List<PageInformation> pageInformations = PageContentExtractor.getSortedPageContents(filename).stream().map(PageInformationService::build).toList();
        List<List<Rectangle2D>> columnsPerPage = new LinkedList<>();
        System.out.printf("Finished TextPosition Extraction in %d ms%n", System.currentTimeMillis() - start);
        System.out.println("start column detection");
        start = System.currentTimeMillis();
        for (PageInformation pageInformation : pageInformations) {
            GapInformation gapInformation = GapDetectionService.findGapsInLines(pageInformation.getPageContents().getSortedTextPositionSequences(),  pageInformation.getMainBodyTextFrame());
            columnsPerPage.add(GapsAcrossLinesService.detectXGapsAcrossLines(gapInformation, pageInformation.getMainBodyTextFrame()));
        }
        System.out.printf("Finished column detection in %d ms%n", System.currentTimeMillis() - start);
        System.out.println("start draw rectangles");
        start = System.currentTimeMillis();
        PdfDraw.drawRectanglesPerPage(filename, columnsPerPage, tmpFileName);
        System.out.printf("Finished drawing rectangles in %d ms%n", System.currentTimeMillis() - start);
    }


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

        String filename = "files/basf/CustomerFiles/invisible_tables_test-two-pages_ocred.pdf";
        var tmpFileName = "/tmp/" + filename.split("/")[2] + "_COLUMNS.pdf";
        System.out.println("start TextPosition extraction");
        long start = System.currentTimeMillis();
        List<PageContents> sortedTextPositionSequencesPerPage = PageContentExtractor.getSortedPageContents(filename);
        List<List<Rectangle2D>> columnsPerPage = new LinkedList<>();
        System.out.printf("Finished TextPosition Extraction in %d ms%n", System.currentTimeMillis() - start);
        System.out.println("start column detection");
        start = System.currentTimeMillis();
        for (PageContents pageContents : sortedTextPositionSequencesPerPage) {
            columnsPerPage.add(DividingColumnDetectionService.detectColumns(pageContents));
        }
        System.out.printf("Finished column detection in %d ms%n", System.currentTimeMillis() - start);
        System.out.println("start draw rectangles");
        start = System.currentTimeMillis();
        PdfDraw.drawRectanglesPerPage(filename, columnsPerPage, tmpFileName);
        System.out.printf("Finished drawing rectangles in %d ms%n", System.currentTimeMillis() - start);
    }

}