package com.knecon.fforesight.service.viewerdoc.service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.file.Path;
import java.util.List;
import java.util.Set;

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

import com.knecon.fforesight.service.viewerdoc.LayerIdentifier;
import com.knecon.fforesight.service.viewerdoc.layers.IdpLayerConfig;
import com.knecon.fforesight.service.viewerdoc.layers.OcrDebugLayerConfig;
import com.pdftron.pdf.ElementBuilder;
import com.pdftron.pdf.ElementReader;
import com.pdftron.pdf.ElementWriter;
import com.pdftron.pdf.PDFDoc;
import com.pdftron.pdf.PDFNet;
import com.pdftron.pdf.Page;
import com.pdftron.pdf.PageIterator;
import com.pdftron.sdf.SDFDoc;

import lombok.SneakyThrows;

@Disabled
class PageContentCleanerTest {

    @BeforeEach
    public void init() {

        PDFNet.initialize("demo:1650351709282:7bd235e003000000004ec28a6743e1163a085e2115de2536ab6e2cfe5a");
    }


    @AfterAll
    public static void cleanup() {

        PDFNet.terminate();
    }


    @Test
    @SneakyThrows
    public void testContentCleaning() {

        Path file = Path.of("/home/kschuettler/Downloads/pdf24_zusammengefügt.pdf");
        File tmpFile = new File("/tmp/OCR_DEMO.pdf");
        try (var in = new FileInputStream(file.toFile());//
             var doc = new PDFDoc(in);//
             var out = new FileOutputStream(tmpFile);//
             ElementWriter pageWriter = new ElementWriter();//
             ElementReader reader = new ElementReader();//
             ElementBuilder builder = new ElementBuilder()//
        ) {

            PageContentCleaner pageContentCleaner = PageContentCleaner.builder()
                    .writer(pageWriter)
                    .reader(reader)
                    .elementBuilder(builder)
                    .markedContentToRemove(Set.of(LayerIdentifier.KNECON_OCR.markedContentName(),
                                                  LayerIdentifier.KNECON_AZURE_IDP.markedContentName(),
                                                  LayerIdentifier.KNECON_OCR_DEBUG.markedContentName(),
                                                  LayerIdentifier.IDP_TABLES.markedContentName(),
                                                  LayerIdentifier.IDP_KV_PAIRS.markedContentName(),
                                                  LayerIdentifier.IDP_SECTIONS.markedContentName()))
                    .build();

            try (PageIterator iterator = doc.getPageIterator()) {
                while (iterator.hasNext()) {
                    Page page = iterator.next();

                    pageContentCleaner.removeMarkedContent(page);
                }
            }

            doc.save(out, SDFDoc.SaveMode.REMOVE_UNUSED, null);
        }

    }

    @Test
    @SneakyThrows
    public void activateLayersByDefault() {

        Path file = Path.of("/tmp/OCR_TEST/pdf24_zusammengefügt (1).pdf/viewerDocument.pdf");
        try (var in = new FileInputStream(file.toFile()); PDFDoc doc = new PDFDoc(in); var out = new FileOutputStream("/tmp/OCR_DEMO_OCRED.pdf")) {
            PdftronLayerUtility.setOrderArrayForPresentGroups(doc, List.of(OcrDebugLayerConfig.CONFIG_INSTANCE, IdpLayerConfig.CONFIG_INSTANCE));
            doc.save(out, SDFDoc.SaveMode.REMOVE_UNUSED, null);
        }

    }

}