package cn.superdesk.uniorder.common.excel.strategy.postil;

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 org.apache.poi.ss.usermodel.*;

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

/**
 * @Description: 设置标注
 * @Author: linjinyu
 * @CreateDate: 2022/7/1 3:35 下午
 * @UpdateUser: linjinyu
 * @UpdateDate: 2022/7/1 3:35 下午
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
public class ExcelImportExportAddPostilCellWriteHandler extends AbstractCellWriteHandler {

	private static NumberFormat nf = NumberFormat.getPercentInstance();


	/**
	 * 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) {

	}

	/**
	 * 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) {
//		这里你可以设置其他的任意格式,我这边只是处理背景颜色
		Workbook  workbook  = writeSheetHolder.getSheet().getWorkbook();
		CellStyle cellStyle = CellStyleUtil.cellStyle(workbook);
		try {
			// 行数大于0,列数为6
			if (cell.getRowIndex() > 0 && cell.getColumnIndex() == 6) {
				//如果我第*行第6列的百分比小于50%,那么施加40度灰的颜色,下同
				if (nf.parse(cell.getStringCellValue()).doubleValue() <= 0.5) {
					cellStyle.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.getIndex());
				} else if (nf.parse(cell.getStringCellValue()).doubleValue() <= 1 && nf.parse(cell.getStringCellValue()).doubleValue() > 0.5) {
					cellStyle.setFillForegroundColor(IndexedColors.BLUE.getIndex());
				} else if (nf.parse(cell.getStringCellValue()).doubleValue() <= 2 && nf.parse(cell.getStringCellValue()).doubleValue() > 1) {
					cellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());
				} else if (nf.parse(cell.getStringCellValue()).doubleValue() <= 6 && nf.parse(cell.getStringCellValue()).doubleValue() > 2) {
					cellStyle.setFillForegroundColor(IndexedColors.GOLD.getIndex());
				} else if (nf.parse(cell.getStringCellValue()).doubleValue() > 6) {
					cellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
				}
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		cell.setCellStyle(cellStyle);
	}
}

class CellStyleUtil {
	/**
	 * 通用样式
	 *
	 * @param workbook
	 * @return
	 */
	public static CellStyle cellStyle(Workbook workbook) {

		CellStyle cellStyle = workbook.createCellStyle();

		// 居中
		cellStyle.setAlignment(HorizontalAlignment.LEFT);
		cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
		cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
		cellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());

		// 设置边框
		cellStyle.setBorderBottom(BorderStyle.THIN);
		cellStyle.setBorderLeft(BorderStyle.THIN);
		cellStyle.setBorderRight(BorderStyle.THIN);
		cellStyle.setBorderTop(BorderStyle.THIN);
		return cellStyle;
	}
}

