package org.xbib.datastructures.xslx;

import javax.xml.stream.XMLStreamException;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;

abstract public class WriterSupport {

	public final static int TYPE_XLS = 1;
	public final static int TYPE_XLSX = 2;

	protected File file;

	public void setFile(File file) {
		this.file = file;
	}

	protected OutputStream output;

	public void setOutputStream(OutputStream output) {
		this.output = output;
	}

	abstract protected int getMaxRowNumOfSheet();

	abstract public void open();

	abstract public void createNewSheet() throws IOException;

	abstract public void writeRow(String[] rowData) throws XMLStreamException, IOException;

	abstract public void writeRow(String[] rowData, CellFormat[] formats) throws XMLStreamException, IOException;

	abstract public void close();

	public static WriterSupport newInstance(int type, File f) {
		WriterSupport support = null;
		if (type == TYPE_XLSX)
			support = new XLSXWriterSupport();
		else
			support = new XLSWriterSupport();
		support.setFile(f);
		return support;
	}

	public static WriterSupport newInstance(int type, OutputStream outputStream) {
		WriterSupport support = null;
		if (type == TYPE_XLSX)
			support = new XLSXWriterSupport();
		else {
			support = new XLSWriterSupport();
		}
		support.setOutputStream(outputStream);
		return support;
	}

	public int getSheetIndex() {
		return sheetIndex;
	}

	public int getRowPos() {
		return rowpos;
	}

	protected int rowpos = getMaxRowNumOfSheet();

	protected int sheetIndex = -1;

	public void increaseRow() throws IOException {
		rowpos++;

		if (rowpos > getMaxRowNumOfSheet()) {
			sheetIndex++;
			createNewSheet();
			rowpos = -1;
		}
	}

}
