package com.stephanstan.pontus.excel

/**
 * Created by Owner on 2/28/2016.
 */
import com.stephanstan.pontus.excel.ExcelHelper
import org.apache.poi.ss.usermodel.CellStyle
import org.apache.poi.ss.usermodel.IndexedColors
import org.apache.poi.xssf.usermodel.XSSFCell
import org.apache.poi.xssf.usermodel.XSSFFont
import org.apache.poi.xssf.usermodel.XSSFRow
import org.apache.poi.xssf.usermodel.XSSFSheet
import org.apache.poi.xssf.usermodel.XSSFWorkbook

/*
 * This Groovy source file was auto generated by running 'gradle buildInit --type groovy-library'
 * by 'Owner' at '1/5/16 11:22 PM' with Gradle 2.10
 *
 * @author Owner, @date 1/5/16 11:22 PM
 */
class ExcelFileLaboratory {
    boolean someLibraryMethod() {
        true
    }

    boolean createXlsxFile(){

        String excelFileName = "docs/excel/Test.xlsx"//name of excel file

        String sheetName = "Sheet1";//name of sheet

        XSSFWorkbook wb = new XSSFWorkbook();
        XSSFSheet sheet = wb.createSheet(sheetName)

        //iterating r number of rows
        for (int r=0;r < 5; r++ )
        {
            XSSFRow row = sheet.createRow(r)

            //iterating c number of columns
            for (int c=0;c < 5; c++ )
            {
                XSSFCell cell = row.createCell(c)

                cell.setCellValue("Cell "+r+" "+c)
            }
        }

        FileOutputStream fileOut = new FileOutputStream(excelFileName)

        //write this workbook to an Outputstream.
        writeOutExcelWorkbook(wb, fileOut)

        true
    }
    boolean createXlsxFile_01(){

        String excelFileName = "docs/excel/Test_01.xlsx"//name of excel file

        String sheetName = "Sheet1";//name of sheet

        XSSFWorkbook wb = ExcelHelper.createWorkbook()
        XSSFSheet sheet = wb.createSheet(sheetName)

        //iterating r number of rows
        for (int r=0;r < 5; r++ )
        {
            XSSFRow row = sheet.createRow(r)

            //iterating c number of columns
            for (int c=0;c < 5; c++ )
            {
                XSSFCell cell = row.createCell(c)

                cell.setCellValue("Cell "+r+" "+c)
            }
        }

        FileOutputStream fileOut = new FileOutputStream(excelFileName)

        writeOutExcelWorkbook(wb, fileOut)

        true
    }

    boolean createDojangBlinger_01(){

        String excelFileName = "docs/excel/Dojang_Blinger.xlsx"//name of excel file

        String sheetName = "Dojang";//name of sheet

        XSSFWorkbook wb = ExcelHelper.createWorkbook()
        XSSFSheet sheet = wb.createSheet(sheetName)

        //iterating r number of rows
        for (int r=0;r < 5; r++ )
        {
            XSSFRow row = sheet.createRow(r)

            //iterating c number of columns
            for (int c=0;c < 5; c++ )
            {
                XSSFCell cell = row.createCell(c)

                cell.setCellValue("Cell "+r+" "+c)
            }
        }

        FileOutputStream fileOut = new FileOutputStream(excelFileName)

        //write this workbook to an Outputstream.
        writeOutExcelWorkbook(wb, fileOut)

        true
    }

    boolean createDojangBlinger_02(){

        String excelFileName = "docs/excel/Dojang_Blinger_02.xlsx"//name of excel file

        String sheetName = "Dojang";//name of sheet

        XSSFWorkbook wb = ExcelHelper.createWorkbook()
        XSSFSheet sheet = wb.createSheet(sheetName)

        // create first row that contains the header
            XSSFRow row = sheet.createRow(0)

        // http://www.concretepage.com/apache-api/how-to-set-background-and-font-color-in-xlsx-using-poi-in-java
        CellStyle style = wb.createCellStyle()
        style.setFillBackgroundColor(IndexedColors.BLACK.getIndex());
        style.setFillPattern(CellStyle.SOLID_FOREGROUND);

        def XSSFFont  font = wb.createFont();
        font.setColor(IndexedColors.WHITE.getIndex());
        style.setFont(font);

        // create first colunm header - Ignore Row
            XSSFCell cell = row.createCell(0)
            cell.setCellValue("Ignore Row")
        cell.setCellStyle(style)

        // create 2nd column - Name - parameter from the domain object Dojang
        cell = row.createCell(1)
        cell.setCellStyle(style)

        cell.setCellValue("Name")

        FileOutputStream fileOut = new FileOutputStream(excelFileName)

        //write this workbook to an Outputstream.
        writeExcelWorkbook(wb, excelFileName)

        true
    }

   /**
     *  better version
     * @param wb
     * @param fileName
     */
    public void writeExcelWorkbook(XSSFWorkbook wb, String fileName )
    {
        FileOutputStream fileOut = new FileOutputStream(fileName);
        wb.write(fileOut);
        fileOut.flush();
        fileOut.close();
    }

    /**
     * deprecated
     * @param wb
     * @param fileOut
     */
    private void writeOutExcelWorkbook(XSSFWorkbook wb, FileOutputStream fileOut) {
        wb.write(fileOut);
        fileOut.flush();
        fileOut.close();
    }

}
