package cn.report.excel.easyexcel.custom;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.ShapeTypes;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFSimpleShape;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import cn.report.excel.easyexcel.api.util.BooleanUtils;
import cn.report.excel.easyexcel.api.write.handler.CellWriteHandler;
import cn.report.excel.easyexcel.api.write.handler.context.CellWriteHandlerContext;
import cn.report.excel.util.PoiDrawLineUtil;

/**
 * 自定义拦截器。对第一行第一列的头超链接到:https://github.com/alibaba/easyexcel
   CreationHelper helper = context.getWriteSheetHolder()
           		.getSheet().getWorkbook().getCreationHelper();
            Hyperlink hyperlink = helper.createHyperlink(HyperlinkType.URL);
            hyperlink.setAddress("https://github.com/alibaba/easyexcel");
            cell.setHyperlink(hyperlink);
            
   示例：
   ExcelWriter excelWriter = EasyExcel.write(fileName, DemoData.class)
				//.password("1234")//加密
				 .inMemory(true) // 注意，此项配置不能少
				.build();
		String text = "       工资\n年龄";
		String slashType = "LEFT_BOTTOM";
		WriteSheet writeSheet = EasyExcel.writerSheet("模板")
				.head(EasyExcelUtil.head(header))
				// 第一行大标题样式设置
				.registerWriteHandler(new SamTitleWriteHandler(bigtitle,header ))
				//设置默认样式及写入头信息开始的行数
				.relativeHeadRowIndex(1)               
				// 表头、内容样式设置
				.registerWriteHandler(horizontalCellStyleStrategy)
				.registerWriteHandler(new SamCellDrawSlashHandler(1,0,2,1,text,slashType))
				// 统一列宽,如需设置自动列宽则new LongestMatchColumnWidthStyleStrategy()
				.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
				.build();
		List<DemoData> data = data();
		excelWriter.write(data, writeSheet);
 */
public class SamCellDrawSlashHandler implements CellWriteHandler
{
	private static final float PXTOPT = 0.75f;
	//绘制斜线的起始位置和终止位置
	private int startRow = 0; 
	private int startCol = 0;
	private int endRow = 1; 
	private int endCol = 1;
	private String text = "         \n  ";
	//slashType 斜线类型 LEFT_BOTTOM左上角为起点,左下角为起点 
	 //RIGHT_BOTTOM右下角为起点 右上角为起点
	private String slashType= "LEFT_BOTTOM";
	
	public SamCellDrawSlashHandler() {
	}
	public SamCellDrawSlashHandler(
			int startRow,int startCol,
			int endRow,int endCol) 
	{
		this.startRow = startRow;
		this.startCol = startCol;
		this.endRow = endRow;
		this.endCol = endCol;
	}
	public SamCellDrawSlashHandler(
			int startRow,int startCol,
			int endRow,int endCol,
			String text) 
	{
		this.startRow = startRow;
		this.startCol = startCol;
		this.endRow = endRow;
		this.endCol = endCol;
		this.text = text;
	}
	public SamCellDrawSlashHandler(
			int startRow,int startCol,
			int endRow,int endCol,
			String text,
			String slashType) 
	{
		this.startRow = startRow;
		this.startCol = startCol;
		this.endRow = endRow;
		this.endCol = endCol;
		this.text = text;
		this.slashType = slashType;
	}
    @Override
    public void afterCellDispose(CellWriteHandlerContext context) 
    {
        Cell cell = context.getCell();
        //这里可以对cell进行任何操作
        if (BooleanUtils.isTrue(context.getHead())
        	&& cell.getColumnIndex() == startCol//列0
        	&& cell.getRowIndex() == startRow //行1
        )
        {
        	Workbook wbook = context.getWriteSheetHolder().getSheet().getWorkbook();
        	XSSFSheet sheet =  (XSSFSheet) context.getWriteSheetHolder().getSheet();
        	CreationHelper helper = wbook.getCreationHelper();
    		XSSFDrawing drawing = sheet.createDrawingPatriarch();
    		ClientAnchor anchor = helper.createClientAnchor();
    		// 设置斜线的开始位置
    		anchor.setCol1(startCol);
    		anchor.setRow1(startRow);
    		//设置斜线的结束位置
    		anchor.setCol2(endCol);
    		anchor.setRow2(endRow);
    		XSSFSimpleShape shape = drawing.createSimpleShape((XSSFClientAnchor) anchor);
    		// 设置形状类型为线型
    		if (slashType == "LEFT_BOTTOM") {
				//斜线类型为左上，右下的时候，使用斜线
				shape.setShapeType(ShapeTypes.LINE);
			} else {
				//斜线类型为左下，右上的时候，使用反斜线
				shape.setShapeType(ShapeTypes.LINE_INV);
			}
    		// 设置线宽
    		shape.setLineWidth(0.5);
    		// 设置线的风格
    		shape.setLineStyle(0);
    		// 设置线的颜色
    		shape.setLineStyleColor(0, 0, 0);
    	
    		XSSFRow row = sheet.getRow(0);
    		row.setHeightInPoints(40 * PXTOPT);
    		XSSFCellStyle cellStyle = PoiDrawLineUtil.getCellFormat((XSSFWorkbook)wbook);
    		cellStyle.setVerticalAlignment(VerticalAlignment.TOP);
    		cell.setCellStyle(cellStyle);
    		//设置文字
    		cell.setCellValue(text);
    	}
    }
}
