package com.smppw.modaq.application.components;

import cn.hutool.core.collection.ListUtil;
import org.apache.fontbox.util.BoundingBox;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDFontDescriptor;
import org.apache.pdfbox.pdmodel.font.PDType3Font;
import org.apache.pdfbox.text.TextPosition;
import technology.tabula.RectangleSpatialIndex;
import technology.tabula.TextElement;
import technology.tabula.TextStripper;
import technology.tabula.Utils;

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

/**
 * @author wangzaijun
 * @date 2024/9/12 14:00
 * @description 自定义的文本去水印方法，发现水印基本是旋转文字并且比报告内其他文字都大；主要依据文本旋转角度和字体大小判断是否为水印
 */
public class CustomTabulaTextStripper extends TextStripper {
    private static final String NBSP = "\u00A0";
    private static final float AVG_HEIGHT_MULT_THRESHOLD = 6.0f;
    private static final float MAX_BLANK_FONT_SIZE = 40.0f;
    private static final float MIN_BLANK_FONT_SIZE = 2.0f;
    private final PDDocument document;
    private final ArrayList<TextElement> textElements;
    private final RectangleSpatialIndex<TextElement> spatialIndex;
    private float minCharWidth = Float.MAX_VALUE;
    private float minCharHeight = Float.MAX_VALUE;
    private float totalHeight = 0.0f;
    private int countHeight = 0;

    public CustomTabulaTextStripper(PDDocument document, int pageNumber) throws IOException {
        super(document, pageNumber);
        this.document = document;
        this.setStartPage(pageNumber);
        this.setEndPage(pageNumber);
        this.textElements = new ArrayList<>();
        this.spatialIndex = new RectangleSpatialIndex<>();
    }

    public void process() throws IOException {
        this.getText(this.document);
    }

    @Override
    protected void writeString(String string, List<TextPosition> textPositions) {
        // 有旋转角度的文字
        List<TextPosition> rotationTexts = ListUtil.list(false);
        for (TextPosition textPosition : textPositions) {
            float[][] values = textPosition.getTextMatrix().getValues();
            double degrees = Math.toDegrees(Math.atan2(values[0][1], values[0][0]));
            if (degrees % 90. > 0.1) {
                rotationTexts.add(textPosition);
            }
        }
        // 如果全是水印文字则直接去除
        if (textPositions.size() == rotationTexts.size()) {
            return;
        }

        // 其他场景需要写TextElement属性
        for (TextPosition textPosition : textPositions) {
            if (textPosition == null) {
                continue;
            }

            String c = textPosition.getUnicode();

            // if c not printable, return
            if (!isPrintable(c)) {
                continue;
            }

            float h = textPosition.getHeightDir();

            // 必须是一个空格，否则SpreadsheetExtractionAlgorithm类的73行报错字符串chat获取时越界
            if (c.equals(NBSP)) { // replace non-breaking space for space
                c = " ";
            }

            // 文字没有旋转角度，并且水印字体大小没有包含当前文字时说明是正常文字
            if (rotationTexts.contains(textPosition)) {
                c = "";
            }

            float wos = textPosition.getWidthOfSpace();

            TextElement te = new TextElement(Utils.round(textPosition.getYDirAdj() - h, 2),
                    Utils.round(textPosition.getXDirAdj(), 2), Utils.round(textPosition.getWidthDirAdj(), 2),
                    Utils.round(textPosition.getHeightDir(), 2), textPosition.getFont(), textPosition.getFontSizeInPt(), c,
                    // workaround a possible bug in PDFBox:
                    // https://issues.apache.org/jira/browse/PDFBOX-1755
                    wos, textPosition.getDir());

            this.minCharWidth = (float) Math.min(this.minCharWidth, te.getWidth());
            this.minCharHeight = (float) Math.min(this.minCharHeight, te.getHeight());

            countHeight++;
            totalHeight += (float) te.getHeight();
            float avgHeight = totalHeight / countHeight;

            //We have an issue where tall blank cells throw off the row height calculation
            //Introspect a blank cell a bit here to see if it should be thrown away
            if ((te.getText() == null || te.getText().trim().isEmpty())) {
                //if the cell height is more than AVG_HEIGHT_MULT_THRESHOLDxaverage, throw it away
                if (avgHeight > 0
                        && te.getHeight() >= (avgHeight * AVG_HEIGHT_MULT_THRESHOLD)) {
                    continue;
                }

                //if the font size is outside of reasonable ranges, throw it away
                if (textPosition.getFontSizeInPt() > MAX_BLANK_FONT_SIZE || textPosition.getFontSizeInPt() < MIN_BLANK_FONT_SIZE) {
                    continue;
                }
            }

            this.spatialIndex.add(te);
            this.textElements.add(te);
        }
    }

    @Override
    protected float computeFontHeight(PDFont font) throws IOException {
        BoundingBox bbox = font.getBoundingBox();
        if (bbox.getLowerLeftY() < Short.MIN_VALUE) {
            // PDFBOX-2158 and PDFBOX-3130
            // files by Salmat eSolutions / ClibPDF Library
            bbox.setLowerLeftY(-(bbox.getLowerLeftY() + 65536));
        }
        // 1/2 the bbox is used as the height todo: why?
        float glyphHeight = bbox.getHeight() / 2;

        // sometimes the bbox has very high values, but CapHeight is OK
        PDFontDescriptor fontDescriptor = font.getFontDescriptor();
        if (fontDescriptor != null) {
            float capHeight = fontDescriptor.getCapHeight();
            if (Float.compare(capHeight, 0) != 0 &&
                    (capHeight < glyphHeight || Float.compare(glyphHeight, 0) == 0)) {
                glyphHeight = capHeight;
            }
            // PDFBOX-3464, PDFBOX-448:
            // sometimes even CapHeight has very high value, but Ascent and Descent are ok
            float ascent = fontDescriptor.getAscent();
            float descent = fontDescriptor.getDescent();
            if (ascent > 0 && descent < 0 &&
                    ((ascent - descent) / 2 < glyphHeight || Float.compare(glyphHeight, 0) == 0)) {
                glyphHeight = (ascent - descent) / 2;
            }
        }

        // transformPoint from glyph space -> text space
        float height;
        if (font instanceof PDType3Font) {
            height = font.getFontMatrix().transformPoint(0, glyphHeight).y;
        } else {
            height = glyphHeight / 1000;
        }

        return height;
    }

    private boolean isPrintable(String s) {
        char c;
        Character.UnicodeBlock block;
        boolean printable = false;
        for (int i = 0; i < s.length(); i++) {
            c = s.charAt(i);
            block = Character.UnicodeBlock.of(c);
            printable |= !Character.isISOControl(c) && block != null && block != Character.UnicodeBlock.SPECIALS;
        }
        return printable;
    }

    public List<TextElement> getTextElements() {
        return this.textElements;
    }

    public RectangleSpatialIndex<TextElement> getSpatialIndex() {
        return spatialIndex;
    }

    public float getMinCharWidth() {
        return minCharWidth;
    }

    public float getMinCharHeight() {
        return minCharHeight;
    }
}
