/* PdfExporter.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.html;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.zkoss.zss.model.CellRegion;
import org.zkoss.zss.model.SBook;
import org.zkoss.zss.model.SBooks;
import org.zkoss.zss.model.SPicture;
import org.zkoss.zss.model.SSheet;
import org.zkoss.zss.model.SheetRegion;
import org.zkoss.zss.range.SExporter;


/**
 * Implementation of {@link XExporter} interface that converts Excel data
 * model {@link org.zkoss.zss.model.sys.SBook} into a PDF format written to a {@link OutputStream}
 * 
 * @author peterkuo, kuro
 * 
 */
// TODO ZSS 3.5 - implement Headings
public class HtmlExporter implements SExporter, Serializable {
	private static final long serialVersionUID = -6528106240649515421L;

	protected SBook wb;

	protected int[] colBreaks;
	protected int[] rowBreaks;

	protected int firstColumn;
	protected int endColumn;
	protected int endRow;
	
	protected int pageCount = 1;
	protected int pageNumber;

	

	protected boolean printGridLines;
	protected boolean printHeadings;

	
	protected float headerFooterTotalWidth;
	protected float leftMargin;
	protected float rightMargin;
	protected float topMargin;
	protected float bottomMargin;
	protected float headerMargin;
	protected float footerMargin;
	// TODO ZSS 3.5 - Header, Footer
//	protected Header header;
//	protected Footer footer;

	protected Map<Integer, Map<String, int[]>> sheetsWithRepeatingRowsAndColumns 
		= new HashMap<Integer, Map<String, int[]>>();
	protected float totalAvailablePageWidth;
	protected float totalAvailablePageHeight;
	
	protected float[] columnLeft;
	protected float[] rowTop;
	protected Map<String, Integer> lastPageRowColumnBounds;
	protected int currentSheetIndex = 0;
	
	List<SPicture> pictures;
	
	// TODO ZSS 3.5 - Picture and Charts
	//protected DrawingManager _dm;
	protected int _outcol1;
	protected int _outcol2;
	protected int _outrow1;
	protected int _outrow2;
	protected org.zkoss.zul.Chart _chart;
	protected String _refSheetName;

	public HtmlExporter() {
	}

	/**
	 * Exports ZK Spreadsheet {@link SBook} into another format written to a 
	 * {@link OutputStream}. Note that it exports entire workbook. 
	 * @param workbook
	 * @param outputStream
	 */
	public void export(final SBook workbook, OutputStream outputStream) throws IOException{
		ToHtml.export(workbook, outputStream, !printHeadings);
	}

	/**
	 * Exports ZK Spreadsheet {@link SSheet} into another format written to a 
	 * @param worksheet sheet instance that contains selected area
	 * @param outputStream outoutStream to which exported contents to be written
	 */
	public void export(SSheet worksheet, OutputStream outputStream) throws IOException{
		ToHtml.export(worksheet, outputStream, !printHeadings);
	}

	@Override
	public void export(SBook book, File file) throws IOException {
		OutputStream os = null;
		try{
			os = new FileOutputStream(file);
			export(book,os);
		}finally {
			if(os!=null){
				try{
					os.close();
				}catch(Exception x){
					x.printStackTrace();
				}
			}
		}
	}

	/**
	 * Exports selected area of ZK Spreadsheet active sheet represented by 
	 * {@link org.zkoss.zss.model.sys.XRange}
	 * @param worksheet sheet instance that contains selected area
	 * @param area area representing selected area to be exported
	 * @param outputStream outoutStream to which exported contents to be written
	 */
	public void export(SheetRegion sheetRegion, OutputStream outputStream) throws IOException{		
		ToHtml.export(sheetRegion.getSheet(), sheetRegion.getRegion(), outputStream, !printHeadings);
	}

	/**
	 * Enables row and column headings printing
	 * @param enable
	 */
	// TODO ZSS 3.5 - Implement Headings
	public void enableHeadings(boolean enable) {
		this.printHeadings = enable;
	}
}
