package com.bankaudit.rest.process.dao;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.jboss.logging.Logger;
import org.springframework.stereotype.Repository;

import com.bankaudit.dao.AbstractDao;
import com.bankaudit.dto.process.AuditRatingSummaryDto;
import com.bankaudit.model.MaintReportDerivedStmt;
import com.bankaudit.process.model.AuditReportStmt;
import com.bankaudit.process.model.IfcBatchProcessStatus;
import com.bankaudit.process.model.IfcEnsureBaasData;
import com.bankaudit.process.model.IfcEnsureBaasIRProcessData;
import com.bankaudit.process.model.IfcRatingData;

@Repository
public class RatingProcessingDaoImpl extends AbstractDao implements RatingProcessingDao {
	
	static final Logger logger = Logger.getLogger(RatingProcessingDaoImpl.class);
		
	@SuppressWarnings({ "unchecked", "serial", "deprecation" })
	public Boolean executeParameterRating(Integer legalEntityCode, String auditId, String auditTypeCode){
		
		logger.info(" executeParameterRating .... "+ legalEntityCode + "::" + auditTypeCode +" :: "+ auditId );
		Session session=getSession();
		boolean execute= true;
		try {
			StringBuilder queryString=new StringBuilder(" SELECT p.param_code,p.legal_entity_code,p.audit_id, p.rm_code, "  //3
					+"  p.param_mesaurement_type, p.param_basis,p.score_assessed,p.score, p.min_range, p.max_range ,r.min_range,r.max_range, " // 11
					+" 	i.input_01, i.input_02, i.input_03, i.input_04, i.input_05, i.input_06, i.formula " // 18
					+" FROM ( "
					+"   SELECT a.legal_entity_code,a.audit_id, a.rm_code, a.param_code, a.param_mesaurement_type, a.param_basis, a.score_assessed,r.min_r,r.max_r,r.score,min_range,max_range  "
					+"     FROM audit_rating_parameter_score a,  "
					+"          (SELECT IFNULL(min_range,-10000) AS min_r, IFNULL(max_range,100000) AS max_r,param_code, score,min_range,max_range FROM maint_rating_parameter_range )r   "
					+"     WHERE a.param_code=r.param_code AND a.audit_id=:auditId AND ( a.score_assessed >= r.min_r AND a.score_assessed < r.max_r) "
					+"   ) AS p "
					+" LEFT JOIN maint_rating_parameter_range r ON p.param_code=r.param_code AND p.max_range=r.min_range AND p.legal_entity_code=r.legal_entity_code "
					+" LEFT JOIN maint_rating_parameter_inputs i ON p.param_code=i.param_code AND p.legal_entity_code=i.legal_entity_code "
					+" WHERE p.legal_entity_code=:legalEntityCode AND p.audit_id=:auditId ");
						
					logger.info("Before the qry...."+ queryString.toString());
					
					Query query=session.createNativeQuery(queryString.toString())
							.setParameter("legalEntityCode", legalEntityCode).setParameter("auditId", auditId);
					
					List qrylst = query.list();	
					String pCode=null, rmCode=null, mesasureType=null,paramBasis=null,ip1=null, ip2=null, ip3=null, ip4=null, ip5=null, ip6=null, formula=null;
					/* 
					try {
						for ( Iterator iter = qrylst.iterator(); iter.hasNext(); ) {
							Object[] obj = (Object[]) iter.next();
							double weightage=0.0, qn=0.0, ql=0.0, weightedScore=0.0;
							auditSummaryDto = new AuditRatingSummaryDto();
							auditSummaryDto.setLegalEntityCode(legalEntityCode);
							auditSummaryDto.setAuditId(auditId);
							auditSummaryDto.setRmCode(rmCode);
							auditSummaryDto.setCategoryCode(obj[2].toString());
							auditSummaryDto.setCategoryName(obj[3].toString());
							if(obj[4] !=null ) {
								auditSummaryDto.setWeightage(obj[4].toString());
								weightage=Double.parseDouble(obj[4].toString());
							}if(obj[5] !=null ) {
								auditSummaryDto.setQuantitativeSore(obj[5].toString());
								qn=Double.parseDouble(obj[5].toString());
							}if(obj[6] !=null ) {
								auditSummaryDto.setQualitativeScore(obj[6].toString());
								ql=Double.parseDouble(obj[6].toString());
							}
							auditSummaryDto.setTotalScore(String.valueOf((double)Math.round(qn+ql)));
							weightedScore=(double)Math.round((qn+ql)*weightage/100);
							auditSummaryDto.setTotalweightedScore(String.valueOf(weightedScore));
							
							lstSummary.add(auditSummaryDto);					
						}			
			
			*/
			
			
			
			
			
			
			/*session.createQuery("UPDATE AuditReportStmt r, MaintReportDerivedStmt d  SET r.printFlag='NO' "
						+ " WHERE r.legalEntityCode =d.legalEntityCode AND r.statementId=d.statementId " 
						+ " 	AND r.statementType='DR' AND d.dependency_type='INV_YR_SUPRESS' "
						+ "		AND r.legalEntityCode=:legalEntityCode AND r.auditId=:auditId ")
				.setParameter("legalEntityCode", legalEntityCode).setParameter("auditId", auditId).executeUpdate(); */
			/*String updateQuery= "UPDATE audit_report_stmt r, maint_report_derived_stmt d  SET r.print_flag='NO' "
					+ "  WHERE r.legal_entity_code=d.legal_entity_code AND r.statement_id=d.statement_id  "
					+ "	 AND r.statement_type='DR' AND d.dependency_type='INV_YR_SUPRESS' "
					+ "  AND r.legal_entity_code=:legalEntityCode AND r.audit_id=:auditId ";
			session.createSQLQuery(updateQuery).setParameter("legalEntityCode",legalEntityCode).setParameter("auditId", auditId).executeUpdate();
			*/
		}catch(Exception e) {
			e.printStackTrace(System.out);
		}return execute;
		
	}
	
	
	/*@SuppressWarnings({ "unchecked", "serial", "deprecation" })
	public void suppressInterveningYrDeriveStmnts(int legalEntityCode,String auditTypeCode,String auditId){
		logger.info(" suppressInterveningYrDeriveStmnts .... "+ legalEntityCode + "::" + auditTypeCode +" :: "+ auditId );
		Session session=getSession();
		try {
			logger.info("Inside suppressInterveningYrDeriveStmnts ");
			/*session.createQuery("UPDATE AuditReportStmt r, MaintReportDerivedStmt d  SET r.printFlag='NO' "
						+ " WHERE r.legalEntityCode =d.legalEntityCode AND r.statementId=d.statementId " 
						+ " 	AND r.statementType='DR' AND d.dependency_type='INV_YR_SUPRESS' "
						+ "		AND r.legalEntityCode=:legalEntityCode AND r.auditId=:auditId ")
				.setParameter("legalEntityCode", legalEntityCode).setParameter("auditId", auditId).executeUpdate(); * /
			String updateQuery= "UPDATE audit_report_stmt r, maint_report_derived_stmt d  SET r.print_flag='NO' "
					+ "  WHERE r.legal_entity_code=d.legal_entity_code AND r.statement_id=d.statement_id  "
					+ "	 AND r.statement_type='DR' AND d.dependency_type='INV_YR_SUPRESS' "
					+ "  AND r.legal_entity_code=:legalEntityCode AND r.audit_id=:auditId ";
			session.createSQLQuery(updateQuery).setParameter("legalEntityCode",legalEntityCode).setParameter("auditId", auditId).executeUpdate();
		}catch(Exception e) {
			e.printStackTrace(System.out);
		}
		
	}*/
		
	
	// To get the CSV data
	@SuppressWarnings("unchecked")
	@Override
	public List<IfcRatingData> getIfcRatingData(Integer legalEntityCode, String auditId) {
		logger.info("Inside getIfcRatingData .. "+legalEntityCode +" auditId.."+auditId );
		Session session=getSession();
		return (List<IfcRatingData>)session.createQuery("select ird from IfcRatingData ird where  ird.legalEntityCode=:legalEntityCode AND ird.auditId=:auditId ")
				.setParameter("legalEntityCode", legalEntityCode).setParameter("auditId", auditId).list();
	}
	

}
