package com.bankaudit.rest.process.dao;

import java.util.ArrayList;
import java.util.Arrays;
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.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;

@Repository
public class BatchProcessingDaoImpl extends AbstractDao implements BatchProcessingDao {
	
	static final Logger logger = Logger.getLogger(BatchProcessingDaoImpl.class);
		
	@SuppressWarnings("unchecked")
	@Override
	public List<IfcEnsureBaasIRProcessData> getIfcParameterDataFromProcess(Integer legalEntityCode, String auditId, String referenceNo) {
		logger.info("Inside getIfcParameterDataFromProcess .. "+legalEntityCode +" auditId.."+auditId +" status.."+referenceNo );
		Session session=getSession();
		return session.createQuery("select ir from IfcEnsureBaasIRProcessData ir"
					+ " where  ir.legalEntityCode=:legalEntityCode AND ir.auditId=:auditId ")
				.setParameter("legalEntityCode", legalEntityCode).setParameter("auditId", auditId).list();
	}
	
	// To get the 1st year data
	@SuppressWarnings("unchecked")
	@Override
	public List<IfcEnsureBaasData> getIfcBaasParameterDataFromProcess(Integer legalEntityCode, String auditId, String referenceNo) {
		logger.info("Inside getIfcBaasParameterDataFromProcess .. "+legalEntityCode +" auditId.."+auditId +" status.."+referenceNo );
		Session session=getSession();
		return session.createQuery("select ib from IfcEnsureBaasData ib"
					+ " where  ib.legalEntityCode=:legalEntityCode AND ib.auditId=:auditId and ib.stageOfFile='L'")
				.setParameter("legalEntityCode", legalEntityCode).setParameter("auditId", auditId).list();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<AuditReportStmt> getAuditDerivedStmts(Integer legalEntityCode, String auditId, String referenceNo) {
		logger.info("Inside getAuditDerivedStmts .. "+legalEntityCode +" auditId.."+auditId +" referenceNo.."+referenceNo );
		Session session=getSession();
		return session.createQuery("select st from AuditReportStmt st "
					+ " where st.legalEntityCode=:legalEntityCode AND st.auditId=:auditId AND st.statementType='DR' ")
				.setParameter("legalEntityCode", legalEntityCode).setParameter("auditId", auditId).list();
	}
	
	/**** Only for testing, by taking only one Derive statement. ****/
	@SuppressWarnings("unchecked")
	@Override
	public List<AuditReportStmt> getAuditDerivedStmtsByStatementId(Integer legalEntityCode, String auditId, String referenceNo, String statementId) {
		logger.info("Inside getAuditDerivedStmtsByStatementId .. "+legalEntityCode +" auditId.."+auditId +" referenceNo.."+referenceNo +" statementId .. "+statementId);
		List<String> statementIds=Arrays.asList("1RRBTSDS","2RRBTSDS","3RRBTSDS","4RRBTSDS","5RRBTSDS");
		Session session=getSession();
		return session.createQuery("select st from AuditReportStmt st "
					+ " where st.legalEntityCode=:legalEntityCode AND st.auditId=:auditId AND st.statementType='DR' AND st.statementId in (:statementIds) ")
				.setParameter("legalEntityCode", legalEntityCode).setParameter("auditId", auditId)
				.setParameter("statementIds", statementIds).list();
	}
	
	
	@SuppressWarnings("unchecked")
	@Override
	public List<MaintReportDerivedStmt> getDerivedStmts(Integer legalEntityCode, String auditTypeCode, String referenceNo) {
		logger.info("Inside getDerivedStmts .. "+legalEntityCode +" auditTypeCode.."+ auditTypeCode +" referenceNo.."+referenceNo );
		Session session=getSession();
		return session.createQuery("select ds from MaintReportDerivedStmt ds "
					+ " where ds.legalEntityCode=:legalEntityCode AND ds.auditTypeCode=:auditTypeCode ")
				.setParameter("legalEntityCode", legalEntityCode).setParameter("auditTypeCode", auditTypeCode).list();
	}	
	
	@Override
	public void updateAuditReportStmt(AuditReportStmt auditReportStmt, Integer id, String statementId){
		Session session = getSession();
		logger.info("Inside updateAuditReportStmt .. :: "+auditReportStmt.getAuditId()+" :: "+ auditReportStmt.getId() +" :: "+auditReportStmt.getStatementId()+" :: "+auditReportStmt.getDeriveStatementOPStmnt() +" :: "+id );
		AuditReportStmt auditReportStmtUpdate=null;
		try{
			String drStatement= auditReportStmt.getStatement();
			/*auditReportStmtUpdate= (AuditReportStmt)session.get(AuditReportStmt.class,auditReportStmt);
			
			if(statementId.equalsIgnoreCase(auditReportStmtUpdate.getStatementId())){
				auditReportStmtUpdate.setStatement(drStatement);		
				session.update(auditReportStmtUpdate);
			}*/
			session.update(auditReportStmt);
		}catch (Exception e) {
			e.printStackTrace(System.out);
		} 
	}
	
	@Override
	public IfcBatchProcessStatus getBatchProcessStatus(Integer legalEntityCode, String auditId, String auditTypeCode) {
		logger.info("Inside getBatchProcessStatus of InterfaceParameterDaoImpl .... ");
		Session session = getSession();
		return (IfcBatchProcessStatus) session
				.createQuery(" FROM IfcBatchProcessStatus ifc where ifc.legalEntityCode=:legalEntityCode AND ifc.auditId=:auditId "
						+ "	AND ifc.auditTypeCode=:auditTypeCode")
				.setParameter("legalEntityCode", legalEntityCode).setParameter("auditId", auditId)
				.setParameter("auditTypeCode", auditTypeCode).uniqueResult();
	}
	
	@SuppressWarnings({ "unchecked", "serial", "deprecation" })
	public void updateAuditReportStmntRupeeSymbl(int legalEntityCode,String auditTypeCode,String auditId){
		logger.info(" updateAuditReportStmntRupeeSymbl .... "+ legalEntityCode + "::" + auditTypeCode +" :: "+ auditId );
		
		try {
			String hql ="";		
			hql+="CALL update_audit_report_stmnt(:legalEntityCode,:auditTypeCcode,:auditId)";
			Session session=getSession();
			Query query = session.createSQLQuery(hql)
					//.addScalar("v_temp", StandardBasicTypes.STRING)
					.setParameter("legalEntityCode", legalEntityCode)
				    .setParameter("auditTypeCcode", auditTypeCode)
				    .setParameter("auditId", auditId);
			
			query.list(); 
			logger.info("Procedure update_audit_report_stmnt executed.");
		}catch(Exception e) {
			e.printStackTrace(System.out);
		}
		
	}
	
	@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);
		}
		
	}
	

}
