package cn.superdesk.uniorder.common.excel.biz;

import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.handler.AbstractCellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.ss.usermodel.*;

import java.text.NumberFormat;
import java.util.List;


/**
 * @Description: Java类作用描述
 * @Author: linjinyu
 * @CreateDate: 2021/9/22 下午5:31
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/9/22 下午5:31
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@AllArgsConstructor
@NoArgsConstructor
@Builder(toBuilder = true)
public class SugarUpBigDecimalCellWriteHandler extends AbstractCellWriteHandler {

	private static NumberFormat nf = NumberFormat.getPercentInstance();

	// 从0开始 页头一般为0 所以从1开始
	private Integer       startRow;
	// 从0开始
	private List<Integer> matchCell;


	/**
	 * Called before create the cell
	 *
	 * @param writeSheetHolder
	 * @param writeTableHolder Nullable.It is null without using table writes.
	 * @param row
	 * @param head             Nullable.It is null in the case of fill data and without head.
	 * @param columnIndex
	 * @param relativeRowIndex Nullable.It is null in the case of fill data.
	 * @param isHead           It will always be false when fill data.
	 */
	@Override
	public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head,
	                             Integer columnIndex, Integer relativeRowIndex, Boolean isHead) {

	}

	/**
	 * Called after the cell is created
	 *
	 * @param writeSheetHolder
	 * @param writeTableHolder Nullable.It is null without using table writes.
	 * @param cell
	 * @param head             Nullable.It is null in the case of fill data and without head.
	 * @param relativeRowIndex Nullable.It is null in the case of fill data.
	 * @param isHead           It will always be false when fill data.
	 */
	@Override
	public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head,
	                            Integer relativeRowIndex, Boolean isHead) {

//		log.info("afterCellCreate startRow {} - matchCell {}", startRow, matchCell);

		if (startRow.compareTo(cell.getRowIndex()) <= 0) {

			if (matchCell.contains(cell.getColumnIndex())) {

//				log.info("afterCellCreate matching getRowIndex {} - getColumnIndex {}", cell.getRowIndex(), cell.getColumnIndex());

//				Workbook   workbook   = writeSheetHolder.getSheet().getWorkbook();
//				CellStyle  cellStyle  = workbook.createCellStyle();
//				DataFormat dataFormat = workbook.createDataFormat();
//				cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00"));
//				cell.setCellStyle(cellStyle);

				Workbook      workbook   = writeSheetHolder.getSheet().getWorkbook();
//				HSSFCellStyle cellStyle  = workbook.createCellStyle();
				CellStyle cellStyle  = workbook.createCellStyle();

				//创建浮点数值格式
//				short doubleFormat = HSSFDataFormat.getBuiltinFormat("0.00");
				DataFormat    dataFormat = workbook.createDataFormat();
//				short doubleFormat = HSSFDataFormat.getBuiltinFormat("0.00");

				cellStyle.setDataFormat(dataFormat.getFormat("0.00"));

				cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
				cell.setCellStyle(cellStyle);
			}
		}


	}

	/**
	 * Called after the cell data is converted
	 *
	 * @param writeSheetHolder
	 * @param writeTableHolder Nullable.It is null without using table writes.
	 * @param cell
	 * @param head             Nullable.It is null in the case of fill data and without head.
	 * @param cellData         Nullable.It is null in the case of add header.
	 * @param relativeRowIndex Nullable.It is null in the case of fill data.
	 * @param isHead           It will always be false when fill data.
	 */
	@Override
	public void afterCellDataConverted(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, CellData cellData,
	                                   Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {

	}

	/**
	 * Called after all operations on the cell have been completed
	 *
	 * @param writeSheetHolder
	 * @param writeTableHolder Nullable.It is null without using table writes.
	 * @param cell
	 * @param head             Nullable.It is null in the case of fill data and without head.
	 * @param cellDataList     Nullable.It is null in the case of add header.There may be several when fill the data.
	 * @param relativeRowIndex Nullable.It is null in the case of fill data.
	 * @param isHead           It will always be false when fill data.
	 */
	@Override
	public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
	                             List<CellData> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {

	}
}


