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.RequestBody;
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.constants.BankAuditConstant;
import com.bankaudit.dto.process.AuditGradingDto;
import com.bankaudit.model.MaintGradingParamScoreMatrix;
import com.bankaudit.rest.process.service.AuditGradingService;
import com.bankaudit.util.BankAuditUtil;
import com.bankaudit.util.ServiceStatus;



@RestController

@RequestMapping("/api/auditGrading")
public class AuditGradingController {
	
	@Autowired
	AuditGradingService auditGradingService;

	static final Logger logger = Logger.getLogger(AuditGradingController.class);
	
	
@RequestMapping(value = "/initiateAuditGrading", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus initiateAuditGrading(@RequestParam(value = "legalEntityCode") Integer legalEntityCode,
			@RequestParam(required=true,value = "auditTypeCode") String auditTypeCode,
			@RequestParam(required=true,value = "auditId") String auditId,
			@RequestParam(required=true,value = "unitId") String unitId,
			@RequestParam(required=true,value = "userId") String userId,
			@RequestParam(required=true,value = "role") String role,
			@RequestParam(required=true,value = "userUnitType") String userUnitType
			) {
		
		logger.info("Inside initiateAuditGrading .. "+legalEntityCode +" :: "+auditTypeCode+" :: "+auditId +" :: "+unitId +" :: "+userId );
		ServiceStatus serviceStatus = new ServiceStatus(); 		 
		String status = auditGradingService.initiateAuditGrading(legalEntityCode, auditTypeCode, auditId, unitId, userId, role, userUnitType);
		if (BankAuditConstant.SUCCESS.equalsIgnoreCase(status)) {
			serviceStatus.setResult("success");
			serviceStatus.setStatus("success");
			serviceStatus.setMessage("Successfully initiated ");
		} else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("failure ");
		} 
		return serviceStatus;
	}
	
	
@RequestMapping(value = "/getMyAuditGrading", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus getMyAuditGrading(@RequestParam(value = "legalEntityCode") Integer legalEntityCode,
			@RequestParam(required=true,value = "auditTypeCode") String auditTypeCode,
			@RequestParam(required=true,value = "auditId") String auditId,
			@RequestParam(required=true,value = "userUnitType") String userUnitType) {
		
		logger.info("Inside getMyAuditGrading .. "+legalEntityCode +" :: "+auditTypeCode+" :: "+auditId );
		ServiceStatus serviceStatus = new ServiceStatus(); 		 
		AuditGradingDto auditGradingDto = auditGradingService.getMyAuditGrading(legalEntityCode, auditTypeCode, auditId, userUnitType);
		if (auditGradingDto != null && auditGradingDto.getAuditGradingQsnDtlsMap() != null && auditGradingDto.getAuditGradingQsnDtlsMap().size()>0) {
			serviceStatus.setResult(auditGradingDto);
			serviceStatus.setStatus("success");
			serviceStatus.setMessage("Successfully retrieved ");
		} else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("no records found ");
		} 
		return serviceStatus;
	}
	
	
@RequestMapping(value = "/saveAuditGrading", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE, consumes=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus saveAuditGrading(@RequestBody AuditGradingDto auditGradingDto) {
		logger.info("Inside saveAuditGrading .. ");
		ServiceStatus serviceStatus = new ServiceStatus(); 		 
		String status = auditGradingService.saveAuditGrading(auditGradingDto);
		if (BankAuditConstant.SUCCESS.equalsIgnoreCase(status)) {
			serviceStatus.setResult(auditGradingDto);
			serviceStatus.setStatus("success");
			serviceStatus.setMessage("Saved Successfully");
		} else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("failure");
		} 
		return serviceStatus;
	}
	 
	
@RequestMapping(value = "/getGradingParamScoreMatrixValue", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus getGradingParamScoreMatrixValue(
			@RequestParam(value = "legalEntityCode") Integer legalEntityCode,
			@RequestParam(required=true,value = "auditTypeCode") String auditTypeCode,
			@RequestParam(required=true,value = "score") Double score) {
		
		logger.info("Inside initiateAuditGrading .. "+legalEntityCode +" :: "+auditTypeCode+" :: "+score );
		ServiceStatus serviceStatus = new ServiceStatus(); 		 
		MaintGradingParamScoreMatrix maintGradingParamScoreMatrix = auditGradingService.getGradingParamScoreMatrixValue(legalEntityCode, auditTypeCode, score);
		if (maintGradingParamScoreMatrix != null && !BankAuditUtil.isEmptyString(maintGradingParamScoreMatrix.getGrading_value())) {
			serviceStatus.setResult(maintGradingParamScoreMatrix);
			serviceStatus.setStatus("success");
			serviceStatus.setMessage("Successfully retrieved ");
		} else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("no records found ");
		} 
		return serviceStatus;
	}
	
	
@RequestMapping(value = "/getMyAuditGradingCompliance", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus getMyAuditGradingCompliance(@RequestParam(value = "legalEntityCode") Integer legalEntityCode,
			@RequestParam(required=true,value = "auditTypeCode") String auditTypeCode,
			@RequestParam(required=true,value = "auditId") String auditId, 
			@RequestParam(required=true,value = "letterCode") String letterCode) {
		
		logger.info("Inside getMyAuditGradingCompliance .. "+legalEntityCode +" :: "+auditTypeCode+" :: "+auditId );
		ServiceStatus serviceStatus = new ServiceStatus(); 		 
		AuditGradingDto auditGradingDto = auditGradingService.getMyAuditGradingCompliance(legalEntityCode, auditTypeCode, auditId, letterCode);
		if (auditGradingDto != null) {
			serviceStatus.setResult(auditGradingDto);
			serviceStatus.setStatus("success");
			serviceStatus.setMessage("Successfully retrieved ");
		} else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("no records found ");
		} 
		return serviceStatus;
	}
	
	
@RequestMapping(value = "/saveAuditGradingCompliance", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE, consumes=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus saveAuditGradingCompliance(@RequestBody AuditGradingDto auditGradingDto) {
		logger.info("Inside saveAuditGrading .. ");
		ServiceStatus serviceStatus = new ServiceStatus(); 		 
		String status = auditGradingService.saveAuditGradingCompliance(auditGradingDto);
		if (BankAuditConstant.SUCCESS.equalsIgnoreCase(status)) {
			serviceStatus.setResult(auditGradingDto);
			serviceStatus.setStatus("success");
			serviceStatus.setMessage("Saved Successfully");
		} else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("failure");
		} 
		return serviceStatus;
	}
}
