package com.njcn.utils;

import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

import java.math.BigInteger;

public class WordUtils {

    /**
     * 设置段落格式
     * @param paragraph 段落对象
     */
    public static void  setParagraphStyle(XWPFParagraph paragraph){
            paragraph.setSpacingBefore(100);
            paragraph.setSpacingAfter(100);
    }

    /**                                                                         
     * 返回指定格式的段落 居中型
     * @param document 文档对象
     */
    public static XWPFParagraph  getCenterParagraph(XWPFDocument document){
    	XWPFParagraph paragraph = document.createParagraph();
    	setParagraphStyle(paragraph);
    	paragraph.setAlignment(ParagraphAlignment.CENTER);
    	paragraph.setVerticalAlignment(TextAlignment.CENTER);
    	return paragraph;
    }
    
    /**                                                                         
     * 返回指定格式的段落 居左型
     * @param document 文档对象
     */
    public static XWPFParagraph  getLeftParagraph(XWPFDocument document){
    	XWPFParagraph paragraph = document.createParagraph();
    	setParagraphStyle(paragraph);
    	paragraph.setAlignment(ParagraphAlignment.LEFT);
    	return paragraph;
    }

    

    /**
     * 添加换行符
     * @param paragraph 指定段落
     * @param amount    行数
     * */
    public static void  addLine(XWPFParagraph paragraph,Integer  amount){
            XWPFRun run = paragraph.createRun();
            run.setFontSize(11);
            for(int i=0;i<amount;i++){
                run.addCarriageReturn();
            }
    }


    /**
     * 添加段落文本
     * @param run 文本执行对象
     * @param fontFamily  字体类型
     * @param fontSize     字体大小
     * @param backgroundColor  字体颜色
     * @param bold  是否加粗
     */
    public static void  addParagraph(XWPFRun run, String fontFamily, Integer fontSize, String backgroundColor, String message, boolean bold){
            run.setText(message);
            run.setColor(backgroundColor);
            run.setFontSize(fontSize);
            run.setFontFamily(fontFamily);
            run.setBold(bold);
    }


    /**
     * 增加自定义标题样式。这里用的是stackoverflow的源码
     *
     * @param docxDocument 目标文档
     * @param strStyleId 样式名称
     * @param headingLevel 样式级别
     */
    public static void addCustomHeadingStyle(XWPFDocument docxDocument, String strStyleId, int headingLevel) {

        CTStyle ctStyle = CTStyle.Factory.newInstance();
        ctStyle.setStyleId(strStyleId);

        CTString styleName = CTString.Factory.newInstance();
        styleName.setVal(strStyleId);
        ctStyle.setName(styleName);

        CTDecimalNumber indentNumber = CTDecimalNumber.Factory.newInstance();
        indentNumber.setVal(BigInteger.valueOf(headingLevel));

        // lower number > style is more prominent in the formats bar
        ctStyle.setUiPriority(indentNumber);

        CTOnOff onoffnull = CTOnOff.Factory.newInstance();
        ctStyle.setUnhideWhenUsed(onoffnull);

        // style shows up in the formats bar
        ctStyle.setQFormat(onoffnull);

        // style defines a heading of the given level
        CTPPr ppr = CTPPr.Factory.newInstance();
        ppr.setOutlineLvl(indentNumber);
        ctStyle.setPPr(ppr);

        XWPFStyle style = new XWPFStyle(ctStyle);

        // is a null op if already defined
        XWPFStyles styles = docxDocument.createStyles();

        style.setType(STStyleType.PARAGRAPH);
        styles.addStyle(style);

    }

    /**
     * 设置文档中标题格式
     * */
    public static void setHeadingStyle(XWPFDocument document){
        addCustomHeadingStyle(document, "标题 1", 1);
        addCustomHeadingStyle(document, "标题 2", 2);
        addCustomHeadingStyle(document, "标题 3", 3);
        addCustomHeadingStyle(document, "标题 4", 4);
        addCustomHeadingStyle(document, "标题 5", 5);
        addCustomHeadingStyle(document, "标题 6", 6);
        addCustomHeadingStyle(document, "标题 7", 7);
    }

    /**
     * 给表格添加一行数据
     * @param paragraph 段落对象
     * @param row 行对象
     * @param data 不定长度的数据
     */
    public static void  setExcelContent(XWPFParagraph paragraph, XWPFTableRow  row,String...  data){
                for (int i=0;i<data.length;i++){
                    XWPFRun run =paragraph.createRun();
                    run.setFontFamily("宋体");
                    run.setText(data[i]);
                    row.getCell(i).setParagraph(paragraph);
                    paragraph.removeRun(0);
                }
    }



    /**
     * 添加表头标题一行数据
     * @param paragraph 段落对象
     * @param row 行对象
     * @param data 不定长度的数据
     */
    public static void  setExcelHeadContent(XWPFParagraph paragraph, XWPFTableRow  row,String...  data){
                XWPFRun run= paragraph.createRun();
                run.setFontFamily("宋体");
                run.setBold(true);
                run.setText(data[0]);
                row.getCell(0).setParagraph(paragraph);
                paragraph.removeRun(0);
                for(int i=1;i<data.length;i++){
                    XWPFRun run1 =paragraph.createRun();
                    run1.setFontFamily("宋体");
                    run1.setBold(true);
                    run1.setText(data[i]);
                    row.addNewTableCell().setParagraph(paragraph);
                    paragraph.removeRun(0);
                }
    }



}
