/* StyleConversionUtil.java

	Purpose:
		
	Description:
		
	History:
		August 23, 5:53:16 PM     2010, Created by Ashish Dasnurkar

Copyright (C) 2010 Potix Corporation. All Rights Reserved.
 */
package org.zkoss.zss.model.impl.pdf;

import org.zkoss.poi.hssf.util.HSSFColor;
import org.zkoss.poi.ss.usermodel.CellStyle;
import org.zkoss.zss.model.SCellStyle;
import org.zkoss.zss.model.SBorder.BorderType;

import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfPCell;

/**
 * Internal Use Only.
 * @author ashish
 *
 */
/*package*/ class StyleConversionUtil {

	public static float DEFAULT_ROW_HEADER_COLUMN_WIDTH = 25f;
	/**
	 * returns PdfPCell horizontal alignment constant corresponding to POI CellStyle 
	 * horizontal alignment constant
	 * @param poiAlignment
	 * @return PdfPCell horizontal alignment constant corresponding to POI CellStyle
	 */
	public static int getCorrespondingPdfPCellAlignment(final short poiAlignment) {
		switch (poiAlignment) {
		case CellStyle.ALIGN_CENTER:
			return PdfPCell.ALIGN_CENTER;
		case CellStyle.ALIGN_LEFT:
			return PdfPCell.ALIGN_LEFT;
		case CellStyle.ALIGN_RIGHT:
			return PdfPCell.ALIGN_RIGHT;
		case CellStyle.ALIGN_JUSTIFY:
			return PdfPCell.ALIGN_JUSTIFIED;
		default:
			return PdfPCell.ALIGN_UNDEFINED;
		}
	}
	
	public static int getCorrespondingPdfPCellAlignment(final SCellStyle.Alignment zssAlignment) {
		switch (zssAlignment) {
		case CENTER_SELECTION:
		case CENTER:
			return PdfPCell.ALIGN_CENTER;
		case LEFT:
			return PdfPCell.ALIGN_LEFT;
		case RIGHT:
			return PdfPCell.ALIGN_RIGHT;
		case JUSTIFY:
			return PdfPCell.ALIGN_JUSTIFIED;
		case FILL:
			return PdfPCell.ALIGN_JUSTIFIED_ALL;
		default:
			return PdfPCell.ALIGN_UNDEFINED;
		}
	}
	
	/**
	 * returns PdfPCell vertical alignment constant corresponding to POI CellStyle 
	 * vertical alignment constant
	 * @param zssAlignment
	 * @return PdfPCell vertical alignment constant corresponding to POI CellStyle 
	 * vertical alignment constant
	 */
	public static int getCorrespondingPdfPCellVerticalAlignment(final SCellStyle.VerticalAlignment zssAlignment) {
		switch (zssAlignment) {
		case CENTER:
			return PdfPCell.ALIGN_MIDDLE;
		case TOP:
			return PdfPCell.ALIGN_TOP;
		case BOTTOM:
			return PdfPCell.ALIGN_BOTTOM;
		case JUSTIFY:
			return PdfPCell.ALIGN_BASELINE;
		default:
			return PdfPCell.ALIGN_UNDEFINED;
		}
	}
	
	/**
	 * returns PdfPCell vertical alignment constant corresponding to POI CellStyle 
	 * vertical alignment constant
	 * @param poiAlignment
	 * @return PdfPCell vertical alignment constant corresponding to POI CellStyle 
	 * vertical alignment constant
	 */
	public static int getCorrespondingPdfPCellVerticalAlignment(final short poiAlignment) {
		
		switch (poiAlignment) {
		case CellStyle.VERTICAL_CENTER:
			return PdfPCell.ALIGN_MIDDLE;
		case CellStyle.VERTICAL_TOP:
			return PdfPCell.ALIGN_TOP;
		case CellStyle.VERTICAL_BOTTOM:
			return PdfPCell.ALIGN_BOTTOM;
		case CellStyle.VERTICAL_JUSTIFY:
			return PdfPCell.ALIGN_BASELINE;
		default:
			return PdfPCell.ALIGN_UNDEFINED;
		}
	}
	
	public static java.awt.Color getBaseColor(HSSFColor hssfColor) {
		if (hssfColor == null)
			return null;
		short[] rgb = hssfColor.getTriplet();
		if(rgb[0] == 0 && rgb[1] == 0 && rgb[2] == 0) {
			return java.awt.Color.white;
		} else {
			return new java.awt.Color(rgb[0], rgb[1], rgb[2]);
		}
	}
	
	public static BorderStyle getBorderStyle(short borderStyle) {
		//	public BorderStyle(float lineWidth, short lineCapStyle, float[] units, float phase) {
		BorderType borderType = null;
		switch (borderStyle) {
		case CellStyle.BORDER_DOTTED:
			borderType = BorderType.DOTTED;
			break;
		case CellStyle.BORDER_HAIR:
			borderType = BorderType.HAIR;
			break;
		case CellStyle.BORDER_DASH_DOT_DOT:
			borderType = BorderType.DASH_DOT_DOT;
			break;
		case CellStyle.BORDER_MEDIUM_DASH_DOT_DOT:
			borderType = BorderType.MEDIUM_DASH_DOT_DOT;
			break;
		case CellStyle.BORDER_DASH_DOT:
			borderType = BorderType.DASH_DOT;
			break;
		case CellStyle.BORDER_DASHED:
			borderType = BorderType.DASHED;
			break;
		case CellStyle.BORDER_THIN:
			borderType = BorderType.THIN;
			break;
		case CellStyle.BORDER_SLANTED_DASH_DOT:
			borderType = BorderType.SLANTED_DASH_DOT;
			break;
		case CellStyle.BORDER_MEDIUM_DASH_DOT:
			borderType = BorderType.MEDIUM_DASH_DOT;
			break;
		case CellStyle.BORDER_MEDIUM_DASHED:
			borderType = BorderType.MEDIUM_DASHED;
			break;
		case CellStyle.BORDER_MEDIUM:
			borderType = BorderType.MEDIUM;
			break;
		case CellStyle.BORDER_THICK:
			borderType = BorderType.THICK;
			break;
		case CellStyle.BORDER_DOUBLE:
			borderType = BorderType.DOUBLE;
			break;
		default:
			borderType = null;
		}
		return getBorderStyle(borderType);
	}

	public static BorderStyle getBorderStyle(BorderType borderStyle) {
		switch (borderStyle) {
		case DOTTED:
			return new BorderStyle(1f, PdfContentByte.LINE_CAP_BUTT, new float[]{1f,1f}, 0f);
		case HAIR:
			return new BorderStyle(1.5f, PdfContentByte.LINE_CAP_BUTT, new float[]{1.5f,1.5f}, 0f);
		case DASH_DOT_DOT:
			return new BorderStyle(1.5f, PdfContentByte.LINE_CAP_BUTT, new float[]{10f, 3f, 3f, 3f, 3f, 3f}, 0f);
		case MEDIUM_DASH_DOT_DOT:
			return new BorderStyle(1.7f, PdfContentByte.LINE_CAP_BUTT, new float[]{10f, 3f, 3f, 3f, 3f, 3f}, 0f);
		case DASH_DOT:
			return new BorderStyle(1.5f, PdfContentByte.LINE_CAP_BUTT, new float[]{10f, 3f, 3f, 3f}, 0f);
		case DASHED:
			return new BorderStyle(1f, PdfContentByte.LINE_CAP_BUTT, new float[]{5f}, 0f);
		case THIN:
			return new BorderStyle(1f, PdfContentByte.LINE_CAP_BUTT, new float[]{}, 0f);
		case SLANTED_DASH_DOT:
			// this is BORDER_DASH_DOT as slanted line ends are not possible yet
			return new BorderStyle(2f, PdfContentByte.LINE_CAP_BUTT, new float[]{10f, 3f, 3f, 3f}, 0f);
		case MEDIUM_DASH_DOT:
			return new BorderStyle(1.7f, PdfContentByte.LINE_CAP_BUTT, new float[]{10f, 3f, 3f, 3f}, 0f);
		case MEDIUM_DASHED:
			return new BorderStyle(2f, PdfContentByte.LINE_CAP_BUTT, new float[]{5f}, 0f);
		case MEDIUM:
			return new BorderStyle(1.7f, PdfContentByte.LINE_CAP_BUTT, new float[]{}, 0f);
		case THICK:
			return new BorderStyle(2.2f, PdfContentByte.LINE_CAP_BUTT, new float[]{}, 0f);
		case DOUBLE:
			// Implement as 2 thin parallel lines separated by DOUBLE_LINE_SPACE
			// see CellRenderEvent#drawDoubleBorder()
			return new BorderStyle(PdfExporter.DOUBLE_LINE_WIDTH, PdfContentByte.LINE_CAP_BUTT, new float[]{}, 0f);
		default:
			return null;
		}
	}
}
