package com.docus.server.gdszyzh.util;

import io.webfolder.cdp.Launcher;
import io.webfolder.cdp.command.Page;
import io.webfolder.cdp.session.Session;
import io.webfolder.cdp.session.SessionFactory;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

public class WebPagePrinter implements AutoCloseable {

    private Launcher launcher;
    private SessionFactory factory;
    private Session session;




    /**
     * 初始化 Chrome
     */
    public void init() {
        // 启动 Chrome，传入 headless 和 disable-gpu 参数 [^51^]
//        String chromePath="C:\\Users\\Lenovo\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe";
        String chromePath = "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe";
        List<String> args = Arrays.asList("--headless", "--disable-gpu");
        this.launcher = new Launcher();

        this.factory = launcher.launch(chromePath, args);
    }

    /**
     * 打印网页到PDF - 使用 CDP4J 的 Page.printToPDF() 无参方法 [^51^]
     */
    public   byte[] printToPdf(String url, PrintConfig config,
                             Boolean landscape,
                             Boolean displayHeaderFooter,
                             Boolean printBackground,
                             Double scale,
                             Double paperWidth,
                             Double paperHeight,
                             Double marginTop,
                             Double marginBottom,
                             Double marginLeft,
                             Double marginRight,
                             String pageRanges,
                             Boolean ignoreInvalidPageRanges,
                             String headerTemplate,
                             String footerTemplate,
                             Boolean preferCSSPageSize


    ) throws IOException {
        if (factory == null) {
            init();
        }

        // 创建浏览器上下文（可选，用于隔离）
        String context = factory.createBrowserContext();

        try (Session session = factory.create(context)) {
            this.session = session;

            // 导航到页面
            session.navigate(url);
            session.waitDocumentReady();

            // 额外等待确保内容加载
            try {
                Thread.sleep(config.getWaitTimeMs());
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }

            // 获取 Page 命令接口
            Page page = session.getCommand().getPage();

            // 执行打印 - 无参方法，返回 byte[] [^51^]
            byte[] pdfBytes = page.printToPDF(landscape,
                    displayHeaderFooter,
                    printBackground,
                    scale,
                    paperWidth,
                    paperHeight,
                    marginTop,
                    marginBottom,
                    marginLeft,
                    marginRight,
                    pageRanges,
                    ignoreInvalidPageRanges,
                    headerTemplate,
                    footerTemplate,
                    preferCSSPageSize);

           return pdfBytes;
        }
    }

    @Override
    public void close() {
        if (factory != null) {
            factory.close();
        }
    }
}