/*
 * DefaultExcelWorkbookBuilder.java
 *
 * Copyright (C) 2002-2017 Takis Diakoumis
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */

package org.executequery.gui.importexport;

import org.apache.poi.hssf.usermodel.*;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

/**
 * @author Takis Diakoumis, Pawel Bialkowski
 */
public class DefaultExcelWorkbookBuilder implements ExcelWorkbookBuilder {

    private int currentRow;

    private HSSFWorkbook workbook;

    private HSSFSheet currentSheet;

    private HSSFCellStyle defaultCellStyle;

    public DefaultExcelWorkbookBuilder() {

        workbook = new HSSFWorkbook();
        defaultCellStyle = createStyle();
    }

    public void reset() {

        currentRow = 0;
        currentSheet = null;
    }

    public void writeTo(OutputStream outputStream) throws IOException {

        workbook.write(outputStream);
    }

    public void createSheet(String sheetName) {

        currentSheet = workbook.createSheet(sheetName);
    }

    public void addRow(List<String> values) {

        fillRow(values, createRow(++currentRow), defaultCellStyle);
    }

    public void addRowHeader(List<String> values) {

        if (currentRow > 0) {

            currentRow++;
        }

        HSSFFont font = createFont();
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

        HSSFCellStyle style = createStyle();
        style.setFont(font);

        fillRow(values, createRow(currentRow), style);
    }

    private HSSFRow createRow(int rowNumber) {

        return currentSheet.createRow(rowNumber);
    }

    private void fillRow(List<String> values, HSSFRow row, HSSFCellStyle style) {

        for (int i = 0, n = values.size(); i < n; i++) {

            HSSFCell cell = row.createCell(i);

            // set encoding no longer supported in POI 3.2
//            cell.setEncoding(HSSFCell.ENCODING_UTF_16);

            cell.setCellStyle(style);
            cell.setCellValue(new HSSFRichTextString(values.get(i)));
        }

    }

    private HSSFCellStyle createStyle() {

        return workbook.createCellStyle();
    }

    private HSSFFont createFont() {

        return workbook.createFont();
    }

}







