/*
 * 
 */
package com.bankaudit.rest.controller;

import java.util.List;

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.DataTableResponse;
import com.bankaudit.dto.process.MaintRatingModelDto;
import com.bankaudit.model.MaintFormula;
import com.bankaudit.model.MaintRatingCategory;
import com.bankaudit.model.MaintRatingGrade;
import com.bankaudit.model.MaintRatingModelWrk;
import com.bankaudit.model.MaintRatingSubCategory;
import com.bankaudit.process.model.IfcRatingParameterMapping;
import com.bankaudit.service.MaintRatingModelService;
import com.bankaudit.util.BankAuditUtil;
import com.bankaudit.util.ServiceStatus;

 

@RestController

@RequestMapping("/api/ratingModel")
public class MaintRatingModelController {

 
	@Autowired
	MaintRatingModelService maintRatingModelService;
	
	
@RequestMapping(value = "/getAll", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	DataTableResponse getAllRatingModel(
			@RequestParam("draw") String drawStr,
			@RequestParam("length") String length,
			@RequestParam("start") String start,
			@RequestParam("search[value]") String search,
			@RequestParam("order[0][column]") String orderColumnStr,
			@RequestParam("order[0][dir]") String orderDirection,
			@RequestParam("legalEntityCode")Integer legalEntityCode ){

		DataTableResponse dataTableResponse = null;

		if (!BankAuditUtil.isEmptyString(drawStr) && !BankAuditUtil.isEmptyString(length)
				&& !BankAuditUtil.isEmptyString(start)) {
			try {
				Integer draw = Integer.parseInt(drawStr);
				Integer size = Integer.parseInt(length);
				Integer page = Integer.parseInt(start) / size;
				Integer orderColumn = null;
				if (!BankAuditUtil.isEmptyString(orderColumnStr)) {
					orderColumn = Integer.parseInt(orderColumnStr);
				} 
				dataTableResponse = maintRatingModelService.getAllRatingModel(legalEntityCode, search, orderColumn, orderDirection, page, size);
		 
				dataTableResponse.setDraw(draw);

			} catch (Exception e) {
				dataTableResponse = new DataTableResponse();
				dataTableResponse.setError(e.getMessage());
				e.printStackTrace();
			}

		} else {
			dataTableResponse = new DataTableResponse();
			dataTableResponse.setError("invalid data");
		}

		return dataTableResponse;
	}
	
	
@RequestMapping(value = "/getRatingModelBy", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus getRatingModelBy( 
			@RequestParam("legalEntityCode")Integer legalEntityCode,
			@RequestParam("rmCode")String rmCode,
			@RequestParam("status")String status){

		ServiceStatus serviceStatus=new ServiceStatus();

		if (legalEntityCode != null) {
			try {				
				
				MaintRatingModelDto maintRatingModelDto = maintRatingModelService.getRatingModelParameterDetails(legalEntityCode, rmCode, status);
				if(maintRatingModelDto!=null){
					serviceStatus.setResult(maintRatingModelDto);
					serviceStatus.setStatus("success");
					serviceStatus.setMessage("record fetched successfully");
				}else {
					serviceStatus.setStatus("success");
					serviceStatus.setMessage("no records found");				
				}				
			} catch (Exception e) {
				e.printStackTrace();
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failed to fetch");			
			}	
		} else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid data"); 
		}
		return serviceStatus;
	}
	 
	
@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus saveRatingModel(@RequestBody MaintRatingModelDto maintRatingModelDto) {

		ServiceStatus serviceStatus=new ServiceStatus();	 
		try{
			MaintRatingModelWrk maintRatingModelWrk = maintRatingModelService.saveRatingModelParameterDetails(maintRatingModelDto); 
			if(maintRatingModelWrk!=null && !BankAuditUtil.isEmptyString(maintRatingModelWrk.getRmCode())){	
				serviceStatus.setResult(maintRatingModelWrk);
				serviceStatus.setStatus("success");
				serviceStatus.setMessage("successfully save");
			}else {
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failed to save");				
			}				
		} catch (Exception e) {
			e.printStackTrace();
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("failure");			
		}	
		return serviceStatus;
	}
	
	
@RequestMapping(method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus updateRatingModel(@RequestBody MaintRatingModelDto maintRatingModelDto) {

		ServiceStatus serviceStatus=new ServiceStatus();	 
		try{
			MaintRatingModelWrk maintRatingModelWrk =  maintRatingModelService.updateRatingModelParameterDetails(maintRatingModelDto);	 
			if(maintRatingModelWrk!=null && !BankAuditUtil.isEmptyString(maintRatingModelWrk.getRmCode())){		
				serviceStatus.setResult(maintRatingModelWrk);
				serviceStatus.setStatus("success");
				serviceStatus.setMessage("successfully save");
			}else {
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failed to save");				
			}				
		} catch (Exception e) {
			e.printStackTrace();
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("failure");			
		}	
		return serviceStatus;
	}	 	
	
	
@RequestMapping(value = "/authorizeRatingModel", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus authorizeRatingModelParameterDetails(@RequestBody  MaintRatingModelWrk maintRatingModelWrk) { 
		ServiceStatus serviceStatus=new ServiceStatus();	 
		try{
			
			String status =  maintRatingModelService.authorizeRatingModelParameterDetails(maintRatingModelWrk);	 
			if(status.equalsIgnoreCase(BankAuditConstant.SUCCESS)){		
				//serviceStatus.setResult(maintRatingModelWrk);
				serviceStatus.setStatus("success");
				serviceStatus.setMessage("successfully save");
			}else {
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failed to save");				
			}				
		} catch (Exception e) {
			e.printStackTrace();
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("failure");			
		}	
		return serviceStatus;
	}	
	
	
@RequestMapping(value = "/getInputValues", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus getInputValues(
			@RequestParam("legalEntityCode")Integer legalEntityCode, 
			@RequestParam(value="auditTypeCode", required=false)String auditTypeCode){
		ServiceStatus serviceStatus = new ServiceStatus();
		if (legalEntityCode != null) {
			try {				
				
				List<IfcRatingParameterMapping> inputList = maintRatingModelService.getInputValues(legalEntityCode, auditTypeCode);
				if(inputList!=null){
					serviceStatus.setResult(inputList);
					serviceStatus.setStatus("success");
					serviceStatus.setMessage("record fetched successfully");
				}else {
					serviceStatus.setStatus("success");
					serviceStatus.setMessage("no records found");				
				}				
			} catch (Exception e) {
				e.printStackTrace();
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failed to fetch");			
			}	
		} else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid data"); 
		}
		return serviceStatus; 
	}
	
	
@RequestMapping(value = "/getFactor", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus getFactor(
			@RequestParam("legalEntityCode")Integer legalEntityCode, 
			@RequestParam(value ="noOfInputs" ,required=false)Integer noOfInputs){
		ServiceStatus serviceStatus = new ServiceStatus();
		if (legalEntityCode != null) {
			try {				
				
				List<MaintFormula> maintFormulaList = maintRatingModelService.getFactor(legalEntityCode, noOfInputs);
				if(maintFormulaList!=null){
					serviceStatus.setResult(maintFormulaList);
					serviceStatus.setStatus("success");
					serviceStatus.setMessage("record fetched successfully");
				}else {
					serviceStatus.setStatus("success");
					serviceStatus.setMessage("no records found");				
				}				
			} catch (Exception e) {
				e.printStackTrace();
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failed to fetch");			
			}	
		} else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid data"); 
		}
		return serviceStatus; 
	}
	
	
@RequestMapping(value = "/getGrades", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus getGrades(
			@RequestParam("legalEntityCode")Integer legalEntityCode, 
			@RequestParam(value="rmCode", required=false)String rmCode){
		 
		ServiceStatus serviceStatus = new ServiceStatus();
		if (legalEntityCode != null) {
			try {							
				List<MaintRatingGrade> maintRatingGradeList = maintRatingModelService.getGrades(legalEntityCode, rmCode);
				if(maintRatingGradeList!=null && maintRatingGradeList.size()>0 ){
					serviceStatus.setResult(maintRatingGradeList);
					serviceStatus.setStatus("success");
					serviceStatus.setMessage("record fetched successfully");
				}else {
					serviceStatus.setResult(maintRatingGradeList);
					serviceStatus.setStatus("success");
					serviceStatus.setMessage("no records found");				
				}				
			} catch (Exception e) {
				e.printStackTrace();
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failed to fetch");			
			}	
		} else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid data"); 
		}
		return serviceStatus; 
	}
	
	
@RequestMapping(value = "/getRatingCategory", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus getRatingCategory(
			@RequestParam("legalEntityCode")Integer legalEntityCode ){ 
		
		ServiceStatus serviceStatus = new ServiceStatus();
		if (legalEntityCode != null) {
			try {							
				List<MaintRatingCategory>  maintRatingCategoryList = maintRatingModelService.getRatingCategory(legalEntityCode);
				if(maintRatingCategoryList!=null){
					serviceStatus.setResult(maintRatingCategoryList);
					serviceStatus.setStatus("success");
					serviceStatus.setMessage("record fetched successfully");
				}else {
					serviceStatus.setStatus("success");
					serviceStatus.setMessage("no records found");				
				}				
			} catch (Exception e) {
				e.printStackTrace();
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failed to fetch");			
			}	
		} else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid data"); 
		}
		return serviceStatus; 
	}
	
	
@RequestMapping(value = "/getRatingSubCategory", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus getRatingSubCategory(
			@RequestParam("legalEntityCode")Integer legalEntityCode,  
			@RequestParam(value="categoryCode", required=false)String categoryCode){
		 
		ServiceStatus serviceStatus = new ServiceStatus();
		if (legalEntityCode != null) {
			try {							
				List<MaintRatingSubCategory>  maintRatingSubCategoryList = maintRatingModelService.getRatingSubCategory(
							legalEntityCode, categoryCode);
				if(maintRatingSubCategoryList!=null){
					serviceStatus.setResult(maintRatingSubCategoryList);
					serviceStatus.setStatus("success");
					serviceStatus.setMessage("record fetched successfully");
				}else {
					serviceStatus.setStatus("success");
					serviceStatus.setMessage("no records found");				
				}				
			} catch (Exception e) {
				e.printStackTrace();
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failed to fetch");			
			}	
		} else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid data"); 
		}
		return serviceStatus; 
	}
	
	
@RequestMapping(value = "/deleteRatingParameterDetails", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus deleteRatingParameterDetails(
			@RequestParam("legalEntityCode")Integer legalEntityCode,  
			@RequestParam("rmCode")String rmCode,
			@RequestParam("paramCode")String paramCode){
		 
		ServiceStatus serviceStatus = new ServiceStatus();
		if (legalEntityCode != null) {
			try {							
				maintRatingModelService.deleteRatingParameterDetailsBy(legalEntityCode, rmCode, paramCode, "");
				serviceStatus.setStatus("success");
				serviceStatus.setMessage("record deleted successfully");				 			
			} catch (Exception e) {
				e.printStackTrace();
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failed to delete record");			
			}	
		} else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid data"); 
		}
		return serviceStatus; 
	}
	
	
@RequestMapping(value = "/deleteRatingParameterRange", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus deleteRatingParameterRangeBy(
			@RequestParam("legalEntityCode")Integer legalEntityCode,  
			@RequestParam("rmCode")String rmCode,
			@RequestParam("paramCode")String paramCode,
			@RequestParam("id")String id){
		 
		ServiceStatus serviceStatus = new ServiceStatus();
		if (legalEntityCode != null) {
			try {							
				maintRatingModelService.deleteRatingParameterRangeBy(legalEntityCode, rmCode, paramCode, id, "");
				serviceStatus.setStatus("success");
				serviceStatus.setMessage("record deleted successfully");				 			
			} catch (Exception e) {
				e.printStackTrace();
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failed to delete record");			
			}	
		} else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid data"); 
		}
		return serviceStatus; 
	}
	
	
@RequestMapping(value = "/deleteRatingParameterCategoryMapping", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus deleteRatingParameterCategoryMappingBy(
			@RequestParam("legalEntityCode")Integer legalEntityCode,  
			@RequestParam("rmCode")String rmCode,
			@RequestParam("paramCode")String paramCode,
			@RequestParam("categoryCode")String categoryCode,
			@RequestParam("subCategoryCode")String subCategoryCode){
		 
		ServiceStatus serviceStatus = new ServiceStatus();
		if (legalEntityCode != null) {
			try {							
				maintRatingModelService.deleteRatingParameterCategoryMappingBy(legalEntityCode, rmCode, paramCode, categoryCode, subCategoryCode, null);
				serviceStatus.setStatus("success");
				serviceStatus.setMessage("record deleted successfully");				 			
			} catch (Exception e) {
				e.printStackTrace();
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failed to delete record");			
			}	
		} else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid data"); 
		}
		return serviceStatus; 
	}
	
	
@RequestMapping(value = "/deleteRatingParameterModelOverlay", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus deleteRatingParameterModelOverlayBy(
			@RequestParam("legalEntityCode")Integer legalEntityCode,  
			@RequestParam("rmCode")String rmCode,
			@RequestParam("categoryCode")String categoryCode,
			@RequestParam("level")String level,
			@RequestParam("criteriaCode")String criteriaCode){
		 
		ServiceStatus serviceStatus = new ServiceStatus();
		if (legalEntityCode != null) {
			try {							
				maintRatingModelService.deleteRatingParameterModelOverlayBy(legalEntityCode, rmCode, level, categoryCode, criteriaCode, null);
				serviceStatus.setStatus("success");
				serviceStatus.setMessage("record deleted successfully");				 			
			} catch (Exception e) {
				e.printStackTrace();
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failed to delete record");			
			}	
		} else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid data"); 
		}
		return serviceStatus; 
	}   
	 
}
