package cn.wise.sc.energy.power.plant.business.utils.poi;


import cn.wise.sc.energy.power.plant.business.utils.poi.styles.POIColor;
import cn.wise.sc.energy.power.plant.business.utils.poi.styles.Style;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFFieldRun;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.xmlbeans.XmlCursor;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSimpleField;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;

/**
 * @author qilei
 * @since 2018-03-20 09:56
 */
public class WordUtil {

    public static void insert2PicturesAndDescribeAfter(XWPFDocument docx, XWPFParagraph currentParagraph, InputStream pic1, InputStream pic2, String picDescribe) throws IOException, InvalidFormatException {
        XmlCursor xmlCursor = currentParagraph.getCTP().newCursor();
        xmlCursor.toNextSibling();
        final XWPFParagraph paragraph = docx.insertNewParagraph(xmlCursor);
        paragraph.setAlignment(ParagraphAlignment.CENTER);
        final XWPFRun run1 = paragraph.createRun();
        run1.addPicture(pic1, XWPFDocument.PICTURE_TYPE_PNG, "", Units.toEMU(205.208), Units.toEMU(153.906));
        run1.addPicture(pic2, XWPFDocument.PICTURE_TYPE_PNG, "", Units.toEMU(205.208), Units.toEMU(153.906));
        final XmlCursor xmlCursor1 = paragraph.getCTP().newCursor();
        xmlCursor1.toNextSibling();
        final XWPFParagraph paragraph1 = docx.insertNewParagraph(xmlCursor1);
        paragraph1.createRun().setText(picDescribe);
        Style.DEFAULT_TABLE_PIC_NAME_STYLE.proccess(paragraph1);
    }

    public static XWPFParagraph insertPictureAndDescribeAfter(XWPFDocument docx, XWPFParagraph currentParagraph, InputStream picStream, String picDescribe) throws IOException, InvalidFormatException {
        XmlCursor xmlCursor = currentParagraph.getCTP().newCursor();
        xmlCursor.toNextSibling();
        final XWPFParagraph paragraph = docx.insertNewParagraph(xmlCursor);
        paragraph.setAlignment(ParagraphAlignment.CENTER);
        final XWPFRun run = paragraph.createRun();
        run.addPicture(picStream, XWPFDocument.PICTURE_TYPE_PNG, "", Units.toEMU(320), Units.toEMU(240));
        final XmlCursor xmlCursor1 = paragraph.getCTP().newCursor();
        xmlCursor1.toNextSibling();
        final XWPFParagraph paragraph1 = docx.insertNewParagraph(xmlCursor1);
        final XWPFRun run1 = paragraph1.createRun();
        run1.setText(picDescribe);
        Style.DEFAULT_TABLE_PIC_NAME_STYLE.proccess(paragraph1);
        return paragraph1;
    }

    /**
     * 仅插入图片 不做描述
     *
     * @param docx
     * @param currentParagraph
     * @param picStream
     * @return
     * @throws IOException
     * @throws InvalidFormatException
     */
    public static XWPFParagraph insertPicture(XWPFDocument docx, XWPFParagraph currentParagraph, InputStream picStream) throws IOException, InvalidFormatException {
        XmlCursor xmlCursor = currentParagraph.getCTP().newCursor();
        xmlCursor.toNextSibling();
        final XWPFParagraph paragraph = docx.insertNewParagraph(xmlCursor);
        paragraph.setAlignment(ParagraphAlignment.CENTER);
        final XWPFRun run = paragraph.createRun();
        run.addPicture(picStream, XWPFDocument.PICTURE_TYPE_PNG, "", Units.toEMU(320), Units.toEMU(240));
        return paragraph;
    }

    public static XWPFParagraph insert2Pictures(XWPFDocument docx, XWPFParagraph currentParagraph, InputStream pic1, InputStream pic2) throws IOException, InvalidFormatException {
        XmlCursor xmlCursor = currentParagraph.getCTP().newCursor();
        xmlCursor.toNextSibling();
        final XWPFParagraph paragraph = docx.insertNewParagraph(xmlCursor);
        paragraph.setAlignment(ParagraphAlignment.CENTER);
        final XWPFRun run1 = paragraph.createRun();
        run1.addPicture(pic1, XWPFDocument.PICTURE_TYPE_PNG, "", Units.toEMU(205.208), Units.toEMU(153.906));
        run1.addPicture(pic2, XWPFDocument.PICTURE_TYPE_PNG, "", Units.toEMU(205.208), Units.toEMU(153.906));
        return paragraph;
    }

    public static XWPFParagraph insertParagraphAfter(XWPFDocument docx, XWPFParagraph currentParagraph) {
        XmlCursor xmlCursor = currentParagraph.getCTP().newCursor();
        xmlCursor.toNextSibling();
        return docx.insertNewParagraph(xmlCursor);
    }

    public static XWPFParagraph insertParagraphAfter(XWPFDocument docx, XmlCursor xmlCursor) {
        xmlCursor.toNextSibling();
        return docx.insertNewParagraph(xmlCursor);
    }

    public static XWPFParagraph insertParagraphAfter(XWPFDocument docx, XWPFTable xwpfTable) {
        XmlCursor xmlCursor = xwpfTable.getCTTbl().newCursor();
        xmlCursor.toNextSibling();
        return docx.insertNewParagraph(xmlCursor);
    }

    public static XWPFTable insertTableAfter(XWPFDocument docx, XWPFParagraph currentParagraph) {
        XmlCursor xmlCursor = currentParagraph.getCTP().newCursor();
        xmlCursor.toNextSibling();
        return docx.insertNewTbl(xmlCursor);
    }

    public static XWPFTable insertTableAfter(XWPFDocument docx, XWPFTable xwpfTable) {
        XmlCursor xmlCursor = xwpfTable.getCTTbl().newCursor();
        xmlCursor.toNextSibling();
        return docx.insertNewTbl(xmlCursor);
    }

    /**
     * 移除paragraph中所有的run，不保留run
     *
     * @param paragraph
     */
    public static void clearMe(XWPFParagraph paragraph) {
        for (int i = paragraph.getRuns().size(); i > 0; i--) {
            paragraph.removeRun(0);
        }
    }

    /**
     * 把自己移除
     *
     * @param paragraph
     */
    public static void removeMe(XWPFParagraph paragraph) {
        final XWPFDocument document = paragraph.getDocument();
    }

    /**
     * 简单的设置字体和字号，为run设置
     *
     * @param run
     * @param fontSizeMutiply2 字号*2 比如要设置10.5号字，就传入21
     * @param englishFont      Nullable
     * @param chineseFont      Nullable
     */
    public static void setFontAndSize(XWPFRun run, int fontSizeMutiply2, String englishFont, String chineseFont) {
        //todo set color
        run.setColor(POIColor.BLUE.toString());
        //给字体赋值，避免设置字体时出现null
        run.setFontFamily("Times New Roman");
        //给字号赋值，避免设置字号时出现null
        run.setFontSize(9);
        //这地方 因为fontSize不存在小数点，所以很蛋疼，这里的BigInteger是fontSize*2，看源码即可知道。
        run.getCTR().getRPr().getSz().setVal(new BigInteger(fontSizeMutiply2 + ""));
        //设置英文字体
        run.getCTR().getRPr().getRFonts().setAscii(englishFont == null ? "Times New Roman" : englishFont);
        //设置中文字体
        run.getCTR().getRPr().getRFonts().setEastAsia(chineseFont == null ? "宋体" : chineseFont);
    }
}
