package com.ruoyi.common.utils.asPose;

import com.aspose.words.*;

/**
 * @Author fuzhilin
 * @Date 2021/11/29 0029 10:42
 * @Description
 */
public class ReplaceAndInsertImage implements IReplacingCallback {

    private String url;
    private String name;
    private double width;
    private double height;

    public ReplaceAndInsertImage(String url, String name, double width, double height) {
        this.url = url;
        this.name = name;
        this.width = width;
        this.height = height;
    }

    @Override
    public int replacing(ReplacingArgs e) throws Exception {
        //获取当前节点
        Node currentNode = e.getMatchNode();
        //节点拆分处理当前匹配字段
        splitRun(currentNode, e.getMatchOffset());
        //获取当前文档
        Document document = (Document) currentNode.getDocument();
        DocumentBuilder builder = new DocumentBuilder(document);
        //将光标移动到指定节点
        builder.moveTo(currentNode);
        //插入图片
        Shape img = builder.insertImage(url,width, height);
        img.setName(name);
        return ReplaceAction.SKIP;
    }

    private void splitRun(Node currentNode, int position) {
        if (currentNode.getNodeType() != NodeType.RUN) {
            return; // 仅处理 Run 节点
        }

        Run currentRun = (Run) currentNode;
        String text = currentRun.getText();
        Node newNode = currentRun.deepClone(true);

        if (text.length() >= position + this.name.length()) {
            currentRun.setText(text.substring(position + this.name.length()));
        } else {
            int remainingLength = position + this.name.length() - text.length();
            currentRun.setText("");

            Node tmpnode = currentRun.getNextSibling();
            while (tmpnode != null && remainingLength > 0) {
                if (tmpnode.getNodeType() == NodeType.RUN) {
                    Run tmpRun = (Run) tmpnode;
                    String tmptext = tmpRun.getText();

                    if (tmptext.length() >= remainingLength) {
                        tmpRun.setText(tmptext.substring(remainingLength));
                        remainingLength = 0;
                    } else {
                        remainingLength -= tmptext.length();
                        tmpRun.setText("");
                    }
                }
                tmpnode = tmpnode.getNextSibling();
            }
        }

        if (position > 0) {
            ((Run) newNode).setText(text.substring(0, position));
            currentRun.getParentNode().insertBefore(newNode, currentRun);
        }
    }

//    private void splitRun(Node currentNode, int position) {
//        String text = currentNode.getText();
//        Node newNode = currentNode.deepClone(true);
//        if (text.length() >= position + this.name.length()) {
//            ((Run) currentNode).setText(text.substring(position + this.name.length()));
//        } else {
//            int morlength = position + this.name.length() - text.length();
//            ((Run) currentNode).setText("");
//            Node tmpnode = currentNode;
//            for (int i = 0; i < this.name.length(); i++) {
////                System.out.println(i);
//                tmpnode = tmpnode.getNextSibling();
//                String tmptext = tmpnode.getText();
////                System.out.println(tmptext);
////                System.out.println(morlength);
////                System.out.println("--------" + (tmptext.length() >= morlength));
//
//                if (tmptext.length() >= morlength) {
//                    ((Run) tmpnode).setText(tmptext.substring(morlength));
//                    break;
//                } else {
//                    morlength = morlength - tmptext.length();
//                    ((Run) tmpnode).setText("");
//                }
//            }
//        }
//        if (position > 0) {
//            ((Run) newNode).setText(text.substring(0, position));
//            currentNode.getParentNode().insertBefore(newNode, currentNode);
//        }
//    }

}
