package com.tuoheng.admin.utils;

import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.*;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.rtf.RtfWriter2;
import com.lowagie.text.rtf.style.RtfParagraphStyle;

import java.awt.*;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Map;

/**
 * word帮助文件
 */
public class WordUtils {

    private Document document;

    private String test;

    private Map<Integer, String> fontFamily = new HashMap<Integer, String>();


    public Document getDocument() {
        return document;
    }

    public void setDocument(Document document) {
        this.document = document;
    }

    public WordUtils() {
        this.document = new Document(PageSize.A4);//����ֽ�Ŵ�С
        document.newPage();
        //init font
        if (getSystem()) {
            fontFamily.put(1, "C:\\Windows\\Fonts\\simhei.ttf");
            fontFamily.put(2, "C:\\Windows\\Fonts\\STXINWEI.TTF");
            fontFamily.put(3, "C:\\Windows\\Fonts\\simsun.ttc,0");
            fontFamily.put(4, "C:\\Windows\\Fonts\\fangsong_GB2312.ttf");
            fontFamily.put(5, "C:\\Windows\\Fonts\\times.ttf");
            fontFamily.put(6, "C:\\Windows\\Fonts\\simfang.ttf");
        } else {
            fontFamily.put(1, "/usr/share/fonts/simhei.ttf");
            fontFamily.put(2, "/usr/share/fonts/STXINWEI.TTF");
            fontFamily.put(3, "/usr/share/fonts/windows/simsun.ttc,0");
            fontFamily.put(4, "/usr/share/fonts/fangsong_GB2312.ttf");
            fontFamily.put(5, "/usr/share/fonts/times.ttf");
        }
    }

    /**
     * 建立一个书写器(Writer)与document对象关联，通过书写器(Writer)可以将文档写入到磁盘中
     *
     * @param filePath 要操作的文档路径，若文档不存在会自动创建
     * @throws com.lowagie.text.DocumentException
     * @throws IOException
     */
    public void openDocument(String filePath) throws DocumentException,
            IOException {
//      建立一个书写器(Writer)与document对象关联，通过书写器(Writer)可以将文档写入到磁盘中
        RtfWriter2.getInstance(this.document, new FileOutputStream(filePath));
        this.document.open();
//      设置中文字体
       /*this.bfChinese = BaseFont.createFont("STSongStd-Light",
               "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);*/
    }

    /**
     * @param titleStr     标题
     * @param fontsize     字体大小
     * @param fontStyle    字体样式
     * @param elementAlign 对齐方式
     * @throws com.lowagie.text.DocumentException
     */
    public void insertTitle(String titleStr, float fontsize, int fontStyle, int elementAlign, float before, float after, int fontFamliyType) throws Exception {
        BaseFont fontFamliy = BaseFont.createFont(fontFamily.get(3), "GBK", BaseFont.NOT_EMBEDDED);
        Font titleFont = new Font(fontFamliy, fontsize, fontStyle);
        Paragraph title = new Paragraph(titleStr);
        // 设置标题格式对齐方式
        title.setAlignment(elementAlign);
        title.setFont(titleFont);
        title.setSpacingBefore(before);
        title.setSpacingAfter(after);

        this.document.add(title);
    }


    /**
     * 设置带有目录格式的标题(标题1格式)
     *
     * @param rtfParagraphStyle 标题1样式
     * @param titleStr          标题
     * @throws com.lowagie.text.DocumentException
     * @throws IOException
     */
    public void insertTitlePattern(String titleStr, RtfParagraphStyle rtfParagraphStyle) throws DocumentException, IOException {
        Paragraph title = new Paragraph(titleStr);
        title.setFont(rtfParagraphStyle);
        this.document.add(title);
    }


    /**
     * 设置带有目录格式的标题(标题2格式)
     *
     * @param titleStr          标题
     * @param rtfParagraphStyle 标题2样式
     * @throws com.lowagie.text.DocumentException
     */
    public void insertTitlePatternSecond(String titleStr, RtfParagraphStyle rtfParagraphStyle) throws DocumentException {
        Paragraph title = new Paragraph(titleStr);
        // 设置标题格式对齐方式
        title.setFont(rtfParagraphStyle);
        this.document.add(title);

    }

    /**
     * 设置带有目录格式的标题(标题3格式)
     *
     * @param titleStr          标题
     * @param rtfParagraphStyle 标题3样式
     * @throws com.lowagie.text.DocumentException
     */
    public void insertTitlePatternThird(String titleStr, RtfParagraphStyle rtfParagraphStyle) throws DocumentException {
        Paragraph title = new Paragraph(titleStr);
        // 设置标题格式对齐方式
        title.setFont(rtfParagraphStyle);
        this.document.add(title);
    }


    /**
     * @param tableName    标题
     * @param fontsize     字体大小
     * @param fontStyle    字体样式
     * @param elementAlign 对齐方式
     * @throws com.lowagie.text.DocumentException
     */
    public void insertTableName(String tableName, int fontsize, int fontStyle, int elementAlign) throws DocumentException {
        Font titleFont = new Font();
        titleFont.setColor(102, 102, 153);
        Paragraph title = new Paragraph(tableName);
        // 设置标题格式对齐方式
        title.setAlignment(elementAlign);
        title.setFont(titleFont);

        this.document.add(title);
    }

    /**
     * @param contextStr   内容
     * @param fontsize     字体大小
     * @param fontStyle    字体样式
     * @param elementAlign 对齐方式
     * @throws com.lowagie.text.DocumentException
     * @throws IOException
     */
    public void insertContext(String contextStr, int fontsize, int fontStyle, int elementAlign, float firstLineIndent, float before, float after) throws DocumentException, IOException {
        // 正文字体风格
        BaseFont fontFamliy = BaseFont.createFont(fontFamily.get(3), "GBK", BaseFont.EMBEDDED);
        Font contextFont = new Font(fontFamliy, fontsize, fontStyle);
        Paragraph context = new Paragraph(contextStr);
        //设置行距
        context.setLeading(3f);
        // 正文格式左对齐
        context.setAlignment(elementAlign);
        context.setFont(contextFont);
        // 离上一段落（标题）空的行数
        //context.setSpacingBefore(1);
        // 设置第一行空的列数
        //context.setFirstLineIndent(20);
        context.setFirstLineIndent(firstLineIndent);
        context.setSpacingBefore(before);
        context.setSpacingAfter(after);
        document.add(context);
    }

    /**
     * 插入正文里的注释
     *
     * @param contextStr   内容
     * @param fontsize     字体大小    5号 10.5
     * @param fontStyle    字体样式
     * @param elementAlign 对齐方式
     * @throws com.lowagie.text.DocumentException
     */
    public void insertContextNote(String contextStr, int fontsize, int fontStyle, int elementAlign, float firstLineIndent, float before, float after) throws Exception {

        BaseFont fontFamliy = BaseFont.createFont(fontFamily.get(3), "GBK", BaseFont.EMBEDDED);
        Font contextFont = new Font(fontFamliy, 10.5f, fontStyle);
        Paragraph context = new Paragraph(contextStr);
        context.setLeading(3f);
        context.setAlignment(elementAlign);
        context.setFont(contextFont);
        context.setFirstLineIndent(firstLineIndent);
        context.setSpacingBefore(before);
        context.setSpacingAfter(after);
        document.add(context);
    }


    public void insertContext(Paragraph context) throws Exception {
        BaseFont fontFamliy = BaseFont.createFont(fontFamily.get(3), "GBK", BaseFont.EMBEDDED);
        Font contextFont = new Font(fontFamliy, 12);
        context.setFont(contextFont);
        document.add(context);
    }

    /**
     * @param imgUrl        图片路径
     * @param imageAlign    显示位置
     * @param height        显示高度
     * @param weight        显示宽度
     * @param percent       显示比例
     * @param heightPercent 显示高度比例
     * @param weightPercent 显示宽度比例
     * @param rotation      显示图片旋转角度
     * @throws MalformedURLException
     * @throws IOException
     * @throws com.lowagie.text.DocumentException
     */
    public void insertImg(String imgUrl, int imageAlign, int height, int weight, int percent, int heightPercent, int weightPercent, int rotation) throws MalformedURLException, IOException, DocumentException {
//      添加图片
        Image img = Image.getInstance(imgUrl);
        if (img == null)
            return;
        img.setAbsolutePosition(0, 0);
        img.setAlignment(imageAlign);
        img.scaleAbsolute(height, weight);
        img.scaleAbsolute(1000, 1000);
        img.scalePercent(percent);
        img.scalePercent(heightPercent, weightPercent);
        img.setRotation(rotation);
        document.add(img);
    }

    /**
     * 添加简单表格
     *
     * @param column 表格列数(必须)
     * @param row    表格行数
     * @throws com.lowagie.text.DocumentException
     */
    public void insertSimpleTable(int column, int row) throws DocumentException {
        Table table = new Table(column);//列数必须设置，而行数则可以按照个人要求来决定是否需要设置
        table.setAlignment(Element.ALIGN_CENTER);// 居中显示
        table.setAlignment(Element.ALIGN_MIDDLE);// 纵向居中显示
        table.setAutoFillEmptyCells(true);// 自动填满
        table.setBorderColor(new Color(0, 125, 255));// 边框颜色
        table.setBorderWidth(1);// 边框宽度
        table.setSpacing(2);// 衬距，
        table.setPadding(2);// 即单元格之间的间距
        table.setBorder(20);// 边框
        for (int i = 0; i < column * 3; i++) {
            table.addCell(new Cell("" + i));
        }
        document.add(table);
    }

    public void insertContext2(String contextStr, int fontsize, int fontStyle, int elementAlign, float firstLineIndent, float hangju, float before, float after, int fontFamliyType) throws DocumentException, IOException {
        BaseFont fontFamliy = BaseFont.createFont(fontFamily.get(fontFamliyType), "GBK", BaseFont.EMBEDDED);
        Font contextFont = new Font(fontFamliy, fontsize, fontStyle);
        Paragraph context = new Paragraph(contextStr);
        //设置行距
        context.setLeading(hangju);
        // 正文格式左对齐
        context.setAlignment(elementAlign);
        context.setFont(contextFont);
        context.setFirstLineIndent(firstLineIndent);
        context.setSpacingBefore(before);
        context.setSpacingAfter(after);
        document.add(context);
    }

    /**
     * 新增
     * hxl 2019-3-29
     * 设置字体
     *
     * @param fontFamliyType
     * @return
     * @throws Exception
     */
    public BaseFont setFont(int fontFamliyType) throws Exception {
        BaseFont fontFamliy = BaseFont.createFont(fontFamily.get(3), "GBK", BaseFont.NOT_EMBEDDED);
        return fontFamliy;
    }


    /**
     * 在操作完成后必须关闭document,否则即使生成了word文档，打开也会发生错误
     *
     * @throws com.lowagie.text.DocumentException
     */
    public void closeDocument() throws DocumentException {
        this.document.close();
    }

    //判断操作系统类型
    public boolean getSystem() {
        if (System.getProperties().getProperty("os.name").toUpperCase().indexOf("WINDOWS") != -1) {
            return true;
        }
        return false;
    }

    public Map<Integer, String> getFontFamily() {
        return fontFamily;
    }

    public void setFontFamily(Map<Integer, String> fontFamily) {
        this.fontFamily = fontFamily;
    }

    //第一级标题样式
    public static RtfParagraphStyle rtfGsBt1 = RtfParagraphStyle.STYLE_HEADING_1;


    //第二级标题样式
    public static RtfParagraphStyle rtfGsBt2 = RtfParagraphStyle.STYLE_HEADING_2;


    //第三级标题样式
    public static RtfParagraphStyle rtfGsBt3 = RtfParagraphStyle.STYLE_HEADING_3;


    //第四级标题样式
    public static RtfParagraphStyle rtfGsBt4 = RtfParagraphStyle.STYLE_HEADING_3;

    static {
        rtfGsBt1.setAlignment(Element.ALIGN_CENTER);
        rtfGsBt1.setStyle(Font.BOLD);
        rtfGsBt1.setSize(22);
        rtfGsBt1.setSpacingBefore(10);
        rtfGsBt1.setSpacingAfter(10);
        rtfGsBt1.setFontName("黑体");

        rtfGsBt2.setAlignment(Element.ALIGN_CENTER);
        rtfGsBt2.setStyle(Font.BOLD);
        rtfGsBt2.setSize(14);
        rtfGsBt2.setSpacingBefore(10);
        rtfGsBt2.setSpacingAfter(10);
        rtfGsBt2.setKeepTogether(true);
        rtfGsBt2.setKeepTogetherWithNext(true);
        rtfGsBt2.setFontName("黑体");

        rtfGsBt3.setAlignment(Element.ALIGN_LEFT);
        rtfGsBt3.setStyle(Font.BOLD);
        rtfGsBt3.setSize(14);
        rtfGsBt3.setSpacingBefore(10);
        rtfGsBt3.setSpacingAfter(0);
        rtfGsBt3.setKeepTogether(true);
        //rtfGsBt3.setKeepTogetherWithNext(true);
        rtfGsBt3.setFontName("FangSong_GB2312");

        rtfGsBt4.setAlignment(Element.ALIGN_LEFT);
        rtfGsBt4.setStyle(Font.BOLD);
        rtfGsBt4.setSize(14);
        rtfGsBt4.setSpacingBefore(10);
        rtfGsBt4.setSpacingAfter(0);
        rtfGsBt4.setKeepTogether(true);
        rtfGsBt4.setKeepTogetherWithNext(true);
        rtfGsBt4.setFontName("FangSong_GB2312");
    }
}
