package com.ejie.ad74a.util;

import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.RegionUtil;

public class UtilPOI {

	private UtilPOI() {

	}

	/**
	 * Funcion que devuelve estilo con texto centrado para el background
	 *
	 * @param workbook
	 * @param color
	 * @return
	 */
	public static CellStyle getStyleBackground(Workbook workbook, short color) {

		CellStyle cellStyle = workbook.createCellStyle();

		cellStyle.setFillForegroundColor(color);
		cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
		cellStyle.setAlignment(HorizontalAlignment.CENTER);

		return cellStyle;
	}
	
	/**
	 * Devuelve fuente Arial contemplando los parametros de negrita, subrayado y tamano.
	 * Si no se indica tamano, por defecto establece 10.
	 * @param workbook
	 * @param bold
	 * @param underline
	 * @param size
	 * @return
	 */
	public static Font getFont(Workbook workbook, Boolean bold, Byte underline, Integer size) {
		
		Font font = workbook.createFont();
		font.setFontName("Arial");
		font.setBold(bold);
		font.setUnderline(underline);
		font.setFontHeightInPoints(null != size ? size.shortValue() : 10);
		
		return font;
	}

	/**
	 * Devuelve estilo con alineacion, fuente, background y borders.
	 * Si el background o el border es null, no los contempla.
	 * 
	 * @param workbook
	 * @param alignment
	 * @param font
	 * @param backgroundColor
	 * @param borderStyle
	 * @return
	 */
	public static CellStyle getCellStyle(Workbook workbook, HorizontalAlignment alignment, Font font,
			IndexedColors backgroundColor, BorderStyle borderStyle) {

		CellStyle cellStyle = workbook.createCellStyle();

		if (null != backgroundColor) {
			cellStyle.setFillForegroundColor(backgroundColor.getIndex());
			cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
		}

		if (null != borderStyle) {
			cellStyle.setBorderBottom(borderStyle);
			cellStyle.setBorderTop(borderStyle);
			cellStyle.setBorderLeft(borderStyle);
			cellStyle.setBorderRight(borderStyle);
		}

		cellStyle.setAlignment(alignment);
		cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
		cellStyle.setWrapText(true);
		cellStyle.setFont(font);

		return cellStyle;
	}

	/**
	 * Inserta borders a una region
	 * @param region
	 * @param sheet
	 */
	public static void frame(CellRangeAddress region,Sheet sheet){
	    sheet.addMergedRegion(region);
	    
	    final BorderStyle border = BorderStyle.THIN;
	    
	    RegionUtil.setBorderBottom(border, region, sheet);
	    RegionUtil.setBorderTop(border, region, sheet);
	    RegionUtil.setBorderLeft(border, region, sheet);
	    RegionUtil.setBorderRight(border, region, sheet);
	}
	
}
