package com.bankaudit.rest.process.controller;

import org.jboss.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.bankaudit.rest.process.service.BatchProcessingService;
import com.bankaudit.util.BankAuditUtil;
import com.bankaudit.util.ServiceStatus;



@RestController

@RequestMapping("/api/batchProcessing")
public class BatchProcessingController {
	
	@Autowired
	BatchProcessingService batchProcessingService;
	
	static final Logger logger = Logger.getLogger(BatchProcessingController.class);
	
	
@RequestMapping(value="/executeBPforDynamicStatement/{legalEntityCode}/{auditId}/{auditTypeCode}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus executeBPforDynamicStatement(@PathVariable("legalEntityCode")Integer legalEntityCode,
			@PathVariable("auditId") String auditId, @PathVariable("auditTypeCode") String auditTypeCode){
		ServiceStatus serviceStatus=new ServiceStatus();
		if(legalEntityCode!=null && !BankAuditUtil.isEmptyString(auditId)){
			try {
			    Boolean execute=batchProcessingService.executeBPforDynamicStatement(legalEntityCode,auditId,auditTypeCode);
			    if(execute)batchProcessingService.updateAuditReportStmntRupeeSymbl(legalEntityCode, auditTypeCode, auditId);
		    	serviceStatus.setResult(execute);
			    serviceStatus.setStatus("success");
			    serviceStatus.setMessage("successfully retrieved ");					
			} catch (Exception e) {
				e.printStackTrace();
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
			}			
		}else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid payload ");
		}
		return serviceStatus;
	}
	
	
@RequestMapping(value="/getBatchInProgressStatus/{legalEntityCode}/{auditId}/{auditTypeCode}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus getBatchInProgressStatus(@PathVariable("legalEntityCode")Integer legalEntityCode,
			@PathVariable("auditId") String auditId, @PathVariable("auditTypeCode") String auditTypeCode){
		ServiceStatus serviceStatus=new ServiceStatus();
		if(legalEntityCode!=null && !BankAuditUtil.isEmptyString(auditId)){
			try {
			    Boolean inProgress=batchProcessingService.getBatchInProgressStatus(legalEntityCode,auditId,auditTypeCode);
		    	serviceStatus.setResult(inProgress);
			    serviceStatus.setStatus("success");
			    serviceStatus.setMessage("successfully retrieved ");					
			} catch (Exception e) {
				e.printStackTrace();
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
			}			
		}else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid payload ");
		}
		return serviceStatus;
	}

}
