package com.bankaudit.rest.process.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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.DataTableResponse;
import com.bankaudit.dto.process.AlertsDto;
import com.bankaudit.dto.process.AlertsResponseDto;
import com.bankaudit.process.model.AuditSchedule;
import com.bankaudit.process.model.TaskDetails;
import com.bankaudit.rest.process.dao.AlertToDoDao;
import com.bankaudit.rest.process.dao.ReportDao;
import com.bankaudit.service.BusinessDateService;
import com.bankaudit.service.MaintEntityService;

@Service
@Transactional("transactionManager")
public class AlertToDoServiceImpl implements AlertToDoService {
	
	@Autowired
	AlertToDoDao alertToDoDao;
	
	@Autowired
	BusinessDateService businessDateService; 
	
	@Autowired
	MaintEntityService maintEntityService;
	
	@Autowired
	ReportDao reportDao;
	
	static final Logger logger = Logger.getLogger(AlertToDoServiceImpl.class);
	
	@Override
	public Map<String, Integer> getAlertsCount(Integer legalEntityCode, String userId, String role) {
		return alertToDoDao.getAlertsCount(legalEntityCode, userId, role);
	}
	
	@Override
	public DataTableResponse getAlertsByUserIdAlertIdDataTable(int legalEntityCode,String userId,String alertId,String role, String search,Integer orderColumn,
			String orderDirection, Integer page, Integer size) {
			DataTableResponse ds=alertToDoDao.getAlertsByUserIdAlertIdDataTable(legalEntityCode,userId,alertId,role,search,orderColumn,
				orderDirection,page,size);
		return ds;
	}
	
	@Override
	public List<AlertsResponseDto> getListofAlerts(Integer legalEntityCode, String userId, String role, String alertId) {
		return alertToDoDao.getListofAlerts(legalEntityCode, userId, role, alertId);
	}
	
	@Override
	public Map<String, Integer> getAlertsCountByUserIdRole(Integer legalEntityCode, String userId, String role, String unitId) {
		Map<String, Integer> alertsCountByUserIdRoleMap= null;
		alertsCountByUserIdRoleMap = new HashMap<>();
		List<AuditSchedule> planAuthCnt = null;
		Integer riskAssessedCnt=0; Integer auditInitiateCnt =0; Integer auditSubmitCnt=0; Integer actionPlanInitiateCnt=0; Integer RCSAClosureCnt=0;
		Integer taskClosureCnt=0;
		Map<String, Integer> taskOpenOverdueCntMap= null;
		boolean isDeptAsRMD =false;
		List<String> unitList = null;  List<String> deptList = null;
		try {
			logger.info("getAlertsCountByUserIdRole  .. "+legalEntityCode+" :: "+userId+" :: "+ role+" :: "+ unitId);
			Date businessDate =businessDateService.getBusinessDateTimestampByLegalEntityCode(legalEntityCode);
			
			if("G1".equalsIgnoreCase(role) || "G6".equalsIgnoreCase(role) ) {
				unitList=maintEntityService.getSubBranchesGyUserIdOrUnitId(legalEntityCode,"unit",unitId);
				//unitList=maintEntityService.getSubBranchesGyUserIdOrUnitId(legalEntityCode,"user",userId);
			}else {
				unitList = new ArrayList<>();
				unitList.add(unitId);
			}
			
			isDeptAsRMD = reportDao.isDeptAsRMD(legalEntityCode, role, userId);
			if(!isDeptAsRMD) {
				deptList =reportDao.getDeptList(legalEntityCode, role, userId);
				if(deptList==null) {
					deptList = new ArrayList();
					deptList.add("NoDept");
				}
			}
			logger.info("isDeptAsRMD .."+isDeptAsRMD+" deptList .. "+deptList);
			logger.info("unitList .."+unitList);
			
			// G1:Operation, G2:Jr. Assessor, G3:Auditor, G4:Jr. Advisor, G5:Admin, G6:Management, G7:Advisor, G8: Assessor
			// G1, G6 -> Unit list Jurisdiction for HO Users.
			// G4, G7, G1, G6 -> data based on the Depts they belongs
			
			if("G2".equalsIgnoreCase(role) || "G8".equalsIgnoreCase(role)) // Only for Assessor
				planAuthCnt = alertToDoDao.getPlanAuth(legalEntityCode, userId, role, unitId);
			
			if("G2".equalsIgnoreCase(role) || "G8".equalsIgnoreCase(role) || "G3".equalsIgnoreCase(role)) // For Assessor and Auditor
				riskAssessedCnt=alertToDoDao.getRiskAssessedCnt(legalEntityCode, userId, role, unitId);
			
			if("G2".equalsIgnoreCase(role) || "G8".equalsIgnoreCase(role) || "G3".equalsIgnoreCase(role) || "G4".equalsIgnoreCase(role) || "G7".equalsIgnoreCase(role))
				auditInitiateCnt=alertToDoDao.getAuditInitiationCnt(legalEntityCode, userId, role, unitId,isDeptAsRMD,deptList);
			
			if("G4".equalsIgnoreCase(role) || "G7".equalsIgnoreCase(role))
				auditSubmitCnt=alertToDoDao.getAuditSubmitCnt(legalEntityCode, userId, role, unitId, isDeptAsRMD,deptList);
			
			if("G4".equalsIgnoreCase(role) || "G7".equalsIgnoreCase(role) || "G6".equalsIgnoreCase(role))
				actionPlanInitiateCnt=alertToDoDao.getActionPlanInitiateCnt(legalEntityCode, userId, role, unitId, unitList, isDeptAsRMD, deptList);
			
			if("G3".equalsIgnoreCase(role) || "G4".equalsIgnoreCase(role) || "G7".equalsIgnoreCase(role)  || "G1".equalsIgnoreCase(role) || "G6".equalsIgnoreCase(role))
				RCSAClosureCnt=alertToDoDao.getRCSAClosureCnt(legalEntityCode, userId, role, unitId, businessDate, unitList, isDeptAsRMD, deptList);
			
			if("G4".equalsIgnoreCase(role) || "G7".equalsIgnoreCase(role) || "G6".equalsIgnoreCase(role) || "G1".equalsIgnoreCase(role)) { // For Advisor and Management
				taskClosureCnt=alertToDoDao.getTaskClosureCnt(legalEntityCode, userId, role, unitId, businessDate, unitList, isDeptAsRMD, deptList);
			 	taskOpenOverdueCntMap =alertToDoDao.getTaskOpenOverdueCount(legalEntityCode, userId, role, unitId, businessDate, unitList, isDeptAsRMD, deptList);
			}
			System.out.println("before  .. ");
			alertsCountByUserIdRoleMap.put("PLAN_AUTH",(planAuthCnt != null)? planAuthCnt.size():0);
			alertsCountByUserIdRoleMap.put("RISK_ASSESSED",riskAssessedCnt);
			alertsCountByUserIdRoleMap.put("VALIDATION_INITIATED",auditInitiateCnt);
			alertsCountByUserIdRoleMap.put("VALIDATION_SUBMITTED",auditSubmitCnt);
			alertsCountByUserIdRoleMap.put("ACTION_PLAN_INITIATED",actionPlanInitiateCnt);
			alertsCountByUserIdRoleMap.put("RCSA_CLOSURE",RCSAClosureCnt);
			alertsCountByUserIdRoleMap.put("TASK_CLOSURE",taskClosureCnt);
			if(taskOpenOverdueCntMap != null) {
				alertsCountByUserIdRoleMap.put("OPEN_TASK_COUNT", (taskOpenOverdueCntMap.get("OPEN_TASK_COUNT") != null)?taskOpenOverdueCntMap.get("OPEN_TASK_COUNT"):0 );
				alertsCountByUserIdRoleMap.put("OVERDUE_TASK_COUNT",(taskOpenOverdueCntMap.get("OVERDUE_TASK_COUNT") != null)?taskOpenOverdueCntMap.get("OVERDUE_TASK_COUNT"):0 );
			}else {
				alertsCountByUserIdRoleMap.put("OPEN_TASK_COUNT",0);
				alertsCountByUserIdRoleMap.put("OVERDUE_TASK_COUNT",0);
			}
			System.out.println("alertsCountByUserIdRoleMap .. "+alertsCountByUserIdRoleMap);
		}catch(Exception e) {
			e.printStackTrace(System.out);
		}
		return alertsCountByUserIdRoleMap;
	}
	
	/*@Override
	public List<AuditSchedule> getPlanAuth(Integer legalEntityCode, String userId, String role, String unitId){
		return alertToDoDao.getPlanAuth(legalEntityCode, userId, role, unitId);
	}*/
	
	@Override
	public DataTableResponse getPlanAuth(Integer legalEntityCode, String search, Integer orderColumn, String orderDirection,
			 Integer page, Integer size, String userId, String role, String unitId, String alertName){
		return alertToDoDao.getPlanAuth(legalEntityCode,search,orderColumn,orderDirection,page,size, userId, role, unitId,alertName);
	}
	
	@Override
	public DataTableResponse getRiskAssessedAlerts(Integer legalEntityCode, String search, Integer orderColumn, String orderDirection,
			 Integer page, Integer size, String userId, String role, String unitId, String alertName){
		return alertToDoDao.getRiskAssessedAlerts(legalEntityCode,search,orderColumn,orderDirection,page,size, userId, role, unitId,alertName);
	}
	
	@Override
	public DataTableResponse getValidationInitiatedAlerts(Integer legalEntityCode, String search, Integer orderColumn, String orderDirection,
			 Integer page, Integer size, String userId, String role, String unitId, String alertName){
		boolean isDeptAsRMD= false;
		List<String> deptList = null;
		try {
			if("G4".equalsIgnoreCase(role) || "G7".equalsIgnoreCase(role)) {
				isDeptAsRMD = reportDao.isDeptAsRMD(legalEntityCode, role, userId);
				if(!isDeptAsRMD) {
					deptList =reportDao.getDeptList(legalEntityCode, role, userId);
					if(deptList==null) {
						deptList = new ArrayList();	deptList.add("NoDept");
					}
				}
			}
			logger.info("isDeptAsRMD .."+isDeptAsRMD+" deptList .. "+deptList);
		}catch(Exception e) {
			e.printStackTrace(System.out);
		}
		return alertToDoDao.getAuditInitiatedAlerts(legalEntityCode,search,orderColumn,orderDirection,page,size, userId, role, unitId,alertName,isDeptAsRMD,deptList);
	}
	
	@Override
	public DataTableResponse getValidationSubmittedAlerts(Integer legalEntityCode, String search, Integer orderColumn, String orderDirection,
			 Integer page, Integer size, String userId, String role, String unitId, String alertName){
		boolean isDeptAsRMD= false;
		List<String> deptList = null;
		try {
			isDeptAsRMD = reportDao.isDeptAsRMD(legalEntityCode, role, userId);
			if(!isDeptAsRMD) {
				deptList =reportDao.getDeptList(legalEntityCode, role, userId);
				if(deptList==null) {
					deptList = new ArrayList();	deptList.add("NoDept");
				}
			}
			logger.info("isDeptAsRMD .."+isDeptAsRMD+" deptList .. "+deptList);
		}catch(Exception e) {
			e.printStackTrace(System.out);
		}
		return alertToDoDao.getAuditSubmittedAlerts(legalEntityCode,search,orderColumn,orderDirection,page,size, userId, role, unitId,alertName,isDeptAsRMD,deptList);
	}
	
	@Override
	public DataTableResponse getActionPlanInitiatedAlerts(Integer legalEntityCode, String search, Integer orderColumn, String orderDirection,
			 Integer page, Integer size, String userId, String role, String unitId, String alertName){
		boolean isDeptAsRMD= false;
		List<String> deptList = null;	List<String> unitList = null;
		try {
			if("G6".equalsIgnoreCase(role)) {
				unitList=maintEntityService.getSubBranchesGyUserIdOrUnitId(legalEntityCode,"unit",unitId);
			}
			isDeptAsRMD = reportDao.isDeptAsRMD(legalEntityCode, role, userId);
			if(!isDeptAsRMD) {
				deptList =reportDao.getDeptList(legalEntityCode, role, userId);
				if(deptList==null) {
					deptList = new ArrayList();	deptList.add("NoDept");
				}
			}
			logger.info("isDeptAsRMD .."+isDeptAsRMD+" deptList .. "+deptList);
		}catch(Exception e) {
			e.printStackTrace(System.out);
		}
		return alertToDoDao.getActionPlanInitiatedAlerts(legalEntityCode,search,orderColumn,orderDirection,page,size, userId, role, unitId, alertName, unitList,isDeptAsRMD,deptList );
	}
	
	@Override
	public DataTableResponse getRCSAClosureAlerts(Integer legalEntityCode, String search, Integer orderColumn, String orderDirection,
			 Integer page, Integer size, String userId, String role, String unitId, String alertName){
		Date businessDate =businessDateService.getBusinessDateTimestampByLegalEntityCode(legalEntityCode);
		boolean isDeptAsRMD= false;
		List<String> deptList = null; List<String> unitList = null;
		if("G1".equalsIgnoreCase(role) || "G6".equalsIgnoreCase(role)) {
			unitList=maintEntityService.getSubBranchesGyUserIdOrUnitId(legalEntityCode,"unit",unitId);
		}else {
			unitList = new ArrayList<String>();
			unitList.add(unitId);
		}
		if(! "G3".equalsIgnoreCase(role)) {
			isDeptAsRMD = reportDao.isDeptAsRMD(legalEntityCode, role, userId);
			if(!isDeptAsRMD) {
				deptList =reportDao.getDeptList(legalEntityCode, role, userId);
				if(deptList==null) {
					deptList = new ArrayList();	deptList.add("NoDept");
				}
			}
		}
		logger.info("isDeptAsRMD .."+isDeptAsRMD+" deptList .. "+deptList);
		return alertToDoDao.getRCSAClosureAlerts(legalEntityCode,search,orderColumn,orderDirection,page,size, userId, role, unitId,alertName,businessDate, unitList,isDeptAsRMD,deptList);
	}
		
	/*@Override
	public List<TaskDetails> getTaskClosureAlerts(Integer legalEntityCode, String userId, String role, String unitId){
		return alertToDoDao.getTaskClosureAlerts(legalEntityCode, userId, role, unitId);
	}
	@Override
	public List<TaskDetails> getOpenTaskAlerts(Integer legalEntityCode, String userId, String role, String unitId){
		return alertToDoDao.getOpenTaskAlerts(legalEntityCode, userId, role, unitId);
	}*/
	
	@Override
	public DataTableResponse getTaskOpenCloseOverdueAlerts(Integer legalEntityCode, String search, Integer orderColumn, String orderDirection,
			 Integer page, Integer size, String userId, String role, String unitId, String alertName){
		Date businessDate =businessDateService.getBusinessDateTimestampByLegalEntityCode(legalEntityCode);
		boolean isDeptAsRMD= false;
		List<String> deptList = null; List<String> unitList = null;
		if("G1".equalsIgnoreCase(role) || "G6".equalsIgnoreCase(role)) {
			unitList=maintEntityService.getSubBranchesGyUserIdOrUnitId(legalEntityCode,"unit",unitId);
		}else {
			unitList = new ArrayList<String>();
			unitList.add(unitId);
		}
		if(! "G3".equalsIgnoreCase(role)) {
			isDeptAsRMD = reportDao.isDeptAsRMD(legalEntityCode, role, userId);
			if(!isDeptAsRMD) {
				deptList =reportDao.getDeptList(legalEntityCode, role, userId);
				if(deptList==null) {
					deptList = new ArrayList();	deptList.add("NoDept");
				}
			}
		}
		logger.info("isDeptAsRMD .."+isDeptAsRMD+" deptList .. "+deptList);
		return alertToDoDao.getTaskOpenCloseOverdueAlerts(legalEntityCode,search,orderColumn,orderDirection,page,size, userId, role, unitId,alertName,businessDate ,unitList, isDeptAsRMD, deptList);
		//return alertToDoDao.getOverDueTaskAlerts(legalEntityCode, userId, role, unitId);
	}
	
}
