package com.bankaudit.template.rest.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.jboss.logging.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestParam;

import com.bankaudit.constants.BankAuditConstant;
import com.bankaudit.dao.AbstractDao;
import com.bankaudit.process.model.AuditDocumentDetails;
import com.bankaudit.process.model.AuditDocumentDetailsWrk;
import com.bankaudit.service.BusinessDateService;
import com.bankaudit.util.BankAuditUtil;

@Repository("docUploadDownloadDao")
public class DocUploadDownloadDaoImpl extends AbstractDao implements DocUploadDownloadDao {
	
	@Autowired
	BusinessDateService businessDateService;
	
	static final Logger logger = Logger.getLogger(DocUploadDownloadDaoImpl.class);
	
	@SuppressWarnings("unchecked")
	@Override
	public List<AuditDocumentDetails> getAuditDocumentDetails(Integer legalEntityCode, String auditTypeCode,
			String planId, String auditId, String observationId, String documentType, String documentSubType, String status, String documentName) {
		Session session =getSession();
		
		session.setDefaultReadOnly(true);		 
		logger.info("Inside getAuditDocumentDetails AType.. "+ auditTypeCode+" AuID:: "+ auditId+" DType:: "+documentType+" SubType:: "+documentSubType+" Sts:: "+status+" DName:: "+documentName);
		StringBuilder str = new StringBuilder("   ");
				
			if(BankAuditConstant.STATUS_AUTH.equalsIgnoreCase(status) ) str.append( " from AuditDocumentDetails where  ");
			else  str.append( " from AuditDocumentDetailsWrk where   ");
			
			str.append( " status =:statusVal "
					+ " and legalEntityCode =:legalEntityCode "
					+ " and auditTypeCode =:auditTypeCode ");
			
			if(!BankAuditUtil.isEmptyString(planId)){
				str.append( " and planId =:planId ");
			}
			if(!BankAuditUtil.isEmptyString(auditId)){
				str.append( " and auditId =:auditId ");
			}
			if(!BankAuditUtil.isEmptyString(documentType)){
				str.append( " and documentType = :documentType ");
			}
			if(!BankAuditUtil.isEmptyString(documentSubType)){
				str.append("and documentSubType =:documentSubType ");
			}
			if(!BankAuditUtil.isEmptyString(observationId)){
				str.append("and observationId =:observationId ");
			}
			if(!BankAuditUtil.isEmptyString(documentName) && documentName.equalsIgnoreCase("Board Resolution")){
				str.append("and documentName =:documentName ");
			}
				
		
		Query qry = session.createQuery(str.toString());
		qry.setParameter("statusVal", status);
		qry.setParameter("legalEntityCode", legalEntityCode);
		qry.setParameter("auditTypeCode", auditTypeCode);
		
		if(!BankAuditUtil.isEmptyString(planId)){
			qry.setParameter("planId", planId);
		}
		if(!BankAuditUtil.isEmptyString(auditId)){
			qry.setParameter("auditId", auditId);
		}
		if(!BankAuditUtil.isEmptyString(documentType)){
			qry.setParameter("documentType", documentType);
		}
		if(!BankAuditUtil.isEmptyString(documentSubType)){
			qry.setParameter("documentSubType", documentSubType);
		}
		if(!BankAuditUtil.isEmptyString(observationId)){
			qry.setParameter("observationId", observationId);
		}
		if(!BankAuditUtil.isEmptyString(documentName) && documentName.equalsIgnoreCase("Board Resolution")){
			qry.setParameter("documentName", documentName);
		}
		
		if(BankAuditConstant.STATUS_AUTH.equalsIgnoreCase(status) ) {
			return qry.list(); 
		}else{
			List<AuditDocumentDetails> listAuditDocumentDetails = new ArrayList<AuditDocumentDetails>();
			List<AuditDocumentDetailsWrk> listAuditDocumentDetailsWrk = qry.list(); 
			for (AuditDocumentDetailsWrk auditDocumentDetailsWrk : listAuditDocumentDetailsWrk) {
				AuditDocumentDetails auditDocumentDetails = new AuditDocumentDetails();
				BeanUtils.copyProperties(auditDocumentDetailsWrk, auditDocumentDetails);
				listAuditDocumentDetails.add(auditDocumentDetails);
			}
			return listAuditDocumentDetails;
		}
	}
	
	@Override
	public String deleteDocumentById(Integer legalEntityCode, Integer documentId, String status){
		logger.info("Inside deleteDocumentById .." + documentId);
		try{
			
			StringBuilder deleteQuery = new StringBuilder();
			 
			if(BankAuditConstant.STATUS_AUTH.equalsIgnoreCase(status) ){
				deleteQuery.append( " Update AuditDocumentDetails auditDocumentDetails "			
					+ " set auditDocumentDetails.status='"+BankAuditConstant.STATUS_DEL+"' Where id =:documentId  ");
			}
			else { 
				deleteQuery.append( " Update AuditDocumentDetailsWrk auditDocumentDetails  "
					+ " set auditDocumentDetails.status='"+BankAuditConstant.STATUS_DEL+"' Where id =:documentId ");
			}
			 
			
			Session session =getSession();			
			Query query = session.createQuery(deleteQuery.toString()).setParameter("documentId", documentId);				 
			query.executeUpdate();
			return BankAuditConstant.SUCCESS;
		}catch (Exception e) {
			logger.error("Error .." + e.getMessage() +" :: "+e.getCause());
			return BankAuditConstant.FAILED;
		}
	}
	
	@Override
	public String deleteAllDocumentBy(Integer legalEntityCode, String auditTypeCode,String planId, String auditId, String observationId,
			String documentType, String documentSubType){
		
		logger.info("Inside deleteAllDocumentBy .." + auditTypeCode+" :: "+ planId +" :: "+auditId+" :: "+ observationId +" :: "+documentType);
		try{
			StringBuilder deleteQuery= new StringBuilder(" Update AuditDocumentDetails auditDocumentDetails "
					+ " set auditDocumentDetails.status='"+BankAuditConstant.STATUS_DEL+"' where"
					+ " legalEntityCode =:legalEntityCode"
					+ " and auditTypeCode =:auditTypeCode "
					+ " and planId =:planId "
					+ " and auditId =:auditId "					
					+ " and documentType =:documentType"
					+ " and documentSubType =:documentSubType ") ;
			if(!BankAuditUtil.isEmptyString(observationId)){
				deleteQuery =deleteQuery.append(" and observationId =:observationId ");
			}
			
			
			Session session =getSession();			
			Query query = session.createQuery(deleteQuery.toString())
			.setParameter("legalEntityCode", legalEntityCode)
			.setParameter("auditTypeCode", auditTypeCode)
			.setParameter("planId", planId)
			.setParameter("auditId", auditId)
			.setParameter("documentType", documentType)
			.setParameter("documentSubType", documentSubType);
			if(!BankAuditUtil.isEmptyString(observationId)){
				query.setParameter("observationId", observationId );
			}
			query.executeUpdate();
			return BankAuditConstant.SUCCESS;
		}catch (Exception e) {
			logger.error("Error .." + e.getMessage() +" :: "+e.getCause());
			return BankAuditConstant.FAILED;
		}
	}

	@Override
	public String getReferencePathById(Integer legalEntityCode, Integer documentId, String status, String auditId){
		Session session =getSession();
		session.setDefaultReadOnly(true);
	 	StringBuilder str =new StringBuilder("  ");
		
		if(BankAuditConstant.STATUS_AUTH.equalsIgnoreCase(status) ) str.append( "Select referencePath from AuditDocumentDetails where legalEntityCode=:legalEntityCode AND id=:id ");
		else  str.append( "Select referencePath from AuditDocumentDetailsWrk where legalEntityCode=:legalEntityCode AND id=:id ");

		if(!BankAuditUtil.isEmptyString(auditId)){
			str.append(" and auditId = :auditId ");
		}
		Query query = session.createQuery(str.toString())
					.setParameter("legalEntityCode", legalEntityCode)
					.setParameter("id", documentId);
			if(!BankAuditUtil.isEmptyString(auditId)){
				query.setParameter("auditId", auditId);
			}
			
		return (String)query.uniqueResult(); 
		/*return (String)(session.createQuery(str.toString())
				.setParameter("legalEntityCode", legalEntityCode)
				.setParameter("id", documentId).uniqueResult());*/
	}
	
	@Override
	public String getCountOfDocuments(Integer legalEntityCode, String auditTypeCode, String planId, String auditId, 
			String observationId, String documentType, String documentSubType, String status, String documentName){
		
		Session session =getSession();
		try{
			session.setDefaultReadOnly(true);		 
			StringBuilder str = new StringBuilder("   ");
					
				if(BankAuditConstant.STATUS_AUTH.equalsIgnoreCase(status) ) str.append( " Select count(*) from AuditDocumentDetails where  ");
				else  str.append( " Select count(*) from AuditDocumentDetailsWrk where   ");
				
				str.append( " status =:statusVal "
						+ " and legalEntityCode =:legalEntityCode "
						+ " and auditTypeCode =:auditTypeCode ");
				
				if(!BankAuditUtil.isEmptyString(planId)){
					str.append( " and planId =:planId ");
				}
				if(!BankAuditUtil.isEmptyString(auditId)){
					str.append( " and auditId =:auditId ");
				}
				if(!BankAuditUtil.isEmptyString(documentType)){
					str.append( " and documentType = :documentType ");
				}
				if(!BankAuditUtil.isEmptyString(documentSubType)){
					str.append("and documentSubType =:documentSubType ");
				}
				if(!BankAuditUtil.isEmptyString(observationId)){
					str.append("and observationId =:observationId ");
				}
				if(!BankAuditUtil.isEmptyString(documentName) && documentName.equalsIgnoreCase("Board Resolution")){
					str.append("and documentName =:documentName ");
				}
					
			
			Query qry = session.createQuery(str.toString());
			qry.setParameter("statusVal", status);
			qry.setParameter("legalEntityCode", legalEntityCode);
			qry.setParameter("auditTypeCode", auditTypeCode);
			
			if(!BankAuditUtil.isEmptyString(planId)){
				qry.setParameter("planId", planId);
			}
			if(!BankAuditUtil.isEmptyString(auditId)){
				qry.setParameter("auditId", auditId);
			}
			if(!BankAuditUtil.isEmptyString(documentType)){
				qry.setParameter("documentType", documentType);
			}
			if(!BankAuditUtil.isEmptyString(documentSubType)){
				qry.setParameter("documentSubType", documentSubType);
			}
			if(!BankAuditUtil.isEmptyString(observationId)){
				qry.setParameter("observationId", observationId);
			}
			if(!BankAuditUtil.isEmptyString(documentName) && documentName.equalsIgnoreCase("Board Resolution")){
				qry.setParameter("documentName", documentName);
			}
			
			if(BankAuditConstant.STATUS_AUTH.equalsIgnoreCase(status) ) {
				return String.valueOf((Long)qry.uniqueResult()); 
				//(Long)query.uniqueResult();
			}else{
				List<AuditDocumentDetails> listAuditDocumentDetails = new ArrayList<AuditDocumentDetails>();
				List<AuditDocumentDetailsWrk> listAuditDocumentDetailsWrk = qry.list(); 
				for (AuditDocumentDetailsWrk auditDocumentDetailsWrk : listAuditDocumentDetailsWrk) {
					AuditDocumentDetails auditDocumentDetails = new AuditDocumentDetails();
					BeanUtils.copyProperties(auditDocumentDetailsWrk, auditDocumentDetails);
					listAuditDocumentDetails.add(auditDocumentDetails);
				}
				return String.valueOf((Long)qry.uniqueResult()); 
			}
		}catch (Exception e) {
			logger.error("Error .." + e.getMessage() +" :: "+e.getCause());
			return BankAuditConstant.FAILED;
		}
	}
	
	@Override
	public String submitObservationDocuments(Integer legalEntityCode, String auditTypeCode, String planId, String auditId,
			 String role, String documentType, String documentSubType, String observationId){
		 
		try{
			
			StringBuilder updateQuery= new StringBuilder(" Update AuditDocumentDetails auditDocumentDetails "
					+ " set auditDocumentDetails.entityStatus='"+BankAuditConstant.STATUS_AUTH+"' where"
					+ " legalEntityCode =:legalEntityCode"
					+ " and auditTypeCode =:auditTypeCode "
					+ " and planId =:planId "
					+ " and auditId =:auditId "					
					+ " and documentType =:documentType"
					+ " and documentSubType =:documentSubType"
					+ " and auditDocumentDetails.entityStatus = '"+BankAuditConstant.STATUS_PENDING_AUTH+"' ") ;
			if(!BankAuditUtil.isEmptyString(observationId)){
				 updateQuery.append(" and observationId =:observationId ");
			}			
			
			Session session =getSession();			
			Query query = session.createQuery(updateQuery.toString())
			.setParameter("legalEntityCode", legalEntityCode)
			.setParameter("auditTypeCode", auditTypeCode)
			.setParameter("planId", planId)
			.setParameter("auditId", auditId)
			.setParameter("documentType", documentType)
			.setParameter("documentSubType", documentSubType);
			if(!BankAuditUtil.isEmptyString(observationId)){
				query.setParameter("observationId", observationId );
			}
			
			query.executeUpdate();
			return BankAuditConstant.SUCCESS;
			
		}catch (Exception e) {
			logger.error("Error .." + e.getMessage() +" :: "+e.getCause());
			return BankAuditConstant.FAILED;
		}	
	}
	
	/*@Override
	public List<AuditDocumentDetails> getAuditDocumentDetailCompliance(Integer legalEntityCode, String auditTypeCode,
			String planId, String auditId, String observationId, String documentType, String documentName, String documentSubType) {
		Session session =getSession();
		
		session.setDefaultReadOnly(true);		 
		
		StringBuilder str= new StringBuilder("from AuditDocumentDetails where "
				+ " status =:statusVal "
				+ " and legalEntityCode =:legalEntityCode"
				+ " and auditTypeCode =:auditTypeCode "
				+ " and planId =:planId "
				+ " and auditId =:auditId "
				+ " and documentName =:documentName ");				
				
				if(!BankAuditUtil.isEmptyString(documentType)){
					str.append(" and documentType =:documentType "	);	
				}
				if(!BankAuditUtil.isEmptyString(documentSubType)){
					str.append( " and documentSubType =:documentSubType ");
				}
			
			Query qry = session.createQuery(str.toString())
						.setParameter("statusVal", BankAuditConstant.STATUS_ACTIVE)
						.setParameter("legalEntityCode", legalEntityCode)
						.setParameter("auditTypeCode", auditTypeCode)
						.setParameter("planId", planId)
						.setParameter("auditId", auditId)						
						.setParameter("documentName", documentName);
									
						if(!BankAuditUtil.isEmptyString(documentType)){
							qry.setParameter("documentType", documentType);
						}
						if(!BankAuditUtil.isEmptyString(documentSubType)){
							qry.setParameter("documentSubType", documentSubType);
						}
			
			return qry.list();		
 
	}*/

}
