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


import cn.wise.sc.energy.power.plant.business.constant.Constance;
import cn.wise.sc.energy.power.plant.business.utils.poi.styles.Style;
import cn.wise.sc.energy.power.plant.business.utils.poi.xwpftable.ToolXWPFCell;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.TextAlignment;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;

import java.math.BigInteger;

/**
 * @author qilei
 * @since 2018-04-09 11:51
 */
public class TableUtil {

    /**
     * table必须是新的，也就是只有一行一列，否则会出现格式错乱。
     *
     * @param table
     * @param headers
     */
    @Deprecated
    public static void buildHeader(XWPFTable table, String... headers) {
        final XWPFParagraph xwpfParagraph1 = table.getRow(0).getCell(0).getParagraphs().get(0);
        xwpfParagraph1.setAlignment(ParagraphAlignment.CENTER);
        xwpfParagraph1.setSpacingBetween(1);
        final XWPFRun run1 = xwpfParagraph1.createRun();
        run1.setText(headers[0]);
        WordUtil.setFontAndSize(run1, 18, null, null);
        for (int i = 1; i < headers.length; i++) {
            final XWPFParagraph xwpfParagraph = table.getRow(0).createCell().getParagraphs().get(0);
            xwpfParagraph.setAlignment(ParagraphAlignment.CENTER);
            xwpfParagraph.setSpacingBetween(1);
            xwpfParagraph.setVerticalAlignment(TextAlignment.CENTER);
            final XWPFRun run = xwpfParagraph.createRun();
            run.setText(headers[i]);
            WordUtil.setFontAndSize(run, 18, null, null);
        }
    }

    @Deprecated
    public static void appendContent(XWPFTable table, String... contentLine) {
        XWPFTableRow row = table.createRow();
        for (int i = 0; i < contentLine.length; i++) {
            final XWPFParagraph xwpfParagraph = row.getCell(i).getParagraphs().get(0);
            xwpfParagraph.setAlignment(ParagraphAlignment.CENTER);
            xwpfParagraph.setSpacingBetween(1);
            final XWPFRun run = xwpfParagraph.createRun();
            run.setText(contentLine[i]);
            WordUtil.setFontAndSize(run, 18, null, null);
        }
    }

    @Deprecated
    public static void buildeColumnHeader(XWPFTable table, String... headers) {
        final XWPFParagraph xwpfParagraph1 = table.getRow(0).getCell(0).getParagraphs().get(0);
        xwpfParagraph1.setAlignment(ParagraphAlignment.CENTER);
        xwpfParagraph1.setSpacingBetween(1);
        final XWPFRun run1 = xwpfParagraph1.createRun();
        run1.setText(headers[0]);
        WordUtil.setFontAndSize(run1, 18, null, null);
        for (int i = 1; i < headers.length; i++) {
            final XWPFParagraph xwpfParagraph = table.createRow().getCell(0).getParagraphs().get(0);
            xwpfParagraph.setAlignment(ParagraphAlignment.CENTER);
            xwpfParagraph.setSpacingBetween(1);
            final XWPFRun run = xwpfParagraph.createRun();
            run.setText(headers[i]);
            WordUtil.setFontAndSize(run, 18, null, null);
        }
    }

    @Deprecated
    public static void appendColumnContent(XWPFTable table, String... contentLine) {
        for (int i = 0; i < contentLine.length; i++) {
            final XWPFParagraph xwpfParagraph = table.getRow(i).createCell().getParagraphs().get(0);
            xwpfParagraph.setAlignment(ParagraphAlignment.CENTER);
            xwpfParagraph.setSpacingBetween(1);
            final XWPFRun run = xwpfParagraph.createRun();
            run.setText(contentLine[i]);
            WordUtil.setFontAndSize(run, 18, null, null);
        }
    }

    /**
     * 对于表格组件的使用，主要需求有按照行列去构建一个table，除此之外，对于每个单元格的样式也是有要求的。在控制样式的同时，要对行高和列宽同时控制，此时需要能够联动的去扩展。
     */

    /**
     * table必须是新的，也就是只有一行一列，否则会出现格式错乱。
     *
     * @param table
     * @param cells
     */
    public static void buildRowHeader(XWPFTable table, ToolXWPFCell... cells) {
        if (cells.length == 0) {//todo
            return;
        }
        final XWPFTableCell cell0 = table.getRow(0).getCell(0);
        final XWPFParagraph xwpfParagraph1 = cell0.getParagraphs().get(0);
        Style.DEFAULT_TABLE_CONTENT_STYLE.proccess(xwpfParagraph1);
        //第一个单元格。
        for (int i = 0; i < cells[0].getRuns().size(); i++) {
            //处理首行换行符
            final String[] contents = cells[0].getRuns().get(i).getContent().split(Constance.RETURN);
            for (int k = 0; k < contents.length; k++) {
                final XWPFRun run = xwpfParagraph1.createRun();
                run.setText(contents[k]);
                if (k + 1 < contents.length) {
                    run.addBreak();
                }
                cells[0].getRuns().get(i).getStyle().proccess(run);
            }
        }
        //trick 居中问题
        cell0.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
        //下面的单元格
        for (int i = 1; i < cells.length; i++) {
            final XWPFTableCell cell = table.getRow(0).createCell();
            final XWPFParagraph xwpfParagraphPer = cell.getParagraphs().get(0);
            Style.DEFAULT_TABLE_CONTENT_STYLE.proccess(xwpfParagraphPer);
            for (int j = 0; j < cells[i].getRuns().size(); j++) {
                //处理换行符的
                final String[] contents = cells[i].getRuns().get(j).getContent().split(Constance.RETURN);
                for (int k = 0; k < contents.length; k++) {
                    final XWPFRun run = xwpfParagraphPer.createRun();
                    run.setText(contents[k]);
                    if (k + 1 < contents.length) {
                        run.addBreak();
                    }
                    cells[i].getRuns().get(j).getStyle().proccess(run);
                }

                //如果该行为空，就设置为continue
                if (cells[i] == ToolXWPFCell.CONTINUE) {
                    cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
                    continue;
                }
                //如果没有下一行，就直接continue
                if (i + 1 >= cells.length) {
                    continue;
                } else if (cells[i + 1] == ToolXWPFCell.CONTINUE) {
                    //如果下一个为空，就设置成RESTART
                    cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
                }
            }
            //trick 因为默认都是靠上居中，尤其横着生成的首行，就是靠上居中的 不合要求
            cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
        }
    }

    public static void buildElecRowHeader(XWPFTable table, ToolXWPFCell... cells) {
        if (cells.length == 0) {//todo
            return;
        }
        final XWPFTableCell cell0 = table.getRow(0).getCell(0);
        final XWPFParagraph xwpfParagraph1 = cell0.getParagraphs().get(0);
        Style.ELEC_TABLE_CONTENT_STYLE.proccess(xwpfParagraph1);
        //第一个单元格。
        for (int i = 0; i < cells[0].getRuns().size(); i++) {
            //处理首行换行符
            final String[] contents = cells[0].getRuns().get(i).getContent().split(Constance.RETURN);
            for (int k = 0; k < contents.length; k++) {
                final XWPFRun run = xwpfParagraph1.createRun();
                run.setText(contents[k]);
                if (k + 1 < contents.length) {
                    run.addBreak();
                }
                cells[0].getRuns().get(i).getStyle().proccess(run);
            }
        }
        //trick 居中问题
        cell0.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
        //下面的单元格
        for (int i = 1; i < cells.length; i++) {
            final XWPFTableCell cell = table.getRow(0).createCell();
            final XWPFParagraph xwpfParagraphPer = cell.getParagraphs().get(0);
            Style.ELEC_TABLE_CONTENT_STYLE.proccess(xwpfParagraphPer);
            for (int j = 0; j < cells[i].getRuns().size(); j++) {
                //处理换行符的
                final String[] contents = cells[i].getRuns().get(j).getContent().split(Constance.RETURN);
                for (int k = 0; k < contents.length; k++) {
                    final XWPFRun run = xwpfParagraphPer.createRun();
                    run.setText(contents[k]);
                    if (k + 1 < contents.length) {
                        run.addBreak();
                    }
                    cells[i].getRuns().get(j).getStyle().proccess(run);
                }
                //如果该行为空，就设置为continue
                if (cells[i] == ToolXWPFCell.CONTINUE) {
                    cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
                    continue;
                }
                //如果没有下一行，就直接continue
                if (i + 1 >= cells.length) {
                    continue;
                } else if (cells[i + 1] == ToolXWPFCell.CONTINUE) {
                    //如果下一个为空，就设置成RESTART
                    cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
                }
            }
            //trick 因为默认都是靠上居中，尤其横着生成的首行，就是靠上居中的 不合要求
            cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
        }
    }

    public static void appendRowContent(XWPFTable table, ToolXWPFCell... cells) {
        XWPFTableRow row = table.createRow();
        row.setHeight(300);
        for (int i = 0; i < cells.length; i++) {
            final XWPFTableCell cell = row.getCell(i);
            cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
            final XWPFParagraph xwpfParagraphPer = cell.getParagraphs().get(0);
            Style.DEFAULT_TABLE_CONTENT_STYLE.proccess(xwpfParagraphPer);
            for (int j = 0; j < cells[i].getRuns().size(); j++) {
                //处理换行符的
                final String[] contents = cells[i].getRuns().get(j).getContent().split(Constance.RETURN);
                for (int k = 0; k < contents.length; k++) {
                    final XWPFRun run = xwpfParagraphPer.createRun();
                    String content = contents[k];
                    run.setText(content);
                    if (k + 1 < contents.length) {
                        run.addBreak();
                    }
                    cells[i].getRuns().get(j).getStyle().proccess(run);
                }

                //如果该行为空，就设置为continue
                if (cells[i] == ToolXWPFCell.CONTINUE) {
                    cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
                    continue;
                }
                //如果没有下一行，就直接continue
                if (i + 1 >= cells.length) {
                    continue;
                } else if (cells[i + 1] == ToolXWPFCell.CONTINUE) {
                    //如果下一个为空，就设置成RESTART
                    cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
                }
            }
        }
    }

    public static void appendElecRowContent(XWPFTable table, ToolXWPFCell... cells) {
        XWPFTableRow row = table.createRow();
        for (int i = 0; i < cells.length; i++) {
            final XWPFTableCell cell = row.getCell(i);
            cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
            final XWPFParagraph xwpfParagraphPer = cell.getParagraphs().get(0);
            Style.ELEC_TABLE_CONTENT_STYLE.proccess(xwpfParagraphPer);
            for (int j = 0; j < cells[i].getRuns().size(); j++) {
                //处理换行符的
                final String[] contents = cells[i].getRuns().get(j).getContent().split(Constance.RETURN);
                for (int k = 0; k < contents.length; k++) {
                    final XWPFRun run = xwpfParagraphPer.createRun();
                    run.setText(contents[k]);
                    if (k + 1 < contents.length) {
                        run.addBreak();
                    }
                    cells[i].getRuns().get(j).getStyle().proccess(run);
                }

                //如果该行为空，就设置为continue
                if (cells[i] == ToolXWPFCell.CONTINUE) {
                    cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
                    continue;
                }
                //如果没有下一行，就直接continue
                if (i + 1 >= cells.length) {
                    continue;
                } else if (cells[i + 1] == ToolXWPFCell.CONTINUE) {
                    //如果下一个为空，就设置成RESTART
                    cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
                }
            }
        }
    }
    /**
     * 清除一个word tablecell中的内容。
     * 保留第一个Paragraph,清除其中的run。
     *
     * @param cell
     */
    public static void clearMe(XWPFTableCell cell) {
        for (int i = cell.getParagraphs().size(); i > 1; i--) {
            cell.removeParagraph(0);
        }
        WordUtil.clearMe(cell.getParagraphs().get(0));
    }
    /**
     * 对XWPFTable设置宽度，默认设置A4的宽度。
     *
     * @param table
     */
    public static void setTableWidth(XWPFTable table) {
        setTableWidth(table, POIConstance.A4_TABLE_EARLY_AND_REVIEW_WIDTH);
    }

    /**
     * 对XWPFTable设置宽度
     *
     * @param table
     * @param width
     */
    public static void setTableWidth(XWPFTable table, int width) {
        CTTblWidth ctTblWidth = table.getCTTbl().addNewTblPr().addNewTblW();
        ctTblWidth.setType(STTblWidth.DXA);
        ctTblWidth.setW(BigInteger.valueOf(width));
    }

    /**
     * 重写单元格内容，采用单元格已有的第一个样式
     *
     * @param cell
     * @param value
     */
    public static void rewriteCell(XWPFTableCell cell, String value) {
        for (XWPFParagraph xwpfParagraph : cell.getParagraphs()) {
            WordUtil.clearMe(xwpfParagraph);
        }
        final XWPFRun run = cell.getParagraphs().get(0).createRun();
        run.setText(value);
    }

    /**
     * 重写单元格内容，使用指定的样式
     *
     * @param cell
     * @param value
     */
    public static void rewriteCell(XWPFTableCell cell, String value, Style style) {
        clearMe(cell);
        final XWPFRun run = cell.addParagraph().createRun();
        run.setText(value);
        style.proccess(run);
    }
}
