package com.bankaudit.rest.process.dao;

import java.util.List;

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

import com.bankaudit.constants.BankAuditConstant;
import com.bankaudit.dao.AbstractDao;
import com.bankaudit.process.model.AuditScheduleStagesStatusHst;
import com.bankaudit.util.BankAuditUtil;

@Repository
public class AuditLogDaoImpl extends AbstractDao implements AuditLogDao{
	
	static final Logger logger = Logger.getLogger(AuditLogDaoImpl.class);
	
	
	@SuppressWarnings("unchecked")
	@Override
	public List<AuditScheduleStagesStatusHst> getAuditLogByProcess(Integer legalEntityCode, String auditId, String userId, String role, String process, String code, String userUnitLevel) {
		Session session=getSession();
		List<AuditScheduleStagesStatusHst> adtLog = null;
		try {
			logger.info("getAuditLogByProcess legalEntityCode.."+legalEntityCode+" userId.."+userId+" role.."+role+" auditId.."+auditId+ " process.."+process+" code.. "+code+" userUnitLevel.. "+userUnitLevel);
			StringBuilder qryStr = new StringBuilder(" select stg as auditScheduleStgHst, "
					+ " (Select concat(u.firstName, ' ',u.middleName,' ',u.lastName) from  User u where u.legalEntityCode =stg.legalEntityCode and u.userId=stg.makerId) as makerName, "
					+ " (Select concat(u.firstName, ' ',u.middleName,' ',u.lastName) from  User u where u.legalEntityCode =stg.legalEntityCode and u.userId=stg.assigneeId) as assigneeName, "
					+ " (Select r.ugRoleName from  MaintUsergroupRoles r where r.legalEntityCode =stg.legalEntityCode and r.ugRoleCode=stg.assigneeRole) as assigneeRoleName, "
					+ " (Select g.description from  GeneralParameter g where g.legalEntityCode =stg.legalEntityCode and g.key1='AUDIT_LOG' and g.key2= :process and g.value=stg.stagesStatus) as action "
					+ " FROM AuditScheduleStagesStatusHst stg "
					+ " WHERE stg.legalEntityCode = :legalEntityCode AND stg.auditId = :auditId  AND stg.actionType = :process ");
			if(!BankAuditUtil.isEmptyString(code)){
				qryStr.append(" AND stg.code = :code ");
			}
			if(BankAuditConstant.AUDIT_LOG_INSPECTION_PROCESS.equalsIgnoreCase(process)) {
				qryStr.append("AND stg.stagesStatus <> 'AS' ");
			}else if(BankAuditConstant.AUDIT_LOG_SUPERVISORY_RATING_PROCESS.equalsIgnoreCase(process) && !"1".equalsIgnoreCase(userUnitLevel)) {  // Not as HO, RO users to get RO records
				qryStr.append(" AND stg.stagesStatus in ('RPU','RPP','RPRU','RPR' ) ");
				qryStr.append(" AND NOT (stg.actionType='SUPERVISORY_RATING' AND stg.stagesStatus='RPRU' AND stg.makerRole='G12' AND stg.makerId=stg.assigneeId) ");  // If Reviewer save with out changing the Reviewer, record not to display in Audit Log
				qryStr.append(" AND NOT (stg.actionType='SUPERVISORY_RATING' AND stg.stagesStatus='RPHU' AND stg.makerRole='G12' AND stg.makerId=stg.assigneeId) ");  // If Reviewer save with out changing the Reviewer, record not to display in Audit Log
			}else if(BankAuditConstant.AUDIT_LOG_GRADING_PROCESS.equalsIgnoreCase(process) && "IR_GRADING".equalsIgnoreCase(code) && !"1".equalsIgnoreCase(userUnitLevel)) {  // RO users to get RO records
				qryStr.append(" AND stg.stagesStatus in ('GROU','GROS','GROA') ");
			}else if(BankAuditConstant.AUDIT_LOG_COMPLIANCE_RATING_PROCESS.equalsIgnoreCase(process)) {
				qryStr.append(" AND stg.stagesStatus NOT IN ('RCFDF','RCDDF') ");
				qryStr.append(" AND NOT (stg.actionType='COMPLIANCE_RATING' AND stg.stagesStatus='RCFM' AND stg.makerRole='G12' AND stg.makerId=stg.assigneeId) ");  // If Reviewer save with out changing the Reviewer, record not to display in Audit Log
				qryStr.append(" AND NOT (stg.actionType='COMPLIANCE_RATING' AND stg.stagesStatus='RCDM' AND stg.makerRole='G12' AND stg.makerId=stg.assigneeId) ");  // If Reviewer save with out changing the Reviewer, record not to display in Audit Log
			}else if(BankAuditConstant.AUDIT_LOG_LETTER_PROCESS.equalsIgnoreCase(process)) {
				qryStr.append(" AND NOT (stg.actionType='LETTER' AND stg.stagesStatus='U' AND stg.makerRole='G12' AND stg.makerId=stg.assigneeId) ");  // If Reviewer save with out changing the Reviewer, record not to display in Audit Log
			}
			
			Query hql= session.createQuery(qryStr.toString()).setParameter("legalEntityCode", legalEntityCode)
					.setParameter("auditId", auditId).setParameter("process", process);
			if(!BankAuditUtil.isEmptyString(code)){
				hql.setParameter("code", code);
			}
			
			adtLog= hql.setResultTransformer(new ResultTransformer() {
				@Override
				public Object transformTuple(Object[] tuple, String[] aliases) {
					AuditScheduleStagesStatusHst stgHst=null; 
					if(tuple!=null){
						if(tuple[0]!=null){
							stgHst = (AuditScheduleStagesStatusHst)tuple[0];
						}
						if(tuple[1]!=null)	stgHst.setMakerName((tuple[1].toString()));
						if(tuple[2]!=null)	stgHst.setAssigneeName((tuple[2].toString()));
						if(tuple[3]!=null)	stgHst.setAssigneeRoleName((tuple[3].toString()));
						if(tuple[4]!=null)	stgHst.setActionName((tuple[4].toString()));
					}					
					return stgHst;
				}
				
				@Override
				public List transformList(List collection) {
					return collection;
				}
			}).list();			
									
		}catch(Exception e) {
			e.printStackTrace();
		}
		return adtLog;
	}
	
	
	
}
