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

import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.VerticalAlign;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

import java.math.BigInteger;

public class Style {
    /**
     * 居中格式
     */
    private ParagraphAlignment alignment;
    /**
     * 表格上下居中，无法在run设置
     */
    //    private XWPFTableCell.XWPFVertAlign verticalAlignment;
    /**
     * 段前间距 磅值
     */
    private Double spaceBefore;
    /**
     * 行距 磅值
     */
    private Double spaceBetween;
    /**
     * 段后间距 磅值
     */
    private Double spaceAfter;
    /**
     * 字号
     */
    private Integer fontSizeMutiply2;
    /**
     * 西文字体
     */
    private String englishFont;
    /**
     * 中文字体
     */
    private String chineseFont;
    /**
     * 颜色
     */
    private POIColor color;
    /**
     * 首行缩进
     */
    private Integer indentationFirstLine;
    /**
     * 悬挂缩进
     */
    private Integer hang;
    /**
     * 加粗
     * true加粗
     * false不加粗
     */
    private boolean bold;
    /**
     * 上下标控制
     * VerticalAlign.BASELINE:      正常;
     * VerticalAlign.SUPERSCRIPT:   上标;
     * VerticalAlign.SUBSCRIPT:     下标.
     */
    private VerticalAlign verticalAlign;
    public static Style TITLE_STYLE = new Style(
            ParagraphAlignment.LEFT,
            0d,
            1d,
            0d,
            32,
            "Times New Roman",
            "宋体",
            POIColor.BLUE,
            VerticalAlign.BASELINE,
            0, null, true);
    public static Style WARNING_TITLE_STYLE = new Style(
            ParagraphAlignment.CENTER,
            0d,
            1d,
            0d,
            32,
            "Times New Roman",
            "宋体",
            POIColor.RED,
            VerticalAlign.BASELINE,
            0, null, true);
    public static final Style DEFAULT_PARA_STYLE = new Style(
            //BOTH为分散对齐
            ParagraphAlignment.BOTH,
            0.5,
            1.5d,
            0.5,
            21,
            "Times New Roman",
            "宋体",
            POIColor.BLUE,
            VerticalAlign.BASELINE,
            420, null, false);
    public static final Style DEFAULT_PARA_LIGHT_STYLE = new Style(
            ParagraphAlignment.BOTH,
            0.5,
            1.5d,
            0.5,
            21,
            "Times New Roman",
            "宋体",
            POIColor.RED,
            VerticalAlign.BASELINE,
            420, null, false);

    public static final Style DEFAULT_TABLE_CONTENT_STYLE = new Style(
            ParagraphAlignment.CENTER,
            0d,
            1.5d,
            0d,
            16,
            "Times New Roman",
            "宋体",
            POIColor.BLUE,
            VerticalAlign.BASELINE,
            null, null, false);

    public static final Style ELEC_TABLE_CONTENT_STYLE = new Style(
            ParagraphAlignment.CENTER,
            0.5,
            1.5d,
            0.5,
            18,
            "Times New Roman",
            "宋体",
            POIColor.BLUE,
            VerticalAlign.BASELINE,
            null, null, false);
    public static final Style REVIEW_TABLE_CONTENT_STYLE = new Style(
            ParagraphAlignment.CENTER,
            null,
            1.5d,
            null,
            18,
            "宋体",
            "宋体",
            POIColor.BLUE,
            VerticalAlign.BASELINE,
            null, null, false);
    public static final Style WARNING_CONTENT_STYLE = new Style(
            ParagraphAlignment.CENTER,
            0.5,
            1.5d,
            0.5,
            18,
            "宋体",
            "宋体",
            POIColor.RED,
            VerticalAlign.BASELINE,
            null, null, false);
    public static final Style DEFAULT_TABLE_CONTENT_SUPER_STYLE = new Style(
            ParagraphAlignment.CENTER,
            null,
            1.5d,
            null,
            18,
            "Times New Roman",
            "宋体",
            POIColor.BLUE,
            VerticalAlign.SUPERSCRIPT,
            null, null, false);
    public static final Style DEFAULT_TABLE_CONTENT_LIGHT_STYLE = new Style(
            ParagraphAlignment.CENTER,
            0d,
            1.5d,
            0d,
            18,
            "Times New Roman",
            "宋体",
            POIColor.RED,
            VerticalAlign.BASELINE,
            null, null, false);
    public static final Style REVIEW_TABLE_CONTENT_LIGHT_STYLE = new Style(
            ParagraphAlignment.CENTER,
            null,
            1.5d,
            null,
            18,
            "宋体",
            "宋体",
            POIColor.RED,
            VerticalAlign.BASELINE,
            null, null, false);

    public static final Style DEFAULT1_TABLE_CONTENT_STYLE = new Style(
            ParagraphAlignment.CENTER,
            null,
            1.5d,
            null,
            18,
            "Times New Roman",
            "宋体",
            POIColor.BLUE,
            VerticalAlign.BASELINE,
            null, null, false);

    public static final Style DEFAULT2_TABLE_CONTENT_STYLE = new Style(
            ParagraphAlignment.CENTER,
            null,
            1.5d,
            null,
            18,
            "Times New Roman",
            "宋体",
            POIColor.RED,
            VerticalAlign.BASELINE,
            null, null, false);
    public static final Style DEFAULT_TABLE_CONTENT_LIGHT_SUPER_STYLE = new Style(
            ParagraphAlignment.CENTER,
            null,
            1.5d,
            null,
            18,
            "Times New Roman",
            "宋体",
            POIColor.RED,
            VerticalAlign.SUPERSCRIPT,
            null, null, false);
    public static final Style DEFAULT_TABLE_PIC_NAME_STYLE = new Style(
            ParagraphAlignment.CENTER,
            0.5d,
            1.5d,
            0.5d,
            18,
            "Times New Roman",
            "宋体",
            POIColor.BLUE,
            VerticalAlign.BASELINE,
            null, null, false);
    public static final Style REVIEW_TABLE_PIC_NAME_STYLE = new Style(
            ParagraphAlignment.CENTER,
            0.5d,
            1.5d,
            0.5d,
            18,
            "宋体",
            "宋体",
            POIColor.BLUE,
            VerticalAlign.BASELINE,
            null, null, false);
    public static final Style BLACK_FONT_TABLE_PIC_NAME_STYLE = new Style(
            ParagraphAlignment.CENTER,
            0.5d,
            1.5d,
            0.5d,
            21,
            "Times New Roman",
            "黑体",
            POIColor.BLUE,
            VerticalAlign.BASELINE,
            null, null, false);
    public static final Style TABLE_COMMENT = new Style(
            ParagraphAlignment.LEFT,
            0d,
            1d,
            0d,
            18,
            "Times New Roman",
            "宋体",
            POIColor.BLUE,
            VerticalAlign.BASELINE,
            null, null, false);

    private Style() {
    }


    public Style(ParagraphAlignment alignment,
                 Double spaceBefore,
                 Double spaceBetween,
                 Double spaceAfter,
                 Integer fontSizeMutiply2,
                 String englishFont,
                 String chineseFont,
                 POIColor color,
                 VerticalAlign verticalAlign,
                 Integer indentationFirstLine,
                 Integer hang,
                 boolean bold) {

        this.alignment = alignment;
        this.spaceBefore = spaceBefore;
        this.spaceBetween = spaceBetween;
        this.spaceAfter = spaceAfter;
        this.fontSizeMutiply2 = fontSizeMutiply2;
        this.englishFont = englishFont;
        this.chineseFont = chineseFont;
        this.color = color;
        this.verticalAlign = verticalAlign;
        this.indentationFirstLine = indentationFirstLine;
        this.hang = hang;
        this.bold = bold;
    }

    public Integer getHang() {
        return hang;
    }

    public void setHang(Integer hang) {
        this.hang = hang;
    }

    public Integer getIndentationFirstLine() {
        return indentationFirstLine;
    }

    public void setIndentationFirstLine(int indentationFirstLine) {
        this.indentationFirstLine = indentationFirstLine;
    }

    public VerticalAlign getVerticalAlign() {
        return verticalAlign;
    }

    public void setVerticalAlign(VerticalAlign verticalAlign) {
        this.verticalAlign = verticalAlign;
    }

    public void setSpaceBetween(Double spaceBetween) {
        this.spaceBetween = spaceBetween;
    }

    public void setFontSizeMutiply2(Integer fontSizeMutiply2) {
        this.fontSizeMutiply2 = fontSizeMutiply2;
    }

    public ParagraphAlignment getAlignment() {
        return alignment;
    }

    public void setAlignment(ParagraphAlignment alignment) {
        this.alignment = alignment;
    }

    public Double getSpaceBetween() {
        return spaceBetween;
    }

    public int getFontSizeMutiply2() {
        return fontSizeMutiply2;
    }

    public void setFontSizeMutiply2(int fontSizeMutiply2) {
        this.fontSizeMutiply2 = fontSizeMutiply2;
    }

    public String getEnglishFont() {
        return englishFont;
    }

    public void setEnglishFont(String englishFont) {
        this.englishFont = englishFont;
    }

    public String getChineseFont() {
        return chineseFont;
    }

    public void setChineseFont(String chineseFont) {
        this.chineseFont = chineseFont;
    }

    public POIColor getColor() {
        return color;
    }

    public void setColor(POIColor color) {
        this.color = color;
    }

    public Double getSpaceBefore() {
        return spaceBefore;
    }

    public void setSpaceBefore(Double spaceBefore) {
        this.spaceBefore = spaceBefore;
    }

    public Double getSpaceAfter() {
        return spaceAfter;
    }

    public void setSpaceAfter(Double spaceAfter) {
        this.spaceAfter = spaceAfter;
    }

    public void setIndentationFirstLine(Integer indentationFirstLine) {
        this.indentationFirstLine = indentationFirstLine;
    }

    public void proccess(XWPFRun run) {
        if (alignment != null) {
            //skip
        }
        if (spaceBetween != null) {
            //skip
        }
        if (fontSizeMutiply2 != null) {
            run.setFontSize(9);
            run.getCTR().getRPr().getSz().setVal(new BigInteger(fontSizeMutiply2 + ""));
        }
        if (verticalAlign != null) {
            run.setSubscript(verticalAlign);
        }
        run.setBold(bold);

        //给字体赋值，避免设置字体时出现null
        run.setFontFamily("Times New Roman");
        run.getCTR().getRPr().getRFonts().setAscii(englishFont == null ? "Times New Roman" : englishFont);
        run.getCTR().getRPr().getRFonts().setEastAsia(chineseFont == null ? "宋体" : chineseFont);
        if (color != null) {
            run.setColor(color.toString());
        }
    }

    /**
     * 对于段落的处理总共有如下几种格式：
     * 居中类型：
     * 段前间距：word中为磅值
     * 行距：word中为倍数
     * 段后间距：word中为磅值
     * 首行缩进：
     *
     * @param paragraph
     */
    public void proccess(XWPFParagraph paragraph) {
        if (alignment != null) {
            paragraph.setAlignment(alignment);
        }
        if (spaceBefore != null) {
            int value = (int) (spaceBefore / 0.05);
            //按照行来设置的优先级更高，如果不设置为0，则磅值会被覆盖
            paragraph.setSpacingBeforeLines(0);
            paragraph.setSpacingBefore(value);
        }
        if (spaceBetween != null) {
            paragraph.setSpacingBetween(spaceBetween);
        }
        if (spaceAfter != null) {
            int value = (int) (spaceAfter / 0.05);
            //按照行来设置的优先级更高，如果不设置为0，则磅值会被覆盖，如果设置afterLines为80，则代表0.8行。
            paragraph.setSpacingAfterLines(0);
            paragraph.setSpacingAfter(value);
        }

        if (indentationFirstLine != null) {
            paragraph.setIndentationFirstLine(indentationFirstLine);
        } else if (hang != null) {
            paragraph.setIndentationHanging(hang);
        } else {
            paragraph.setIndentationFirstLine(0);
        }

        for (XWPFRun run : paragraph.getRuns()) {
            this.proccess(run);
        }
    }


}
