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

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.google.common.base.Strings;
import com.knecon.fforesight.service.layoutparser.processor.LayoutparserSettings;
import com.pdftron.pdf.PDFNet;

import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
@RequiredArgsConstructor
public class PDFNetInitializer {

    @Value("${pdftron.license:}")
    private String pdftronLicense;


    @SneakyThrows
    @PostConstruct
    // Do not change back to application runner, if it is application runner it takes messages from the queue before PDFNet is initialized, that leads to UnsatisfiedLinkError.
    public void init() {

        if (Strings.isNullOrEmpty(pdftronLicense)) {
            return;
        }
        log.info("Initializing Native Libraries");
        log.info("Setting pdftron license: {}", pdftronLicense);
        PDFNet.setTempPath("/tmp/pdftron");
        PDFNet.initialize(pdftronLicense);

    }


    @PreDestroy
    public void terminate() {

        PDFNet.terminate();
    }

}
