/*******************************************************************************
 * Copyright 2Fi Business Solutions Ltd.
 *
 * This code is copyrighted. Under no circumstances should any party, people,
 * or organization should redistribute any portions of this code in any form,
 * either verbatim or through electronic media, to any third parties, unless
 * under explicit written permission by 2Fi Business Solutions Ltd.
 ******************************************************************************/
package com.ffii.tbms.reports.view;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ffii.core.utils.ExcelUtils;
import com.ffii.core.utils.Params;
import com.ffii.core.web.view.document.AbstractExcelView;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

/**
 * @author fung
 */
public class AuditLogExportExcelView extends AbstractExcelView {

	protected Date printDate;
	protected final String REPORT_CODE = "NEW Report";
	protected final String REPORT_NAME = "Audit Log Export";

	protected CellStyle normalStyle;
	protected CellStyle wrapTextStyle;

	@SuppressWarnings("unchecked")
	@Override
	protected void buildExcelDocument(Map<String, Object> model, Workbook workbook, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		printDate = new Date();
		response.setHeader("Content-Disposition",
				"attachment; filename=\"" + response.encodeURL(REPORT_CODE + " - " + REPORT_NAME + "_") + ".xlsx\"");
		Sheet sheet = workbook.getSheetAt(0);
		// CommonReportUtils.setPageForAllColWidth(sheet, Boolean.FALSE);
		createStyle(workbook);

		// get Data
		List<Map<String, Object>> records = (List<Map<String, Object>>) model.get(Params.RECORDS);

		int rowIndex = 1;
		for (int i = 0; i < records.size(); i++) {
			Map<String, Object> record = records.get(i);
			int colIndex = 0;

			setCellValue(sheet, rowIndex, colIndex++, record.get("tableName"), normalStyle);
			setCellValue(sheet, rowIndex, colIndex++, record.get("recordId"), normalStyle);
			setCellValue(sheet, rowIndex, colIndex++, record.get("modifiedBy"), normalStyle);
			setCellValue(sheet, rowIndex, colIndex++, record.get("modifiedUser"), normalStyle);
			// setCellValue(sheet, rowIndex, colIndex++,
			// CommonReportUtils.getDatetimeStr((Date) record.get("modified")),
			// normalStyle);

			int nRowIndex = printFields(sheet, (String) record.get("newData"), rowIndex, colIndex++);
			int oRowIndex = printFields(sheet, (String) record.get("oldData"), rowIndex, colIndex++);

			rowIndex = nRowIndex > oRowIndex ? nRowIndex : oRowIndex;
			rowIndex++;
		}
	}

	protected void createStyle(Workbook workbook) {
		normalStyle = workbook.createCellStyle();
		wrapTextStyle = workbook.createCellStyle();

		// Font font = workbook.createFont();
		// font.setFontHeightInPoints(CommonReportUtils.TEXT_SIZE);
		// font.setFontName(CommonReportUtils.TEXT_FONT_NAME);
		// normalStyle.setFont(font);

		// wrapTextStyle.setFont(font);
		wrapTextStyle.setWrapText(Boolean.TRUE);
	}

	protected int printFields(Sheet sheet, String jsonStr, int rowIndex, final int COL_INDEX) {
		// if (StringUtils.isNotBlank(jsonStr)) {
		// 	try {
		// 		Map<String, Object> map = JsonUtils.fromJsonString(jsonStr, Map.class);
		// 		for (String k : map.keySet()) {
		// 			if ("created".equals(k)||"modified".equals(k)) {
					
		// 			}else {
					
		// 			}
		// 			setCellValue(sheet, rowIndex, COL_INDEX, k + ": " +
		// 			CommonReportUtils.getValue(map.get(k)), wrapTextStyle);
		// 			sheet.getRow(rowIndex++).setHeight((short) -1);
		// 		}
		// 	} catch (Exception e) {
		// 		setCellValue(sheet, rowIndex, COL_INDEX, "json error: " + jsonStr, wrapTextStyle);
		// 		sheet.getRow(rowIndex++).setHeight((short) -1);
		// 	}
		// }
		return rowIndex;
	}

	protected void setCellValue(Sheet sheet, int rowIndex, int colIndex, Object value, CellStyle style) {
		if (style != null) {
			Cell cell = ExcelUtils.getCell(sheet, rowIndex, colIndex);
			cell.setCellStyle(style);
			ExcelUtils.setCellValue(cell, value);
		} else {
			ExcelUtils.setCellValue(sheet, rowIndex, colIndex, value);
		}
	}
}
