package cn.pluss.platform.util.excel.model;

import org.apache.poi.xssf.usermodel.XSSFCellStyle;

import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 带合并单元格的，规律的row
 * 一行内容的cell
 * @author Djh
 */
public class Row {

    private List<Cell> cellList;

    private DateTimeFormatter df;

    private XSSFCellStyle cellStyle;

    private int scaleCol;

    private int scaleRow;

    private int spanRow = -1;

    private int spanCol = -1;

    public Row() {
        this.scaleCol = 0;
        this.scaleRow = 0;
    }

    public void setDataTimeFormatter(DateTimeFormatter df) {
        this.df = df;
    }

    public void setCellStyle (XSSFCellStyle cellStyle) {
        this.cellStyle = cellStyle;
    }

    public Row(List<Cell> cellList, int scaleCol, int scaleRow) {
        this.cellList = cellList;
        this.scaleCol = scaleCol;
        this.scaleRow = scaleRow;
        computeRowAndCel();
    }

    public void setCellList(List<Cell> cellList) {
        this.cellList = cellList;
        computeRowAndCel();
    }
    /**
     * 计算跨行和跨列的宽度,当cellList被修改，需要调用此方法
     */
    public void computeRowAndCel() {
        int startRow = Integer.MAX_VALUE;
        int endRow = -1;

        int startCol = Integer.MAX_VALUE;
        int endCol = -1;

        for (Cell cell: cellList) {
            startRow = Math.min(startRow, cell.getStartRow());
            endRow = Math.max(endRow, cell.getEndY());

            startCol = Math.min(startCol, cell.getStartCol());
            endCol = Math.max(endCol, cell.getEndX());
        }

        spanRow = endRow - startRow + 1;
        spanCol = endCol - startCol + 1;
    }

    public int getSpanCol() {
        return spanCol;
    }

    public int getSpanRow() {
        return spanRow;
    }

    public void add2Sheet(Sheet sheet) {
        for (Cell cell: cellList) {
            cell.addCellRangeAddress(sheet, scaleCol, scaleRow, cellStyle);
        }
    }
}
