/*******************************************************************************
 * 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.math.BigDecimal;
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.NumberUtils;
import com.ffii.core.utils.Params;
import com.ffii.core.web.view.document.AbstractExcelView;
import com.ffii.tbms.order.Order;
import com.ibm.icu.text.DecimalFormat;
import com.ibm.icu.text.SimpleDateFormat;

import org.apache.poi.hssf.usermodel.HSSFHeader;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

public class OrderReportExcelView extends AbstractExcelView {

	protected Date printDate;
	protected final String REPORT_NAME = "Order Report";
	DecimalFormat df = new DecimalFormat("#,##0.00");

	@SuppressWarnings("unchecked")
	@Override
	protected void buildExcelDocument(Map<String, Object> model, Workbook workbook, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		printDate = new Date();
		String pattern = "yyyy-MM-dd";
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
		String todayStr = simpleDateFormat.format(printDate);
		response.setHeader("Content-Disposition",
				"attachment; filename=\"" + response.encodeURL(REPORT_NAME + "_"+todayStr) + ".xlsx\"");
		Sheet sheet = workbook.getSheetAt(0);
		// get Data
		List<Map<String, Object>> records = (List<Map<String, Object>>) model.get(Params.RECORDS);
		Map<String, Object> map = (Map<String, Object>) model.get("map");
		String reportHeader =  (String)model.get("reportHeader");

		sheet.getHeader().setCenter(HSSFHeader.font("Calibri", "bold")+(reportHeader==null?"":reportHeader)+"\n"+REPORT_NAME+" ("+
			simpleDateFormat.format(map.get("dateFrom"))+" - "+simpleDateFormat.format(map.get("dateTo"))+")");

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

			if(rowIndex>1){
				ExcelUtils.copyAndInsertRow(workbook, sheet, 1, rowIndex);
			}

			String itemStr = "";
			String priceStr = "";
			if(record.get("item") != null && !record.get("item").equals("")){
				String[] items = record.get("item").toString().split("\\|\\|;");
				for(String item:items){
					String name = item.split("\\|;")[0];
					BigDecimal price = NumberUtils.toDecimal(item.split("\\|;")[1], BigDecimal.ZERO);
					itemStr = itemStr.equals("")?"":itemStr+"\n";
					itemStr = itemStr+name;
					priceStr = priceStr.equals("")?"":priceStr+"\n";
					priceStr = priceStr+df.format(price);
				}
			}

			ExcelUtils.setCellValue(sheet, rowIndex, colIndex++, i+1);
			ExcelUtils.setCellValue(sheet, rowIndex, colIndex++, record.get("date"));
			ExcelUtils.setCellValue(sheet, rowIndex, colIndex++, record.get("orderNo"));
			ExcelUtils.setCellValue(sheet, rowIndex, colIndex++, record.get("refNo"));
			ExcelUtils.setCellValue(sheet, rowIndex, colIndex++, record.get("customerName"));
			ExcelUtils.setCellValue(sheet, rowIndex, colIndex++, getStatusStr(NumberUtils.intValue(record.get("status"))));
			ExcelUtils.setCellValue(sheet, rowIndex, colIndex++, itemStr);
			ExcelUtils.setCellValue(sheet, rowIndex, colIndex++, priceStr);
			ExcelUtils.setCellValue(sheet, rowIndex, colIndex++, record.get("orderAmount"));
			ExcelUtils.setCellValue(sheet, rowIndex, colIndex++, record.get("remarks"));
	
			sheet.getRow(rowIndex).setHeightInPoints(-1);

			rowIndex++;
		}
	}

	public String getStatusStr(int status){
		if(status == Order.STATUS_COMPLETED) return "Completed";
		if(status == Order.STATUS_ON_HAND) return "On Hand";
		if(status == Order.STATUS_CANCELLED) return "Cancelled";
		return "";
	}
}
