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

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.file.Files;
import java.nio.file.Path;

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

import com.pdftron.pdf.PDFDoc;
import com.pdftron.pdf.PDFNet;
import com.pdftron.sdf.SDFDoc;

import lombok.SneakyThrows;

class ViewerDocVersioningUtilityTest {

    @BeforeEach
    public void init() {

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


    @AfterAll
    public static void cleanup() {

        PDFNet.terminate();
    }


    @Test
    @SneakyThrows
    public void testMarking() {

        File file = new ClassPathResource("files/empty.pdf").getFile();
        Path tmpFile = Files.createTempFile("markedDocument", ".pdf");
        try (var in = new FileInputStream(file); var doc = new PDFDoc(in); var out = new FileOutputStream(tmpFile.toFile())) {
            ViewerDocVersioningUtility.setVersionInDocument(doc, "layoutParserVersion", "layoutParsingType");
            doc.save(out, SDFDoc.SaveMode.LINEARIZED, null);
        }
        assert ViewerDocVersioningUtility.isCurrentVersion(tmpFile.toFile());
        assert tmpFile.toFile().delete();
    }

}