/*******************************************************************************
 * 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 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 CustomerFirstOrderReportExcelView extends AbstractExcelView {

	protected Date printDate;
	protected final String REPORT_NAME = "Customer First Order Report";

	@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);
		String reportHeader =  (String)model.get("reportHeader");

		sheet.getHeader().setCenter(HSSFHeader.font("Calibri", "bold")+(reportHeader==null?"":reportHeader)+"\n"+REPORT_NAME);


		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);
			}

			ExcelUtils.setCellValue(sheet, rowIndex, colIndex++, i+1);
			ExcelUtils.setCellValue(sheet, rowIndex, colIndex++, record.get("minDate"));
			ExcelUtils.setCellValue(sheet, rowIndex, colIndex++, record.get("orderNo"));
			ExcelUtils.setCellValue(sheet, rowIndex, colIndex++, record.get("customerName"));
			String phone = "";
			if(record.get("phone1") != null ){
				phone = record.get("phone1").toString().trim();
			}
			if(record.get("phone2") != null ){
				phone = phone.equals("")?record.get("phone2").toString():"/"+record.get("phone2").toString();
			}
			ExcelUtils.setCellValue(sheet, rowIndex, colIndex++, phone);
			ExcelUtils.setCellValue(sheet, rowIndex, colIndex++, record.get("email"));
			ExcelUtils.setCellValue(sheet, rowIndex, colIndex++, record.get("address"));
			ExcelUtils.setCellValue(sheet, rowIndex, colIndex++, record.get("companyName"));
			ExcelUtils.setCellValue(sheet, rowIndex, colIndex++, record.get("companyPhone"));
			ExcelUtils.setCellValue(sheet, rowIndex, colIndex++, record.get("companyEmail"));
			ExcelUtils.setCellValue(sheet, rowIndex, colIndex++, record.get("companyAddress"));
			ExcelUtils.setCellValue(sheet, rowIndex, colIndex++, record.get("remarks"));
			sheet.getRow(rowIndex).setHeightInPoints(-1);
			rowIndex++;
		}

		List<Map<String, Object>> noOrderList = (List<Map<String, Object>>) model.get("noOrderList");

		if(noOrderList == null || noOrderList.size()<=0) return;

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

			if(i==0){
				rowIndex++;
				ExcelUtils.copyAndInsertRow(workbook, sheet, 1, rowIndex);
				ExcelUtils.addMergedRegion(sheet, rowIndex-1, rowIndex-1, 0, 3);
				ExcelUtils.setCellValue(sheet, rowIndex-1, 0, "Not Order Yet");
			}else{
				ExcelUtils.copyAndInsertRow(workbook, sheet, 1, rowIndex);
			}

			ExcelUtils.setCellValue(sheet, rowIndex, colIndex++, i+1);
			ExcelUtils.setCellValue(sheet, rowIndex, colIndex++, "--");
			ExcelUtils.setCellValue(sheet, rowIndex, colIndex++, "--");
			ExcelUtils.setCellValue(sheet, rowIndex, colIndex++, record.get("customerName"));
			String phone = "";
			if(record.get("phone1") != null ){
				phone = record.get("phone1").toString().trim();
			}
			if(record.get("phone2") != null ){
				phone = phone.equals("")?record.get("phone2").toString():"/"+record.get("phone2").toString();
			}
			ExcelUtils.setCellValue(sheet, rowIndex, colIndex++, phone);
			ExcelUtils.setCellValue(sheet, rowIndex, colIndex++, record.get("email"));
			ExcelUtils.setCellValue(sheet, rowIndex, colIndex++, record.get("address"));
			ExcelUtils.setCellValue(sheet, rowIndex, colIndex++, record.get("companyName"));
			ExcelUtils.setCellValue(sheet, rowIndex, colIndex++, record.get("companyPhone"));
			ExcelUtils.setCellValue(sheet, rowIndex, colIndex++, record.get("companyEmail"));
			ExcelUtils.setCellValue(sheet, rowIndex, colIndex++, record.get("companyAddress"));
			ExcelUtils.setCellValue(sheet, rowIndex, colIndex++, record.get("remarks"));
			sheet.getRow(rowIndex).setHeightInPoints(-1);
			rowIndex++;
		}


	}
}
