package com.bankaudit.rest.process.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.jboss.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bankaudit.dto.process.AuditRatingCompositeScoreDto;
import com.bankaudit.dto.process.AuditRatingScoreDto;
import com.bankaudit.model.MaintRatingEscalationMatrix;
import com.bankaudit.process.model.AuditRatingCompositeCategoryScore;
import com.bankaudit.process.model.AuditRatingCompositeOverlay;
import com.bankaudit.process.model.AuditRatingCompositeScore;
import com.bankaudit.process.model.AuditSchedule;
import com.bankaudit.rest.process.dao.AuditRatingCompositeDao;
import com.bankaudit.rest.process.dao.IfcEnsureBaasDataDao;
import com.bankaudit.rest.process.dao.MyAuditsDao;
import com.bankaudit.service.BusinessDateService;

@Service("auditRatingCompositeService")
@Transactional("transactionManager")
public class AuditRatingCompositeServiceImpl implements AuditRatingCompositeService{

	@Autowired(required=true)
	MyAuditsDao myAuditsDao;
	
	@Autowired(required=true)
	AuditRatingCompositeDao auditRatingCompositeDao;
	
	@Autowired(required=true)
	IfcEnsureBaasDataDao ifcEnsureBaasDataDao;
	
	@Autowired
	BusinessDateService businessDateService;
		
	static final Logger logger = Logger.getLogger(AuditRatingCompositeServiceImpl.class);

	@Override
	public AuditRatingCompositeScoreDto getCompositeScoreByAuditId(int legalEntityCode,String auditId, String auditTypeCode, String unitId, String userType, String userId, String compRatingStatus) {
		logger.info("Inside getCompositeScoreByAuditId .. "+ auditId+" :: "+ auditTypeCode+" :: "+userType+" :: "+userId+" :: "+ compRatingStatus);
		if(compRatingStatus==null || "null".equalsIgnoreCase(compRatingStatus)) {
			boolean isDataPresent= auditRatingCompositeDao.verifyDataPresentForCompositeScore(legalEntityCode, auditId); 
			logger.info("Inside getCompositeScoreByAuditId Data exist for :: "+ isDataPresent);
			if(!isDataPresent) {
				boolean success=auditRatingCompositeDao.createAndGetAuditRatingCompositeScore(legalEntityCode, auditId, auditTypeCode, unitId, userId);
				/** For exception scenario, like Biannual **/
				boolean isAuditBiAnnual=ifcEnsureBaasDataDao.isAuditBiAnnual(legalEntityCode, auditId, auditTypeCode);
				logger.info("isAuditBiAnnual .. "+isAuditBiAnnual);
				if(isAuditBiAnnual)
					auditRatingCompositeDao.updateExceptionAuditRatingCompositeScores(legalEntityCode, auditId, auditTypeCode, userType); //Updating the score for exception cases
				auditRatingCompositeDao.createAuditRatingCompositeCategoryScore(legalEntityCode, auditId, auditTypeCode, unitId, userId);
				auditRatingCompositeDao.createAuditRatingCompositeOverlay(legalEntityCode, auditId, auditTypeCode, userId);
				auditRatingCompositeDao.fetchAndUpdateAuditCompositeScoresFromSupervisoryRating(legalEntityCode, auditId, userType); // To fetch the Rating from Supervisory and update in Composite 
			}
		}
		
		//Logic to get Category score and then Parameter scores grouped under Category
		List<AuditRatingCompositeScore> compScrLst=auditRatingCompositeDao.getAuditRatingCompositeScoreByAuditId(legalEntityCode,auditId,auditTypeCode,userType);
		List<AuditRatingCompositeCategoryScore> catScoreLst=auditRatingCompositeDao.getAuditRatingCompositeCategoryScoreByAuditId(legalEntityCode,auditId, auditTypeCode, userType);
		for(AuditRatingCompositeCategoryScore catScore: catScoreLst) {
			catScore.setAuditRatingCompositeScoreLst((List<AuditRatingCompositeScore>)compScrLst.stream().filter(p -> p.getCategoryCode().equals(catScore.getCategoryCode())).collect(Collectors.toList()));
		}
		AuditRatingCompositeScoreDto compScoreDto= new AuditRatingCompositeScoreDto();
		compScoreDto.setAuditId(auditId);compScoreDto.setLegalEntityCode(legalEntityCode);
		compScoreDto.setCompCategryScoreLst(catScoreLst);
		//Logic end here
		
		if(auditTypeCode.contains("StCB")) {
			boolean isScheduledStcb=ifcEnsureBaasDataDao.isScheduledStcb(legalEntityCode, auditId, auditTypeCode);
			logger.info("Inside getParams isScheduledStcb .. "+isScheduledStcb);
			compScoreDto.setScheduledBank(isScheduledStcb);
		}
		
		return compScoreDto;		 
	}
	
	@Override
	public boolean verifyCSVUpload(int legalEntityCode,String auditId) {
		//boolean isDataPresent= auditRatingParameterScoreDao.verifyDataPresentForParamScore(legalEntityCode, auditId); 
		/*if(!isDataPresent)*/ boolean isDataPresent=auditRatingCompositeDao.verifyCSVUpload(legalEntityCode, auditId); 
		return isDataPresent;
	}
	
	@Override
	public List<AuditRatingCompositeScore> insertAndGetAuditCompositeRatingByAuditId(int legalEntityCode,String auditId, String unitId, String auditTypeCode, String userId) {
		List<AuditRatingCompositeScore> compScrLst=null;
		boolean isDataPresent= auditRatingCompositeDao.verifyDataPresentForCompositeScore(legalEntityCode, auditId); 
		logger.info("Data exist for :: "+ auditId+" :: "+ isDataPresent);
		if(!isDataPresent) {
			boolean success=auditRatingCompositeDao.createAndGetAuditRatingCompositeScore(legalEntityCode, auditId, unitId, auditTypeCode, userId);
			auditRatingCompositeDao.createAuditRatingCompositeCategoryScore(legalEntityCode, auditId, unitId, auditTypeCode, userId);
			auditRatingCompositeDao.createAuditRatingCompositeOverlay(legalEntityCode, auditId, auditTypeCode, userId);
		    if(success) compScrLst= auditRatingCompositeDao.getAuditRatingCompositeScoreByAuditId(legalEntityCode,auditId,auditTypeCode,"PIO");
		}
		return compScrLst;
	}
	
	@Override
	public void executeAndUpdateAuditCompositeScoresFromIRCSV(int legalEntityCode, String auditId, String userType) {
		auditRatingCompositeDao.executeAndUpdateAuditCompositeScoresFromIRCSV(legalEntityCode, auditId, userType); // Updating Composite Score after IR CSV upload
		auditRatingCompositeDao.executeAndUpdateAuditCompositeOverlayFromIRCSV(legalEntityCode, auditId, userType); // Updating Overlay after IR CSV upload 		
	
		boolean isAuditBiAnnual=ifcEnsureBaasDataDao.isAuditBiAnnual(legalEntityCode, auditId, null);
		logger.info("isAuditBiAnnual .. "+isAuditBiAnnual);
		if(isAuditBiAnnual)
			auditRatingCompositeDao.updateExceptionAuditRatingCompositeScores(legalEntityCode, auditId, null, userType); //Updating the score for exception cases
		
	}
	
	@Override
	public void fetchAndUpdateAuditCompositeScoresFromSupervisoryRating(int legalEntityCode, String auditId, String userType) {
		auditRatingCompositeDao.fetchAndUpdateAuditCompositeScoresFromSupervisoryRating(legalEntityCode, auditId, userType); // Updating Composite Score after IR CSV upload
		//auditRatingCompositeDao.executeAndUpdateAuditCompositeOverlayFromIRCSV(legalEntityCode, auditId, userType); // Updating Overlay after IR CSV upload 		
	}
	
	@Override
	public void updateAuditRatingCompositeScores(List<AuditRatingCompositeScore> auditRatingCompositeScores, int legalEntityCode, String auditId, String userType) {
		auditRatingCompositeDao.updateAuditRatingCompositeScores(auditRatingCompositeScores, legalEntityCode, auditId, userType); 
	}
	
	@Override
	public void updateAuditRatingCompositeCategoryScores(List<AuditRatingCompositeCategoryScore> auditRatingCompositeCategoryScores, int legalEntityCode, String auditId, String userType) {
		auditRatingCompositeDao.updateAuditRatingCompositeCategoryScores(auditRatingCompositeCategoryScores, legalEntityCode, auditId, userType); 
	}
	
	@Override
	public void updateAuditRatingCompositeOverlay(List<AuditRatingCompositeOverlay> auditRatingCompositeOverlay, int legalEntityCode, String auditId, String userId, String userType) {
		auditRatingCompositeDao.updateAuditRatingCompositeOverlay(auditRatingCompositeOverlay, legalEntityCode, auditId,userId, userType); 
	}
	
	@Override
	public List<AuditRatingCompositeCategoryScore> getCompositeCategoryScoreByAuditId(int legalEntityCode,String auditId, String auditTypeCode, String userType, String compRatingStatus) {
		logger.info("Inside getCompositeScoreByAuditId .. "+ auditId+" :: "+ auditTypeCode+" :: "+userType+" :: "+ compRatingStatus);
		//List<AuditRatingCompositeScore> cmpScrs= auditRatingCompositeDao.getAuditRatingCompositeScoreOnlyByAuditId(legalEntityCode, auditId,  auditTypeCode,  userType);
		List<AuditRatingCompositeCategoryScore> cmpCatScrLst= auditRatingCompositeDao.getAuditRatingCompositeCategoryScoreByAuditId(legalEntityCode, auditId,  auditTypeCode,  userType);
		Map<String, String> paramMap=auditRatingCompositeDao.getParamScoreMapByAuditId(legalEntityCode,auditId, auditTypeCode, userType);
		boolean ovrLayYes= auditRatingCompositeDao.isRatingCompositeOverlayApplied(legalEntityCode, auditId);
		logger.info("Overlay applied .."+ ovrLayYes);
		
		for(AuditRatingCompositeCategoryScore catLst: cmpCatScrLst) {
			String catCode=catLst.getCategoryCode();
			if(catCode.equalsIgnoreCase("CT011")) {
				try {
					double p01=Double.parseDouble(paramMap.get("P0001"));
					if(p01>10)catLst.setCategoryRating("III");
					else if(p01 >3 && p01 <= 10 )catLst.setCategoryRating("II");
					else if(p01 < 3 )catLst.setCategoryRating("I");
					else catLst.setCategoryRating("Error");
				}catch(Exception e) {
					catLst.setCategoryRating("Error");
					e.printStackTrace(System.out);}
			}else if(catCode.equalsIgnoreCase("CT012")) {
				try {
					double p02=Double.parseDouble(paramMap.get("P0002"));
					if(p02 < 9)catLst.setCategoryRating("III");
					else if(p02 >=9 && p02 < 10.5 )catLst.setCategoryRating("II");
					else if(p02 >= 10.5 )catLst.setCategoryRating("I");
					else catLst.setCategoryRating("Error");
				}catch(Exception e) {
					catLst.setCategoryRating("Error");
					e.printStackTrace(System.out);}
			}else if(catCode.equalsIgnoreCase("CT013")) {
				try {
					double p01=Double.parseDouble(paramMap.get("P0001"));
				}catch(Exception e) {e.printStackTrace(System.out);}
			}else if(catCode.equalsIgnoreCase("CT014")) {
				
			}else if(catCode.equalsIgnoreCase("CT015")) {
				
			}else if(catCode.equalsIgnoreCase("CT016")) {
				
			}else if(catCode.equalsIgnoreCase("CT017")) {
				
			}else if(catCode.equalsIgnoreCase("CT018")) {
				
			}
			
			// Apply overlay on the Category value
			if(ovrLayYes) {
				if("Error".equalsIgnoreCase(catLst.getCategoryRating())) catLst.setCategoryRatingAftrOverlay("Error");
				if("IV".equalsIgnoreCase(catLst.getCategoryRating())) catLst.setCategoryRatingAftrOverlay("III");
				if("III".equalsIgnoreCase(catLst.getCategoryRating())) catLst.setCategoryRatingAftrOverlay("II");
				if("II".equalsIgnoreCase(catLst.getCategoryRating())) catLst.setCategoryRatingAftrOverlay("I");
				if("I".equalsIgnoreCase(catLst.getCategoryRating())) catLst.setCategoryRatingAftrOverlay("I");
			}
		}
		
		return cmpCatScrLst;
	}
	
	// Getting the Composite category score and Escalation matrix together
	@Override
	public AuditRatingCompositeScoreDto getCompositeCategoryScoreEscalationMtrxByAuditId(int legalEntityCode,String auditId, String auditTypeCode, String userType, String compRatingStatus) {
		logger.info("Inside getCompositeScoreByAuditId .. "+ auditId+" :: "+ auditTypeCode+" :: "+userType+" :: "+ compRatingStatus);
		AuditRatingCompositeScoreDto cmpScoreDto= new AuditRatingCompositeScoreDto();
		
		//List<AuditRatingCompositeScore> cmpScrs= auditRatingCompositeDao.getAuditRatingCompositeScoreOnlyByAuditId(legalEntityCode, auditId,  auditTypeCode,  userType);
		List<AuditRatingCompositeCategoryScore> cmpCatScrLst= auditRatingCompositeDao.getAuditRatingCompositeCategoryScoreByAuditId(legalEntityCode, auditId,  auditTypeCode,  userType);
		Map<String, String> paramMap=auditRatingCompositeDao.getParamScoreMapByAuditId(legalEntityCode,auditId, auditTypeCode, userType);
		boolean ovrLayYes= auditRatingCompositeDao.isRatingCompositeOverlayApplied(legalEntityCode, auditId);
		logger.info("Overlay applied .."+ ovrLayYes+" :: "+ auditTypeCode);
		Map lowestMap= new HashMap<String, String>();
		double catPrcntgTotal=0.0, catPrcntgTotalOneBelow=0.0, finalPrcntg=0.0;
		boolean isScheduledStcb=false;
		
		if(auditTypeCode.contains("StCB")) {
			isScheduledStcb=ifcEnsureBaasDataDao.isScheduledStcb(legalEntityCode, auditId, auditTypeCode);
			logger.info("isScheduledStcb .. "+isScheduledStcb);
		}
		
		try {
			for(AuditRatingCompositeCategoryScore catLst: cmpCatScrLst) {
				String catCode=catLst.getCategoryCode();
				
				if("RRBEI".equalsIgnoreCase(auditTypeCode)) {  // for RRBEI audit type
					if(catCode.equalsIgnoreCase("CT011")) { // Non-Performing Assets
						try {
							double p01=Double.parseDouble(paramMap.get("P0001"));
							if(p01>10)catLst.setCategoryRating("III");
							else if(p01 >3 && p01 <= 10 ) catLst.setCategoryRating("II");
							else if(p01 <= 3 )catLst.setCategoryRating("I");
							else catLst.setCategoryRating("Error");
						}catch(Exception e) {
							catLst.setCategoryRating("Error");
							e.printStackTrace(System.out);}
					}else if(catCode.equalsIgnoreCase("CT012")) { // CRAR
						try {
							double p02=Double.parseDouble(paramMap.get("P0002"));
							if(p02 < 9)catLst.setCategoryRating("III");
							else if(p02 >=9 && p02 < 10.5 )catLst.setCategoryRating("II");
							else if(p02 >= 10.5 )catLst.setCategoryRating("I");
							else catLst.setCategoryRating("Error");
						}catch(Exception e) {
							catLst.setCategoryRating("Error");
							e.printStackTrace(System.out);}
					}else if(catCode.equalsIgnoreCase("CT013")) { // Erosion
						try {
							double p05=Double.parseDouble(paramMap.get("P0005")); // Balance in Profit and Loss account
							double p06=Double.parseDouble(paramMap.get("P0006")); // Free Reserves
							
							double p03=Double.parseDouble(paramMap.get("P0003"));
							double p04=Double.parseDouble(paramMap.get("P0004"));
							double p07=Double.parseDouble(paramMap.get("P0007")); // Paid-up Capital (%)
							double p08=Double.parseDouble(paramMap.get("P0008")); // Deposits (%)
							double p09=Double.parseDouble(paramMap.get("P0009")); // BOrrowing (%)
							
							if(p06 > 50)catLst.setCategoryRating("III");
							else if(p06 >0 && p06 <= 50 )catLst.setCategoryRating("II");
							else if(p06 == 0 && p05 <=100 )catLst.setCategoryRating("I");
							else if(p03 == 0 && p04 ==0 && p05 == 0 && p06 ==0 && p07 == 0 && p08 ==0 && p09 ==0 )catLst.setCategoryRating("I");
							else catLst.setCategoryRating("Error");
							
							/*
							if(p06 > 50)catLst.setCategoryRating("III");
							else if(p06 >0 && p06 <= 50 )catLst.setCategoryRating("II");
							else if(p06 == 0 && p05 <=100 )catLst.setCategoryRating("I");
							else if(p03 == 0 && p04 ==0 && p05 == 0 && p06 ==0 && p07 == 0 && p08 ==0 && p09 ==0 )catLst.setCategoryRating("I");
							else catLst.setCategoryRating("Error");*/
						}catch(Exception e) {e.printStackTrace(System.out);}
					}else if(catCode.equalsIgnoreCase("CT014")) {  // Profitability
						try {
							double p11=Double.parseDouble(paramMap.get("P0011"));
							double p11_II=Double.parseDouble(paramMap.get("P0011_II"));
							if(p11 < 0 && p11_II < 0  )catLst.setCategoryRating("III");
							else if(p11 < 0 ||  p11_II < 0 )catLst.setCategoryRating("II");
							else if(p11 >= 0 &&  p11_II >= 0 )catLst.setCategoryRating("I");
							else catLst.setCategoryRating("Error");
						}catch(Exception e) {e.printStackTrace(System.out);}
					}else if(catCode.equalsIgnoreCase("CT015")) {  // Management & Governance
						try {
							double p12=Double.parseDouble(paramMap.get("P0012"));
							String p13=paramMap.get("P0013"), p14=paramMap.get("P0014"), p15=paramMap.get("P0015"), p16=paramMap.get("P0016");
							String p17=paramMap.get("P0017"), p18=paramMap.get("P0018"), p19=paramMap.get("P0019"), p20=paramMap.get("P0020");
							if(p12 >= 80 && p13.equalsIgnoreCase("Yes") && p14.equalsIgnoreCase("Yes") && p15.equalsIgnoreCase("Yes") && p16.equalsIgnoreCase("Yes") && p17.equalsIgnoreCase("Yes") && p18.equalsIgnoreCase("Yes")  
									&& p19.equalsIgnoreCase("Yes")  && p20.equalsIgnoreCase("Yes") )catLst.setCategoryRating("I");
							else if(p12 >= 80 && (p13.equalsIgnoreCase("No") || p14.equalsIgnoreCase("No") || p15.equalsIgnoreCase("No") || p16.equalsIgnoreCase("No") || p17.equalsIgnoreCase("No") || p18.equalsIgnoreCase("No")  
									|| p19.equalsIgnoreCase("No")  || p20.equalsIgnoreCase("No")))catLst.setCategoryRating("II");
							else if(p12 >= 60 && p12 < 80) catLst.setCategoryRating("II");
							else if(p12 < 60) catLst.setCategoryRating("III");
							else catLst.setCategoryRating("Error");
						}catch(Exception e) {e.printStackTrace(System.out);}
					}else if(catCode.equalsIgnoreCase("CT016")) {  // Liquidity
						try {
							// D: Default for less than 2 fortnights (including technical default)
							// F: Frequent/Continuous default for 2 fortnights and above     OR   F: Frequent/Continuous
							// NDD: Not able to meet depositors demand
							String p21=paramMap.get("P0021"), p22=paramMap.get("P0022"), p23=paramMap.get("P0023"), p24=paramMap.get("P0024");
							logger.info("CT016 .. "+ p21+" :: "+p22+" :: "+ p23+" :: "+p24);
							if(p21.equalsIgnoreCase("F") || p22.equalsIgnoreCase("F") || p23.equalsIgnoreCase("F") || p24.equalsIgnoreCase("NDD"))
								catLst.setCategoryRating("III");
							else if(p21.equalsIgnoreCase("D") || p22.equalsIgnoreCase("D") || p23.equalsIgnoreCase("Yes") || p24.equalsIgnoreCase("Yes"))
								catLst.setCategoryRating("II");
							else if(p21.equalsIgnoreCase("No") && p22.equalsIgnoreCase("No") && p23.equalsIgnoreCase("No") && p24.equalsIgnoreCase("No"))
								catLst.setCategoryRating("I");
							else catLst.setCategoryRating("Error");
						}catch(Exception e) {e.printStackTrace(System.out);}
					}else if(catCode.equalsIgnoreCase("CT017")) { // Systems (Internal checks & control)
						try {
							double p25=Double.parseDouble(paramMap.get("P0025"));
							String p26=paramMap.get("P0026"), p27=paramMap.get("P0027"), p28=paramMap.get("P0028");
							String p29=paramMap.get("P0029"), p30=paramMap.get("P0030");
							if(p25 >= 80 && p26.equalsIgnoreCase("Yes") && p27.equalsIgnoreCase("Yes") && p28.equalsIgnoreCase("Yes")  
									&& p29.equalsIgnoreCase("Yes")  && p30.equalsIgnoreCase("Yes") )catLst.setCategoryRating("I");
							else if(p25 >= 80 && (p26.equalsIgnoreCase("No") || p27.equalsIgnoreCase("No") || p28.equalsIgnoreCase("No")  
									|| p29.equalsIgnoreCase("No")  || p30.equalsIgnoreCase("No")))catLst.setCategoryRating("II");
							else if(p25 >= 60 && p25 < 80) catLst.setCategoryRating("II");
							else if(p25 < 60) catLst.setCategoryRating("III");
							else catLst.setCategoryRating("Error");
						}catch(Exception e) {e.printStackTrace(System.out);}
					}else if(catCode.equalsIgnoreCase("CT018")) { // Compliance
						try {
							double p31=Double.parseDouble(paramMap.get("P0031"));
							String p32=paramMap.get("P0032"), p33=paramMap.get("P0033"), p34=paramMap.get("P0034"), p35=paramMap.get("P0035");
							String p36=paramMap.get("P0036"), p37=paramMap.get("P0037"), p38=paramMap.get("P0038"), p39=paramMap.get("P0039");
							if(p31 >= 80 && p32.equalsIgnoreCase("Yes") && p33.equalsIgnoreCase("Yes") && p34.equalsIgnoreCase("Yes") && p35.equalsIgnoreCase("Yes") && p36.equalsIgnoreCase("Yes") && p37.equalsIgnoreCase("Yes")  
									&& p38.equalsIgnoreCase("Yes")  && p39.equalsIgnoreCase("Yes") )catLst.setCategoryRating("I");
							else if(p31 >= 80 && (p32.equalsIgnoreCase("No") || p33.equalsIgnoreCase("No") || p34.equalsIgnoreCase("No") || p35.equalsIgnoreCase("No") || p36.equalsIgnoreCase("No") || p37.equalsIgnoreCase("No")  
									|| p38.equalsIgnoreCase("No")  || p39.equalsIgnoreCase("No")))catLst.setCategoryRating("II");
							else if(p31 >= 60 && p31 < 80) catLst.setCategoryRating("II");
							else if(p31 < 60) catLst.setCategoryRating("III");
							else catLst.setCategoryRating("Error");
						}catch(Exception e) {e.printStackTrace(System.out);}
					}
				}else {  // for DCCBI, DCCBEI, StCBI & StCBEI audit type
					String pm01=null, pm02=null, pm03=null, pm04=null, pm05=null, pm06=null, pm07=null, pm08=null, pm09=null, pm10=null, pm11=null, pm12=null, pm13=null, pm14=null, pm14_II=null, pm14_III=null, pm15=null, pm16=null, pm17=null, pm18=null, pm19=null, pm20=null, pm21=null, pm22=null, pm23=null, pm24=null, pm25=null, pm26=null, pm27=null, pm28=null, pm29=null, pm30=null, pm31=null, pm32=null, pm33=null, pm34=null, pm35=null, pm36=null, pm37=null, pm38=null, pm39=null, pm40=null, pm41=null, pm385=null;  // pm385 added for the new Parameter
					if("DCCBI".equalsIgnoreCase(auditTypeCode)) {
						pm01="P0101"; pm02="P0102"; pm03="P0103"; pm04="P0104"; pm05="P0105"; pm06="P0106"; pm07="P0107"; pm08="P0108"; pm09="P0109"; pm10="P0110"; pm11="P0111"; pm12="P0112"; pm13="P0113"; pm14="P0114"; pm14_II="P0114_II"; pm14_III="P0114_III"; pm15="P0115"; pm16="P0116"; pm17="P0117"; pm18="P0118"; pm19="P0119"; pm20="P0120"; pm21="P0121"; pm22="P0122"; pm23="P0123"; pm24="P0124"; pm25="P0125"; pm26="P0126"; pm27="P0127"; pm28="P0128"; pm29="P0129"; pm30="P0130"; pm31="P0131"; pm32="P0132"; pm33="P0133"; pm34="P0134"; pm35="P0135"; pm36="P0136"; pm37="P0137"; pm38="P0138"; pm385="P01385"; pm39="P0139"; pm40="P0140"; pm41="P0141";       // pm385 added for the new Parameter
					}else if("DCCBEI".equalsIgnoreCase(auditTypeCode)) {
						pm01="P1101"; pm02="P1102"; pm03="P1103"; pm04="P1104"; pm05="P1105"; pm06="P1106"; pm07="P1107"; pm08="P1108"; pm09="P1109"; pm10="P1110"; pm11="P1111"; pm12="P1112"; pm13="P1113"; pm14="P1114"; pm14_II="P1114_II"; pm14_III="P1114_III"; pm15="P1115"; pm16="P1116"; pm17="P1117"; pm18="P1118"; pm19="P1119"; pm20="P1120"; pm21="P1121"; pm22="P1122"; pm23="P1123"; pm24="P1124"; pm25="P1125"; pm26="P1126"; pm27="P1127"; pm28="P1128"; pm29="P1129"; pm30="P1130"; pm31="P1131"; pm32="P1132"; pm33="P1133"; pm34="P1134"; pm35="P1135"; pm36="P1136"; pm37="P1137"; pm38="P1138"; pm385="P11385"; pm39="P1139"; pm40="P1140"; pm41="P1141";
					}else if("StCBI".equalsIgnoreCase(auditTypeCode)) {
						pm01="P0201"; pm02="P0202"; pm03="P0203"; pm04="P0204"; pm05="P0205"; pm06="P0206"; pm07="P0207"; pm08="P0208"; pm09="P0209"; pm10="P0210"; pm11="P0211"; pm12="P0212"; pm13="P0213"; pm14="P0214"; pm14_II="P0214_II"; pm14_III="P0214_III"; pm15="P0215"; pm16="P0216"; pm17="P0217"; pm18="P0218"; pm19="P0219"; pm20="P0220"; pm21="P0221"; pm22="P0222"; pm23="P0223"; pm24="P0224"; pm25="P0225"; pm26="P0226"; pm27="P0227"; pm28="P0228"; pm29="P0229"; pm30="P0230"; pm31="P0231"; pm32="P0232"; pm33="P0233"; pm34="P0234"; pm35="P0235"; pm36="P0236"; pm37="P0237"; pm38="P0238"; pm385="P02385"; pm39="P0239"; pm40="P0240"; pm41="P0241";
					}else if("StCBEI".equalsIgnoreCase(auditTypeCode)) {
						pm01="P1201"; pm02="P1202"; pm03="P1203"; pm04="P1204"; pm05="P1205"; pm06="P1206"; pm07="P1207"; pm08="P1208"; pm09="P1209"; pm10="P1210"; pm11="P1211"; pm12="P1212"; pm13="P1213"; pm14="P1214"; pm14_II="P1214_II"; pm14_III="P1214_III"; pm15="P1215"; pm16="P1216"; pm17="P1217"; pm18="P1218"; pm19="P1219"; pm20="P1220"; pm21="P1221"; pm22="P1222"; pm23="P1223"; pm24="P1224"; pm25="P1225"; pm26="P1226"; pm27="P1227"; pm28="P1228"; pm29="P1229"; pm30="P1230"; pm31="P1231"; pm32="P1232"; pm33="P1233"; pm34="P1234"; pm35="P1235"; pm36="P1236"; pm37="P1237"; pm38="P1238"; pm385="P12385"; pm39="P1239"; pm40="P1240"; pm41="P1241";
					}
					
					if(catCode.equalsIgnoreCase("CT011")) {	// Level of Non-Performing Assets				
						try {
							double p01=Double.parseDouble(paramMap.get(pm01));   // Gross NPA %
							double p02=Double.parseDouble(paramMap.get(pm02));   // Net NPA %
							/*if(p01<=5 || p02 <=3) { catLst.setCategoryRating("I"); catLst.setCategoryPrcntg("10"); catLst.setCategoryPrcntgOneBelow("6");}
							else if(p01<=15 || p02 <=10) {catLst.setCategoryRating("II"); catLst.setCategoryPrcntg("6"); catLst.setCategoryPrcntgOneBelow("3");}
							else if(p01<=25 || p02 <=15) {catLst.setCategoryRating("III"); catLst.setCategoryPrcntg("3"); catLst.setCategoryPrcntgOneBelow("0");}
							else if(p01 > 25 || p02 > 15){catLst.setCategoryRating("IV"); catLst.setCategoryPrcntg("0"); catLst.setCategoryPrcntgOneBelow("0");}*/
							if(p01<5 || (p01 >=5 && p01 <15 && p02 <3) ) { catLst.setCategoryRating("I"); catLst.setCategoryPrcntg("10"); catLst.setCategoryPrcntgOneBelow("6");}    		// (Gross NPA < 5% ) OR (5% <= Gross NPA <15% and Net NPA <3%)
							else if((p01 >=5 && p01 <15 && p02 >=3) || (p01 >=15 && p01 <25 && p02 <10)) {catLst.setCategoryRating("II"); catLst.setCategoryPrcntg("6"); catLst.setCategoryPrcntgOneBelow("3");}  // (5% <= Gross NPA <15% and Net NPA >= 3%)  OR  (15% <= Gross NPA <25% and Net NPA < 10%)
							else if(p01 >=15 && p01 <25 && p02 >=10 && p02 <15) {catLst.setCategoryRating("III"); catLst.setCategoryPrcntg("3"); catLst.setCategoryPrcntgOneBelow("0");}  	// 15% <= Gross NPA <25% AND 10% <=  Net NPA < 15%
							else if(p01 >= 25 || p02 >= 15){catLst.setCategoryRating("IV"); catLst.setCategoryPrcntg("0"); catLst.setCategoryPrcntgOneBelow("0");}  						// Gross NPA >= 25%  OR  Net NPA >= 15%
							else catLst.setCategoryRating("Error");
						}catch(Exception e) {
							catLst.setCategoryRating("Error");
							e.printStackTrace(System.out);}
					}else if(catCode.equalsIgnoreCase("CT012")) {	// Capital to Risk-weighted Assets Ratio (CRAR)			
						try {
							double p03=Double.parseDouble(paramMap.get(pm03));
							if(p03 >=9) { catLst.setCategoryRating("I"); catLst.setCategoryPrcntg("10"); catLst.setCategoryPrcntgOneBelow("6");}
							else if(p03 >=7) {catLst.setCategoryRating("II"); catLst.setCategoryPrcntg("6"); catLst.setCategoryPrcntgOneBelow("3");}
							else if(p03 >=4) {catLst.setCategoryRating("III"); catLst.setCategoryPrcntg("3"); catLst.setCategoryPrcntgOneBelow("0");}
							else if(p03 < 4) {catLst.setCategoryRating("IV"); catLst.setCategoryPrcntg("0"); catLst.setCategoryPrcntgOneBelow("0");}
							else catLst.setCategoryRating("Error");
						}catch(Exception e) {
							catLst.setCategoryRating("Error");
							e.printStackTrace(System.out);}
					}else if(catCode.equalsIgnoreCase("CT013")) {	// Erosion					
						try {
							double p04=Double.parseDouble(paramMap.get(pm04));  // Impact of Erosion on Loss Provisions including Revaluation Reserve (%)
							double p05=Double.parseDouble(paramMap.get(pm05));  // Impact of Erosion on Provision for standard assets (%)
							double p06=Double.parseDouble(paramMap.get(pm06));  // Impact of Erosion on Balance in Profit and Loss account (%)
							double p07=Double.parseDouble(paramMap.get(pm07));  // Impact of Erosion on Free Reserves (%)
							double p08=Double.parseDouble(paramMap.get(pm08));  // Impact of Erosion on Paid-up Capital - Others (%)
							double p09=Double.parseDouble(paramMap.get(pm09));  // Impact of Erosion on Non-refundable Govt.  Deposits, Recapitalisation assistance received, etc. (%)
							double p10=Double.parseDouble(paramMap.get(pm10));  // Impact of Erosion on Paid-up Capital (%)
							double p11=Double.parseDouble(paramMap.get(pm11));  // Impact of Erosion on Deposits (%)
							double p12=Double.parseDouble(paramMap.get(pm12));  // Impact of Erosion on Borrowings (%)
							
							logger.info("Errosion .. FreeReserve(p07)::"+ p07 +" PaidCapital(p10)::"+p07 +" Deposit(p11)::"+p11);
							
							// Logic based on (Free Reserve), (Paid-up Capital) and (Deposits)
							if(p04 ==0 && p05 == 0 && p06 ==0 && p07 == 0 && p08 ==0 && p09 ==0 && p10==0 && p11==0 && p12==0) { // Scenario TC 1
								catLst.setCategoryRating("I"); catLst.setCategoryPrcntg("20"); catLst.setCategoryPrcntgOneBelow("10");
							}else if(p07 <= 50) {
								catLst.setCategoryRating("I"); catLst.setCategoryPrcntg("20"); catLst.setCategoryPrcntgOneBelow("10");
							}else if(p07 > 50 && p07 <= 100 && p10==0 ) {
								catLst.setCategoryRating("II"); catLst.setCategoryPrcntg("10"); catLst.setCategoryPrcntgOneBelow("0");
							}else if(p07 == 100 && p10 > 0 && p10 <= 50 ) {
								catLst.setCategoryRating("II"); catLst.setCategoryPrcntg("6"); catLst.setCategoryPrcntgOneBelow("0");
							}else if(p07 == 100 && p10 > 50 && p10 <= 100 && p11==0 ) {
								catLst.setCategoryRating("II"); catLst.setCategoryPrcntg("3"); catLst.setCategoryPrcntgOneBelow("0");
							}else if(p07 == 100 && p10 == 100 && p11 <= 20 ) {
								catLst.setCategoryRating("III"); catLst.setCategoryPrcntg("0"); catLst.setCategoryPrcntgOneBelow("0");
							}else if(p07 == 100 && p10 == 100 && p11 > 20 ) {
								catLst.setCategoryRating("IV"); catLst.setCategoryPrcntg("0"); catLst.setCategoryPrcntgOneBelow("0");
							}
							
							/*
							if(p04==100 && p05==100 && p06==100 && p07==100 && p08==100 && p09==100 && p10==100 &&  p11>20 ) { // Scenario TC 15
								catLst.setCategoryRating("IV"); catLst.setCategoryPrcntg("0"); catLst.setCategoryPrcntgOneBelow("0");
							}else if(p04==100 && p05==100 && p06==100 && p07==100 && p08==100 && p09==100 && p10==100 && ( p11<=20 && p11> 0) ) { // Scenario TC 13, 14
								catLst.setCategoryRating("III"); catLst.setCategoryPrcntg("0"); catLst.setCategoryPrcntgOneBelow("0");   
							}else if(p04==100 && p05==100 && p06==100 && p07==100 && p08==100 && p09==100 && p10>=75 && p11==0 ) { // Scenario TC 11, 12
								catLst.setCategoryRating("II"); catLst.setCategoryPrcntg("3"); catLst.setCategoryPrcntgOneBelow("0");
							}else if(p04==100 && p05==100 && p06==100 && p07==100 && p08==100 && p09==100 && p10>0 && p11==0 ) {   // Scenario TC 9, 10
								catLst.setCategoryRating("II"); catLst.setCategoryPrcntg("6"); catLst.setCategoryPrcntgOneBelow("0");
							}else if(p04==100 && p05==100 && p06==100 && p07>=75 ) {  											   // Scenario TC 6, 7, 8
								catLst.setCategoryRating("II"); catLst.setCategoryPrcntg("10"); catLst.setCategoryPrcntgOneBelow("0");
							}else if(p04==100 && p05==100 && p06>=0) { 															   // Scenario TC 2, 3, 4, 5
								catLst.setCategoryRating("I"); catLst.setCategoryPrcntg("20"); catLst.setCategoryPrcntgOneBelow("10");
							}else if((p04==100 && p05>=0) || p04>=0 ) { 															   // Scenario TC 1
								catLst.setCategoryRating("I"); catLst.setCategoryPrcntg("20"); catLst.setCategoryPrcntgOneBelow("10");
							}else if(p04 ==0 && p05 == 0 && p06 ==0 && p07 == 0 && p08 ==0 && p09 ==0 && p10==0 && p11==0 && p12==0) { // Scenario TC 1
								catLst.setCategoryRating("I"); catLst.setCategoryPrcntg("20"); catLst.setCategoryPrcntgOneBelow("10");
							}*/
							
							/* Commented on 27Aug24
							if(p04==100 && p05==100 && p06==100 && p07==100 && p08==100 && p09==100 && p10==100 &&  p11>20 ) { // Scenario TC 15
								catLst.setCategoryRating("IV"); catLst.setCategoryPrcntg("0"); catLst.setCategoryPrcntgOneBelow("0");
							}else if(p04==100 && p05==100 && p06==100 && p07==100 && p08==100 && p09==100 && p10==100 && ( p11<=20 && p11> 0) ) { // Scenario TC 13, 14
								catLst.setCategoryRating("III"); catLst.setCategoryPrcntg("0"); catLst.setCategoryPrcntgOneBelow("0");   
							}else if(p04==100 && p05==100 && p06==100 && p07==100 && p08==100 && p09==100 && p10>=75 && p11==0 ) { // Scenario TC 11, 12
								catLst.setCategoryRating("II"); catLst.setCategoryPrcntg("3"); catLst.setCategoryPrcntgOneBelow("0");
							}else if(p04==100 && p05==100 && p06==100 && p07==100 && p08==100 && p09==100 && p10>0 && p11==0 ) {   // Scenario TC 9, 10
								catLst.setCategoryRating("II"); catLst.setCategoryPrcntg("6"); catLst.setCategoryPrcntgOneBelow("0");
							}else if(p04==100 && p05==100 && p06==100 && p07>=75 ) {  											   // Scenario TC 6, 7, 8
								catLst.setCategoryRating("II"); catLst.setCategoryPrcntg("10"); catLst.setCategoryPrcntgOneBelow("0");
							}else if(p04==100 && p05==100 && p06>=0) { 															   // Scenario TC 2, 3, 4, 5
								catLst.setCategoryRating("I"); catLst.setCategoryPrcntg("20"); catLst.setCategoryPrcntgOneBelow("10");
							}else if((p04==100 && p05>=0) || p04>=0 ) { 															   // Scenario TC 1
								catLst.setCategoryRating("I"); catLst.setCategoryPrcntg("20"); catLst.setCategoryPrcntgOneBelow("10");
							}else if(p04 ==0 && p05 == 0 && p06 ==0 && p07 == 0 && p08 ==0 && p09 ==0 && p10==0 && p11==0 && p12==0) { // Scenario TC 1
								catLst.setCategoryRating("I"); catLst.setCategoryPrcntg("20"); catLst.setCategoryPrcntgOneBelow("10");
							}*/
							
							/*if(p04 ==0 && p05 == 0 && p06 ==0 && p07 == 0 && p08 ==0 && p09 ==0 && p10==0 && p11==0 && p12==0) { 
								catLst.setCategoryRating("I"); catLst.setCategoryPrcntg("20"); catLst.setCategoryPrcntgOneBelow("10");}
							else if(p04==100 && p05==100 && p06==100 && p07<=50 ) { 
								catLst.setCategoryRating("I"); catLst.setCategoryPrcntg("20"); catLst.setCategoryPrcntgOneBelow("10");}
							else if(p04==100 && p05==100 && p06==100 && p07>50 ) { 
								catLst.setCategoryRating("II"); catLst.setCategoryPrcntg("10"); catLst.setCategoryPrcntgOneBelow("0");}
							else if(p04==100 && p05==100 && p06==100 && p07==100 && p08==100 && p09==100 && p10<=50 ) {
								catLst.setCategoryRating("II"); catLst.setCategoryPrcntg("6"); catLst.setCategoryPrcntgOneBelow("0");
							}else if(p04==100 && p05==100 && p06==100 && p07==100 && p08==100 && p09==100 && p10==100 &&  p11==0 ) {
								catLst.setCategoryRating("II"); catLst.setCategoryPrcntg("3"); catLst.setCategoryPrcntgOneBelow("0");
							}else if(p04==100 && p05==100 && p06==100 && p07==100 && p08==100 && p09==100 && p10==100 &&  p11<=20 ) {
								catLst.setCategoryRating("III"); catLst.setCategoryPrcntg("0"); catLst.setCategoryPrcntgOneBelow("0");
							}else if(p04==100 && p05==100 && p06==100 && p07==100 && p08==100 && p09==100 && p10==100 &&  p11>20 ) {
								catLst.setCategoryRating("IV"); catLst.setCategoryPrcntg("0"); catLst.setCategoryPrcntgOneBelow("0");
							}*/else catLst.setCategoryRating("Error");					
						}catch(Exception e) {
							catLst.setCategoryRating("Error");
							e.printStackTrace(System.out);}
					}else if(catCode.equalsIgnoreCase("CT014")) {	// Profitability	
						double p14=Double.parseDouble(paramMap.get(pm14));
						double p14_II=Double.parseDouble(paramMap.get(pm14_II));
						double p14_III=Double.parseDouble(paramMap.get(pm14_III));
						double p15=Double.parseDouble(paramMap.get(pm15));
						try {
							if(p14 <0 && p14_II <0 && p14_III <0) {
								catLst.setCategoryRating("IV"); catLst.setCategoryPrcntg("0"); catLst.setCategoryPrcntgOneBelow("0");
							}else if(p14 >0 && p14_II >0 && p14_III >0 && p15 >0) {
								catLst.setCategoryRating("II"); catLst.setCategoryPrcntg("6"); catLst.setCategoryPrcntgOneBelow("3");
							}else if(p14 >0 && p14_II >0 && p14_III >0 && p15 ==0) {
								catLst.setCategoryRating("I"); catLst.setCategoryPrcntg("10"); catLst.setCategoryPrcntgOneBelow("6");
							}else if(p14 >0 && p14_II >0 && p14_III >0 && p15 ==0) {
								catLst.setCategoryRating("I"); catLst.setCategoryPrcntg("10"); catLst.setCategoryPrcntgOneBelow("6");
							}else if( (p14 >0 || p14 <0) && (p14_II >0 || p14_II <0) && (p14_III >0 || p14_III <0) && (p15 ==0 || p15>0) ) {
								catLst.setCategoryRating("III"); catLst.setCategoryPrcntg("3"); catLst.setCategoryPrcntgOneBelow("0");
							}
						}catch(Exception e) {
						catLst.setCategoryRating("Error");
						e.printStackTrace(System.out);}
					}else if(catCode.equalsIgnoreCase("CT015")) {	// Management and Governance					
						try {
							double p16=Double.parseDouble(paramMap.get(pm16));
							String p17=paramMap.get(pm17);  	String p18=paramMap.get(pm18);		String p19=paramMap.get(pm19);
							String p20=paramMap.get(pm20);		String p21=paramMap.get(pm21);		String p22=paramMap.get(pm22);
							String p23=paramMap.get(pm23);		String p24=paramMap.get(pm24);
							
							if(auditTypeCode.contains("DCCB")) p20="Yes"; // As for DCCB 2 parameters are not applicable P0120, P1120
							if(auditTypeCode.contains("StCB") && isScheduledStcb) p19="Yes"; // For Scheduled Stcb  P0219, P1219 default to YES
							else if(auditTypeCode.contains("StCB") && !isScheduledStcb) p20="Yes"; // For Non Scheduled Stcb  P0220, P1220 default to YES
							
							if(p16 >=80 && "Yes".equalsIgnoreCase(p17) && "Yes".equalsIgnoreCase(p18) && "Yes".equalsIgnoreCase(p19) && "Yes".equalsIgnoreCase(p20)
									&& "Yes".equalsIgnoreCase(p21) && "Yes".equalsIgnoreCase(p22) && "Yes".equalsIgnoreCase(p23) && "Yes".equalsIgnoreCase(p24)) {
								catLst.setCategoryRating("I"); catLst.setCategoryPrcntg("15"); catLst.setCategoryPrcntgOneBelow("8");
							}else if(p16 >=80 && ("No".equalsIgnoreCase(p17) || "No".equalsIgnoreCase(p18) || "No".equalsIgnoreCase(p19) || "No".equalsIgnoreCase(p20)
									|| "No".equalsIgnoreCase(p21) || "No".equalsIgnoreCase(p22) || "No".equalsIgnoreCase(p23) || "No".equalsIgnoreCase(p24))) {
								catLst.setCategoryRating("II"); catLst.setCategoryPrcntg("8"); catLst.setCategoryPrcntgOneBelow("3");
							}else if(p16 >=60 ) {
								catLst.setCategoryRating("II"); catLst.setCategoryPrcntg("8"); catLst.setCategoryPrcntgOneBelow("3");
							}else if(p16 >=40 ) {
								catLst.setCategoryRating("III"); catLst.setCategoryPrcntg("3"); catLst.setCategoryPrcntgOneBelow("0");
							}else if(p16 < 40 ) {
								catLst.setCategoryRating("IV"); catLst.setCategoryPrcntg("0"); catLst.setCategoryPrcntgOneBelow("0");
							}else catLst.setCategoryRating("Error");
						}catch(Exception e) {
							catLst.setCategoryRating("Error");
							e.printStackTrace(System.out);}
					}else if(catCode.equalsIgnoreCase("CT016")) {  // Liquidity
						try {
							// D: Default for less than 2 fortnights (including technical default)
							// F: Frequent/Continuous default for 2 fortnights and above     OR   F: Frequent/Continuous
							// NDD: Not able to meet depositors demand
							String p25=paramMap.get(pm25), p26=paramMap.get(pm26), p27=paramMap.get(pm27), p28=paramMap.get(pm28);
							if(p25.equalsIgnoreCase("F") || p26.equalsIgnoreCase("F") || p27.equalsIgnoreCase("F") || p28.equalsIgnoreCase("NDD")) {
								catLst.setCategoryRating("III");catLst.setCategoryPrcntg("0"); catLst.setCategoryPrcntgOneBelow("0");
							}else if(p25.equalsIgnoreCase("D") || p26.equalsIgnoreCase("D") || p27.equalsIgnoreCase("Yes") || p28.equalsIgnoreCase("Yes")) {
								catLst.setCategoryRating("II");catLst.setCategoryPrcntg("6"); catLst.setCategoryPrcntgOneBelow("0");
							}else if(p25.equalsIgnoreCase("No") && p26.equalsIgnoreCase("No") && p27.equalsIgnoreCase("No") && p28.equalsIgnoreCase("No")) {
								catLst.setCategoryRating("I");catLst.setCategoryPrcntg("10"); catLst.setCategoryPrcntgOneBelow("6");
							}else catLst.setCategoryRating("Error");
						}catch(Exception e) {
							catLst.setCategoryRating("Error");
							e.printStackTrace(System.out);}
					}else if(catCode.equalsIgnoreCase("CT017")) {	// Systems (Internal checks & control)					
						try {
							double p29=Double.parseDouble(paramMap.get(pm29));
							String p30=paramMap.get(pm30);  	String p31=paramMap.get(pm31);		
							if(p29 >=80 && "Yes".equalsIgnoreCase(p30) && "Yes".equalsIgnoreCase(p31)) {
								catLst.setCategoryRating("I"); catLst.setCategoryPrcntg("15"); catLst.setCategoryPrcntgOneBelow("8");
							}else if(p29 >=80 && ("No".equalsIgnoreCase(p30) || "No".equalsIgnoreCase(p31))) {
								catLst.setCategoryRating("II"); catLst.setCategoryPrcntg("8"); catLst.setCategoryPrcntgOneBelow("3");
							}else if(p29 >=60 ) {
								catLst.setCategoryRating("II"); catLst.setCategoryPrcntg("8"); catLst.setCategoryPrcntgOneBelow("3");
							}else if(p29 >=40 ) {
								catLst.setCategoryRating("III"); catLst.setCategoryPrcntg("3"); catLst.setCategoryPrcntgOneBelow("0");
							}else if(p29 < 40 ) {
								catLst.setCategoryRating("IV"); catLst.setCategoryPrcntg("0"); catLst.setCategoryPrcntgOneBelow("0");
							}else catLst.setCategoryRating("Error");
						}catch(Exception e) {
							catLst.setCategoryRating("Error");
							e.printStackTrace(System.out);}
					}else if(catCode.equalsIgnoreCase("CT018")) {	// Compliance to various Acts					
						try {
							double p32=Double.parseDouble(paramMap.get(pm32));
							String p33=paramMap.get(pm33);  	String p34=paramMap.get(pm34);		String p35=paramMap.get(pm35);
							String p36=paramMap.get(pm36);		String p37=paramMap.get(pm37);		String p38=paramMap.get(pm38);  String p385=paramMap.get(pm385);  
							String p39=paramMap.get(pm39);		String p40=paramMap.get(pm40);		String p41=paramMap.get(pm41); 
							
							if(auditTypeCode.contains("DCCB")) p36="Yes"; // As for DCCB 2 parameters are not applicable P0136, P1136
							if(auditTypeCode.contains("StCB") && isScheduledStcb) p35="Yes"; // For Scheduled Stcb  P0235, P1235 default to YES
							else if(auditTypeCode.contains("StCB") && !isScheduledStcb) p36="Yes"; // For Non Scheduled Stcb  P0236, P1236 default to YES
							
							if(p32 >=80 && "Yes".equalsIgnoreCase(p33) && "Yes".equalsIgnoreCase(p34) && "Yes".equalsIgnoreCase(p35) && "Yes".equalsIgnoreCase(p36) && "Yes".equalsIgnoreCase(p37)
									&& "Yes".equalsIgnoreCase(p38) && "Yes".equalsIgnoreCase(p385) && "Yes".equalsIgnoreCase(p39) && "Yes".equalsIgnoreCase(p40) && "Yes".equalsIgnoreCase(p41)) { // p385 added in the condition
								catLst.setCategoryRating("I"); catLst.setCategoryPrcntg("10"); catLst.setCategoryPrcntgOneBelow("6");
							}else if(p32 >=80 && ("No".equalsIgnoreCase(p33) || "No".equalsIgnoreCase(p34) || "No".equalsIgnoreCase(p35) || "No".equalsIgnoreCase(p36)  && "Yes".equalsIgnoreCase(p37)
									|| "No".equalsIgnoreCase(p38) || "No".equalsIgnoreCase(p385) || "No".equalsIgnoreCase(p39) || "No".equalsIgnoreCase(p40) || "No".equalsIgnoreCase(p41))) { // p385 added in the condition
								catLst.setCategoryRating("II"); catLst.setCategoryPrcntg("6"); catLst.setCategoryPrcntgOneBelow("3");
							}else if(p32 >=60 ) {
								catLst.setCategoryRating("II"); catLst.setCategoryPrcntg("6"); catLst.setCategoryPrcntgOneBelow("3");
							}else if(p32 >=40 ) {
								catLst.setCategoryRating("III"); catLst.setCategoryPrcntg("3"); catLst.setCategoryPrcntgOneBelow("0");
							}else if(p32 < 40 ) {
								catLst.setCategoryRating("IV"); catLst.setCategoryPrcntg("0"); catLst.setCategoryPrcntgOneBelow("0");
							}else catLst.setCategoryRating("Error");
						}catch(Exception e) {
							catLst.setCategoryRating("Error");
							e.printStackTrace(System.out);}
					}
					// Code used to calculate PercentageTotalOneBelow, for all the Categories when there is a Deflator
					/*if(!("Error").equalsIgnoreCase(catLst.getCategoryRating())){
						catPrcntgTotal+=Double.parseDouble(catLst.getCategoryPrcntg());
						catPrcntgTotalOneBelow+=Double.parseDouble(catLst.getCategoryPrcntgOneBelow());
					}*/	
					// Code modified to calculate PercentageTotalOneBelow, for only the "Management & Governance" Category when there is a Deflator
					if(!("Error").equalsIgnoreCase(catLst.getCategoryRating())){
						catPrcntgTotal+=Double.parseDouble(catLst.getCategoryPrcntg());
						if(catCode.equalsIgnoreCase("CT015")) { // for "Management & Governance" need to add/include the onePercentage below
							catPrcntgTotalOneBelow+=Double.parseDouble(catLst.getCategoryPrcntgOneBelow());
						}else catPrcntgTotalOneBelow+=Double.parseDouble(catLst.getCategoryPrcntg()); // // for the other Categories there is no impact of Deflator, so will include/add the normal percentage
					}					
				}logger.info("catCode .. "+catCode+" catPrcntgTotal  .."+catPrcntgTotal +" catPrcntgTotalOneBelow .."+ catPrcntgTotalOneBelow);			
				catLst.setCategoryRatingAftrOverlay(catLst.getCategoryRating());  // Set the rating to after overlay for display
				catLst.setCategoryPrcntgAftrOverlay(catLst.getCategoryPrcntg());  // Set the percentage to after overlay for display
				
				// Apply overlay on the Category value
				logger.info("overlay .... "+ovrLayYes);
				finalPrcntg=catPrcntgTotal; 
				logger.info("finalPrcntg .. "+ finalPrcntg);
				if(ovrLayYes) {
					if("Error".equalsIgnoreCase(catLst.getCategoryRating())) catLst.setCategoryRatingAftrOverlay("Error");
					if("RRBEI".equalsIgnoreCase(auditTypeCode)) {
						if("III".equalsIgnoreCase(catLst.getCategoryRating())) catLst.setCategoryRatingAftrOverlay("III");
						else if("II".equalsIgnoreCase(catLst.getCategoryRating())) catLst.setCategoryRatingAftrOverlay("III");
						else if("I".equalsIgnoreCase(catLst.getCategoryRating())) catLst.setCategoryRatingAftrOverlay("II");
					}else {
						if(catCode.equalsIgnoreCase("CT015")) { // As the deflator is only applicable to "Management & Governance"
							logger.info("catCode .. "+catCode);
							if("IV".equalsIgnoreCase(catLst.getCategoryRating())) catLst.setCategoryRatingAftrOverlay("IV");
							else if("III".equalsIgnoreCase(catLst.getCategoryRating())) catLst.setCategoryRatingAftrOverlay("IV");
							else if("II".equalsIgnoreCase(catLst.getCategoryRating())) catLst.setCategoryRatingAftrOverlay("III");
							else if("I".equalsIgnoreCase(catLst.getCategoryRating())) catLst.setCategoryRatingAftrOverlay("II");
							//finalPrcntg=catPrcntgTotalOneBelow; // overriding the FInalPercentage
							catLst.setCategoryPrcntgAftrOverlay(catLst.getCategoryPrcntgOneBelow());// Setting the percentage one level below only for "Management & Governance"
							logger.info("finalPrcntg inside overlay Management .. .. "+finalPrcntg);
						}finalPrcntg=catPrcntgTotalOneBelow; // overriding the FInalPercentage
					}logger.info("finalPrcntg .. "+ finalPrcntg);
					
					//catLst.setCategoryPrcntgAftrOverlay(catLst.getCategoryPrcntgOneBelow());// commented as, Reducing the percentage is only applicable to "Management & Governance"
				}
				lowestMap.put(catLst.getCategoryRatingAftrOverlay(), catLst.getCategoryRatingAftrOverlay());
			}// loop ends here
			
			logger.info("");
			cmpScoreDto.setCompCategryScoreLst(cmpCatScrLst);
			
			/********* Get the Escalation Matrix **************/
			logger.info(" lowestMap .."+lowestMap);
			logger.info("AuditType::"+auditTypeCode + " OverLays::"+ovrLayYes);
			String categoryValue="";
			if("RRBEI".equalsIgnoreCase(auditTypeCode)) {
				if(lowestMap.containsKey("Error")) categoryValue="Error";
				else if(lowestMap.containsKey("IV")) categoryValue="IV";
				else if(lowestMap.containsKey("III")) categoryValue="III";
				if(ovrLayYes) {
					if(lowestMap.containsKey("II")) categoryValue="III";
					else if(lowestMap.containsKey("I")) categoryValue="II";
				}else {
					if(lowestMap.containsKey("II")) categoryValue="II";
					else if(lowestMap.containsKey("I")) categoryValue="I";
				}
			}
			String catDesc="";
			
			if(!"RRBEI".equalsIgnoreCase(auditTypeCode)) {
				if(finalPrcntg >= 75 ) categoryValue="A";
				else if(finalPrcntg >= 60 ) categoryValue="B";
				else if(finalPrcntg >= 24 ) categoryValue="C";
				else if(finalPrcntg < 60 ) categoryValue="D";
			}
			logger.info("Final AuditType::"+auditTypeCode + " OverLay::"+ovrLayYes+" categoryValue::"+categoryValue);
			
			MaintRatingEscalationMatrix escMtrx= auditRatingCompositeDao.getRatingEscalationMatrixByAuditId(legalEntityCode,auditId,  auditTypeCode, categoryValue);
			if(!"RRBEI".equalsIgnoreCase(auditTypeCode)) 
				escMtrx.setCategoryScore(String.valueOf(finalPrcntg));
			
			cmpScoreDto.setRatingEscalationMatrix(escMtrx);
			/******End Here*******/		
			
		}catch(Exception e) {
			e.printStackTrace(System.out);
			cmpScoreDto.setCompCategryScoreLst(cmpCatScrLst);
		}
		return cmpScoreDto;
	}
	
	public List<AuditRatingCompositeOverlay> getAuditRatingCompositeOverlayByAuditId(int legalEntityCode,String auditId, String auditTypeCode, String userType){
		logger.info("Inside getAuditRatingCompositeOverlayByAuditId .. "+ auditId+" :: "+ auditTypeCode+" :: "+userType);
		return auditRatingCompositeDao.getAuditRatingCompositeOverlayByAuditId(legalEntityCode, auditId, auditTypeCode, userType);
	}
	
	public MaintRatingEscalationMatrix getRatingEscalationMatrixByAuditId(int legalEntityCode,String auditId, String auditTypeCode, String userType){
		logger.info("Inside getRatingEscalationMatrix .. "+ auditId+" :: "+ auditTypeCode+" :: "+userType);
		return auditRatingCompositeDao.getRatingEscalationMatrixByAuditId(legalEntityCode, auditId, auditTypeCode, userType);
	}
	
	@Override
	public void updateAuditRatingScores(AuditRatingScoreDto auditRatingScoreDto, int legalEntityCode, String auditId, String userId, String userType) {
		auditRatingCompositeDao.updateAuditRatingScores(auditRatingScoreDto, legalEntityCode, auditId, userId, userType); 
	}
	
	// To verify whether IR finalized and Supervisory rating submitted
	@Override
	public String verifyIRFinalisedAndSupervisoryRatingSubmitted(int legalEntityCode,String auditTypeCode, String auditId) {
		logger.info("Inside verifyIRFinalisedAndSupervisoryRatingSubmitted .. "+ auditId+" :: "+ auditTypeCode);
		AuditSchedule sch= myAuditsDao.getAuditByAuditId(legalEntityCode, auditTypeCode, auditId);
		String verifySubmit="YES";
		if(sch !=null ) {
			String rptSts=sch.getReportStatus();  // Report status
			String sRatSts=sch.getRatingParameterStatus();  // Supervisory rating status
			logger.info("RptSts .."+rptSts +" RatingSts .."+sRatSts);
			if( !("RS".equalsIgnoreCase(rptSts) || "RD".equalsIgnoreCase(rptSts))) {  // If not equals to RS or RD
				verifySubmit="IRNO";
			}else if( !( "RPR".equalsIgnoreCase(sRatSts) || "RPHU".equalsIgnoreCase(sRatSts) || "RPS".equalsIgnoreCase(sRatSts))) {  // ignored RPP // included RPHU
				verifySubmit="SRNO";
			}
		}
		return verifySubmit;
	}	
}
