package com.bankaudit.report.v1.rest.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jboss.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bankaudit.report.v1.dto.AuditBookReportDataModel;
import com.bankaudit.report.v1.service.AuditBookReportService;
import com.bankaudit.util.BankAuditUtil;
import com.bankaudit.util.ServiceStatus;

/**
 * This controller includes audit plan process like
 * get Branch units,schedule dates,team and validation.  
 * 
 * @author narendar.burada
 * @version 1.0
 * @since 2014-03-31
 */



@RestController

@RequestMapping("/api/auditReport1")
public class ReportBookController {

	 
	ServiceStatus serviceStatus;
	
	@Autowired
	AuditBookReportService auditBookReportService;
	 
	static final Logger logger = Logger.getLogger(ReportBookController.class);
	
	/**
	 * This method is to fetch all branches which are fall under schedule plan start and end dates. 
	 * @param legalEntityCode
	 * @param auditTypecode
	 * @param planStartDate
	 * @param planEndDate
	 * @param maker
	 * @return AuditPlansList
	 */
	
@RequestMapping(value = "/generateAuditBookReport", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus  generateAuditBookReport(
			@RequestParam("legalEntityCode") int legalEntityCode,
			@RequestParam(value="auditId",required=false) String auditId,
			@RequestParam(value="reportLanguage",required=false) String reportLanguage){

		serviceStatus=new ServiceStatus();
		try {
			logger.info("generateAuditBookReport .. "+legalEntityCode + " :: "+ auditId );
			if((legalEntityCode>=0) && (auditId!=null && !BankAuditUtil.isEmptyString(auditId)) ){ 
				    Boolean isReportGenerate = true;
				    Map<String, Object> params = new HashMap<String, Object>();					
					  auditBookReportService.generatePDFReport(params, legalEntityCode, auditId);
				  /*  Map<String, Object> params = new HashMap<String, Object>();					
					byte[] bytes = auditBookReportService.generatePDFReport(params, legalEntityCode, auditId);
					return ResponseEntity.ok().header("Content-Type", "application/pdf; charset=UTF-8")
							.header("Content-Disposition", "inline; filename=\"" + "Test" + ".pdf\"").body(bytes);   
				    */
					 List<AuditBookReportDataModel> listChapterDto =    auditBookReportService.generateAuditBookReport(legalEntityCode, auditId, reportLanguage);
					if(isReportGenerate){						
						serviceStatus.setResult(listChapterDto);
						serviceStatus.setStatus("success");
						serviceStatus.setMessage("successfuly retrieved ");
					}else {
						serviceStatus.setStatus("failure");
						serviceStatus.setMessage("Report Generation Failed.");
					} 
			}else {
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("invalid input.");
			}

		} catch (Exception e) {
			e.printStackTrace();
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("failure");
		}

		return serviceStatus;
	}
 }
