package com.bankaudit.service;

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

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

import com.bankaudit.constants.BankAuditConstant;
import com.bankaudit.dao.MaintControlDesignEffectivenessDao;
import com.bankaudit.dto.DataTableResponse;
import com.bankaudit.model.MaintControlDesignEffectiveness;
import com.bankaudit.model.MaintControlDesignEffectivenessHst;
import com.bankaudit.model.MaintControlDesignEffectivenessWrk;

@Service("maintControlDesignEffectivenessService")
@Transactional("transactionManager")
public class MaintControlDesignEffectivenessServiceImpl implements MaintControlDesignEffectivenessService {

	@Autowired
	MaintControlDesignEffectivenessDao maintControlDesignEffectivenessDao;
	
	@Autowired
	BusinessDateService businessDateService; 
	
	@Override
	public void createMaintControlDesignEffectiveness(MaintControlDesignEffectiveness maintControlDesignEffectiveness)throws Exception {
		
		if(maintControlDesignEffectiveness.getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_UNAUTH)){
			MaintControlDesignEffectivenessWrk maintControlDesignEffectivenessWrk =new MaintControlDesignEffectivenessWrk(); 
			BeanUtils.copyProperties(maintControlDesignEffectiveness, maintControlDesignEffectivenessWrk);
			maintControlDesignEffectivenessDao.save(maintControlDesignEffectivenessWrk);
			
		}else if (maintControlDesignEffectiveness.getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_AUTH)) {
			maintControlDesignEffectivenessDao.save(maintControlDesignEffectiveness);
		}else{
			throw new Exception("invalid status");
		}
	}
	
	@Override
	public void updateMaintControlDesignEffectiveness(MaintControlDesignEffectiveness maintControlDesignEffectiveness) {
		if(!maintControlDesignEffectiveness.getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_AUTH)){
			if(maintControlDesignEffectiveness.getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_UNAUTH)
					|| maintControlDesignEffectiveness.getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_MOD)){
				maintControlDesignEffectiveness.setMakerTimestamp(businessDateService.getBusinessDateTimestampByLegalEntityCode(maintControlDesignEffectiveness.getLegalEntityCode()));
			}
			// get Db object and save into the history
			MaintControlDesignEffectiveness maintControlDesignEffectivenessDb=
					getControlDesignEffByCodeAndType(maintControlDesignEffectiveness.getLegalEntityCode(), maintControlDesignEffectiveness.getControlDesignEffCode(), maintControlDesignEffectiveness.getControlDesignEffType(), BankAuditConstant.STATUS_UNAUTH);
			if(maintControlDesignEffectivenessDb!=null){
				maintControlDesignEffectivenessDao.flushSession();
				MaintControlDesignEffectivenessHst maintControlDesignEffectivenessHst=new MaintControlDesignEffectivenessHst();
				BeanUtils.copyProperties(maintControlDesignEffectivenessDb, maintControlDesignEffectivenessHst);
				maintControlDesignEffectivenessDao.save(maintControlDesignEffectivenessHst);
			}
			//delete the existing object rfrom work  
/*			maintCriticalityDao.deleteMaintCriticalityWrk(maintCriticalityWrk.getLegalEntityCode(),maintCriticalityWrk.getUgRoleCode());*/
			// delete the existing work maintEntityAuditSubgroupMapping 
			MaintControlDesignEffectivenessWrk maintControlDesignEffectivenessWrk=new MaintControlDesignEffectivenessWrk();
			BeanUtils.copyProperties(maintControlDesignEffectiveness, maintControlDesignEffectivenessWrk);
			maintControlDesignEffectivenessDao.flushSession();
			maintControlDesignEffectivenessDao.saveOrUpdate(maintControlDesignEffectivenessWrk);
			
		}else{
			// get Db object and save into the history
			MaintControlDesignEffectiveness maintControlDesignEffectivenessDb=
					getControlDesignEffByCodeAndType(maintControlDesignEffectiveness.getLegalEntityCode(), maintControlDesignEffectiveness.getControlDesignEffCode(), maintControlDesignEffectiveness.getControlDesignEffType(), BankAuditConstant.STATUS_AUTH);
			if(maintControlDesignEffectivenessDb!=null){
				MaintControlDesignEffectivenessHst maintControlDesignEffectivenessHst=new MaintControlDesignEffectivenessHst();
				BeanUtils.copyProperties(maintControlDesignEffectivenessDb, maintControlDesignEffectivenessHst);
				maintControlDesignEffectivenessDao.save(maintControlDesignEffectivenessHst);
			}
			//delete the existing object from both the tables  
			maintControlDesignEffectivenessDao.deleteMaintControlDesignEffectiveness(maintControlDesignEffectiveness.getLegalEntityCode(), maintControlDesignEffectiveness.getControlDesignEffCode(), maintControlDesignEffectiveness.getControlDesignEffType(), BankAuditConstant.STATUS_UNAUTH);
			maintControlDesignEffectivenessDao.flushSession();
			maintControlDesignEffectiveness.setCheckerTimestamp(maintControlDesignEffectiveness.getBusinessdateTimestamp());
			// save again 
			maintControlDesignEffectivenessDao.saveOrUpdate(maintControlDesignEffectiveness);
			 
		}
	}
		
		@Override
		public MaintControlDesignEffectiveness getControlDesignEffByCodeAndType(Integer legalEntityCode,String controlDesignEffCode,
				String controlDesignEffType,String status) {

			Map<String,Object> properties=new HashMap<String,Object>();
			properties.put("controlDesignEffCode", controlDesignEffCode);
			properties.put("controlDesignEffType", controlDesignEffType);
			properties.put("legalEntityCode", legalEntityCode);
			if(!BankAuditConstant.STATUS_AUTH.equalsIgnoreCase(status)){
				MaintControlDesignEffectiveness maintControlDesignEffectiveness=null;
				MaintControlDesignEffectivenessWrk maintControlDesignEffectivenessWrk= (MaintControlDesignEffectivenessWrk)maintControlDesignEffectivenessDao.getUniqueEntityByMatchingProperties(MaintControlDesignEffectivenessWrk.class, properties);			
				if(maintControlDesignEffectivenessWrk!=null){
					maintControlDesignEffectiveness=new MaintControlDesignEffectiveness();
					BeanUtils.copyProperties(maintControlDesignEffectivenessWrk, maintControlDesignEffectiveness);
				}
				return maintControlDesignEffectiveness;
			}else {
				return (MaintControlDesignEffectiveness)maintControlDesignEffectivenessDao.getUniqueEntityByMatchingProperties(MaintControlDesignEffectiveness.class, properties);			
			}
		}
			
		@SuppressWarnings("unchecked")
		@Override
		public List<MaintControlDesignEffectiveness> getControlDesignEffectivenessByType(Integer legalEntityCode,String controlDesignEffType) {
			Map<String,Object> properties=new HashMap<String,Object>();
			properties.put("legalEntityCode", legalEntityCode);
			properties.put("controlDesignEffType", controlDesignEffType);
			properties.put("entityStatus", BankAuditConstant.STATUS_ACTIVE);
			return maintControlDesignEffectivenessDao.getEntitiesByMatchingProperties(MaintControlDesignEffectiveness.class, properties);
		}
	
		@Override
		public DataTableResponse getAllMaintControlDesignEffectiveness(String search, Integer orderColumn, String orderDirection,
				Integer page, Integer size,Integer legalEntityCode) {
			
			return maintControlDesignEffectivenessDao.getAllMaintControlDesignEffectiveness( search,  orderColumn,  orderDirection,
					 page,  size,legalEntityCode);
		}

		@Override
		public Boolean isMaintControlDesignEffectiveness(Integer legalEntityCode, String controlDesignEffCode, String controlDesignEffType) {
			return maintControlDesignEffectivenessDao.isMaintControlDesignEffectiveness( legalEntityCode,  controlDesignEffCode,  controlDesignEffType);
		}
		
		@Override
		public Boolean validateScore(MaintControlDesignEffectiveness maintControlDesignEffectiveness) {
			return maintControlDesignEffectivenessDao.validateScore(maintControlDesignEffectiveness);
		}
		
		@SuppressWarnings("unchecked")
		@Override
		public List<MaintControlDesignEffectiveness> getMaintControlDesignEffectiveness(Integer legalEntityCode) {
			
			Map<String, Object> properties=new HashMap<String,Object>();
			properties.put("legalEntityCode", legalEntityCode);
			properties.put("entityStatus", BankAuditConstant.STATUS_ACTIVE);
			
	        List<MaintControlDesignEffectiveness> maintControlDesignEffectivenessess =  maintControlDesignEffectivenessDao.getEntitiesByMatchingProperties(MaintControlDesignEffectiveness.class, properties);
	        return maintControlDesignEffectivenessess;
		}
}
