package com.bankaudit.rest.process.service;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bankaudit.model.User;
import com.bankaudit.process.model.AuditSchedule;
import com.bankaudit.process.model.AuditScheduleStagesStatusHst;
import com.bankaudit.rest.process.dao.AuditScheduleStagesStatusHstDao;
import com.bankaudit.util.BankAuditUtil;

@Service("auditScheduleStagesStatusHstService")
@Transactional("transactionManager")
public class AuditScheduleStagesStatusHstServiceImpl implements AuditScheduleStagesStatusHstService{

	@Autowired
	AuditScheduleStagesStatusHstDao auditScheduleStagesStatusHstDao;
	
	@Override
	public void saveAuditStagesStatus(AuditSchedule auditSchedule, AuditScheduleStagesStatusHst auditScheduleStagesStatusHstData){	 
		
		//if(!BankAuditUtil.isEmptyString(auditSchedule.getUserId())){
			Map<String, Object> properties = new HashMap<>();
			User userAssignee = null;
			if(!BankAuditUtil.isEmptyString(auditSchedule.getAssigneeId())){
				properties.put("legalEntityCode", auditSchedule.getLegalEntityCode());
				properties.put("userId", auditSchedule.getAssigneeId());		
				userAssignee = (User) auditScheduleStagesStatusHstDao.getUniqueEntityByMatchingProperties(User.class, properties);
			}
			properties.put("userId", auditSchedule.getActionMakerId());		
			User userMaker = (User) auditScheduleStagesStatusHstDao.getUniqueEntityByMatchingProperties(User.class, properties);
			
			AuditScheduleStagesStatusHst auditScheduleStagesStatusHst = new AuditScheduleStagesStatusHst(); 
			auditScheduleStagesStatusHst.setLegalEntityCode(auditSchedule.getLegalEntityCode());
			auditScheduleStagesStatusHst.setAuditTypeCode(auditSchedule.getAuditTypeCode());
			auditScheduleStagesStatusHst.setAuditId(auditSchedule.getAuditId());
			auditScheduleStagesStatusHst.setPlanId(auditSchedule.getPlanId());
			auditScheduleStagesStatusHst.setUnitId(auditSchedule.getUnitId());
			
			auditScheduleStagesStatusHst.setAuditStatus(auditSchedule.getAuditStatus());
			auditScheduleStagesStatusHst.setObservationStatus(auditSchedule.getObservationStatus());
			auditScheduleStagesStatusHst.setReportStatus(auditSchedule.getReportStatus());
			auditScheduleStagesStatusHst.setRespondStatus(auditSchedule.getRespondStatus());
			auditScheduleStagesStatusHst.setReviewStatus(auditSchedule.getReviewStatus());			 
			
			if(auditScheduleStagesStatusHstData != null){
				if(!BankAuditUtil.isEmptyString(auditScheduleStagesStatusHstData.getUnitId())){
					auditScheduleStagesStatusHst.setUnitId(auditScheduleStagesStatusHstData.getUnitId());
				}
				auditScheduleStagesStatusHst.setId(auditScheduleStagesStatusHstData.getId());
				auditScheduleStagesStatusHst.setStagesStatus(auditScheduleStagesStatusHstData.getStagesStatus());
				auditScheduleStagesStatusHst.setCode(auditScheduleStagesStatusHstData.getCode());
				auditScheduleStagesStatusHst.setActionType(auditScheduleStagesStatusHstData.getActionType());				
			}
			auditScheduleStagesStatusHst.setMakerId(auditSchedule.getActionMakerId());
			auditScheduleStagesStatusHst.setMakerTimestamp(new Timestamp(System.currentTimeMillis()));
			auditScheduleStagesStatusHst.setMakerRole(auditSchedule.getActionMakerRole());
			auditScheduleStagesStatusHst.setMakerGrade(userMaker.getGrade());			
			auditScheduleStagesStatusHst.setMakerDesignation(userMaker.getDesignation());
			auditScheduleStagesStatusHst.setMakerDepartment(userMaker.getDepartment());
			
			if(userAssignee != null){
				auditScheduleStagesStatusHst.setAssigneeId(auditSchedule.getAssigneeId());
				auditScheduleStagesStatusHst.setAssigneeGrade(userAssignee.getGrade());
				auditScheduleStagesStatusHst.setAssigneeDesignation(userAssignee.getDesignation());
				auditScheduleStagesStatusHst.setAssigneeDepartment(userAssignee.getDepartment());
				auditScheduleStagesStatusHst.setAssigneeRole(auditScheduleStagesStatusHstData!=null?auditScheduleStagesStatusHstData.getAssigneeRole():null);
			}
			if("WS".equalsIgnoreCase(auditSchedule.getReviewStatus())) { 
				auditScheduleStagesStatusHst.setAuditStatus("AW"); 
			}
			//if("RRR".equalsIgnoreCase(auditScheduleStagesStatusHst.getStagesStatus()) && "RRR".equalsIgnoreCase(auditSchedule.getReportStatus())
			if("RRR".equalsIgnoreCase(auditScheduleStagesStatusHst.getStagesStatus()) || "RA".equalsIgnoreCase(auditScheduleStagesStatusHst.getStagesStatus())){  // The assignee role made it to Allocator, when the Report status is Review Ready.
				auditScheduleStagesStatusHst.setAssigneeRole("G11");
			}else if("RS".equalsIgnoreCase(auditScheduleStagesStatusHst.getStagesStatus()) 
					&& "RS".equalsIgnoreCase(auditSchedule.getRespondStatus())){
				auditScheduleStagesStatusHst.setAssigneeRole("G7");
			}
			
			
			auditScheduleStagesStatusHstDao.flushSession(); 
			auditScheduleStagesStatusHstDao.saveOrUpdate(auditScheduleStagesStatusHst);
		//}
	}
	
	@Override
	public AuditScheduleStagesStatusHst getLatestRecordByMatchingProperties(Map<String, Object> auditSchStagesStatusHstProperties){
		return auditScheduleStagesStatusHstDao.getLatestRecordByMatchingProperties(auditSchStagesStatusHstProperties);
	}
	
	/*@Override
	public void saveOrUpdateAuditStagesStatus(AuditSchedule auditSchedule, AuditScheduleStagesStatusHst auditScheduleStagesStatusHstData){	 
	 
		//if(!BankAuditUtil.isEmptyString(auditSchedule.getUserId())){
		
			Map<String, Object> propertiesUser = new HashMap<>();
			propertiesUser.put("legalEntityCode", auditSchedule.getLegalEntityCode());
			propertiesUser.put("userId", auditSchedule.getUserId());		
			User user = (User) auditScheduleStagesStatusHstDao.getUniqueEntityByMatchingProperties(User.class, propertiesUser);
					
			Map<String, Object> properties = new HashMap<>();
			properties.put("legalEntityCode", auditSchedule.getLegalEntityCode());
			properties.put("auditTypeCode", auditSchedule.getAuditTypeCode());
			properties.put("auditId", auditSchedule.getAuditId());	 
			
			if(auditScheduleStagesStatusHstData != null){
				if(!BankAuditUtil.isEmptyString(auditScheduleStagesStatusHstData.getStagesStatus())){
					properties.put("stagesStatus", auditScheduleStagesStatusHstData.getStagesStatus());
				}
				if(!BankAuditUtil.isEmptyString(auditScheduleStagesStatusHstData.getCode())){
					properties.put("code", auditScheduleStagesStatusHstData.getCode());
				}
				if(!BankAuditUtil.isEmptyString(auditScheduleStagesStatusHstData.getActionType())){
					properties.put("actionType", auditScheduleStagesStatusHstData.getActionType());
				}
			}
			AuditScheduleStagesStatusHst auditScheduleStagesStatusHst = (AuditScheduleStagesStatusHst) 
					auditScheduleStagesStatusHstDao.getUniqueEntityByMatchingProperties(AuditScheduleStagesStatusHst.class, properties);	
			 			
			if(auditScheduleStagesStatusHst == null){
				auditScheduleStagesStatusHst = new AuditScheduleStagesStatusHst();
				BeanUtils.copyProperties(auditScheduleStagesStatusHstData, auditScheduleStagesStatusHst); 
			}		
			//BeanUtils.copyProperties(auditSchedule, auditScheduleStagesStatusHst); 
			auditScheduleStagesStatusHst.setLegalEntityCode(auditSchedule.getLegalEntityCode());
			auditScheduleStagesStatusHst.setAuditTypeCode(auditSchedule.getAuditTypeCode());
			auditScheduleStagesStatusHst.setAuditId(auditSchedule.getAuditId());
			auditScheduleStagesStatusHst.setPlanId(auditSchedule.getPlanId());
			auditScheduleStagesStatusHst.setUnitId(auditSchedule.getUnitId());
			
			auditScheduleStagesStatusHst.setAuditStatus(auditSchedule.getAuditStatus());
			auditScheduleStagesStatusHst.setObservationStatus(auditSchedule.getObservationStatus());
			auditScheduleStagesStatusHst.setReportStatus(auditSchedule.getReportStatus());
			auditScheduleStagesStatusHst.setRespondStatus(auditSchedule.getRespondStatus());
			auditScheduleStagesStatusHst.setReviewStatus(auditSchedule.getReviewStatus());
			
			auditScheduleStagesStatusHst.setUserId(auditSchedule.getUserId());
			auditScheduleStagesStatusHst.setUserTimestamp(new Timestamp(System.currentTimeMillis()));
			auditScheduleStagesStatusHst.setMaker(auditSchedule.getUserIdMaker());
			auditScheduleStagesStatusHst.setMakerTimestamp(new Timestamp(System.currentTimeMillis()));
			auditScheduleStagesStatusHst.setRole(auditSchedule.getRole());
			auditScheduleStagesStatusHst.setGrade(user.getGrade());
			auditScheduleStagesStatusHst.setDesignation(user.getDesignation());
			auditScheduleStagesStatusHst.setDepartment(user.getDepartment());
			
			if("WS".equalsIgnoreCase(auditSchedule.getReviewStatus())) { 
				auditScheduleStagesStatusHst.setAuditStatus("AW"); 
			}
			
			auditScheduleStagesStatusHstDao.save(auditScheduleStagesStatusHst);
		//}
	}*/
	
}
