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.MaintControlOperatingEffectivenessDao;
import com.bankaudit.dto.DataTableResponse;
import com.bankaudit.model.MaintControlOperatingEffectiveness;
import com.bankaudit.model.MaintControlOperatingEffectivenessHst;
import com.bankaudit.model.MaintControlOperatingEffectivenessWrk;

@Service("maintControlOperatingEffectivenessService")
@Transactional("transactionManager")
public class MaintControlOperatingEffectivenessServiceImpl implements MaintControlOperatingEffectivenessService{
	
	@Autowired
	MaintControlOperatingEffectivenessDao maintControlOperatingEffectivenessDao;
	
	@Autowired
	BusinessDateService businessDateService; 
	
	@Override
	public void createMaintControlOperatingEffectiveness(MaintControlOperatingEffectiveness maintControlOperatingEffectiveness)throws Exception {
		
		if(maintControlOperatingEffectiveness.getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_UNAUTH)){
			MaintControlOperatingEffectivenessWrk maintControlOperatingEffectivenessWrk =new MaintControlOperatingEffectivenessWrk(); 
			BeanUtils.copyProperties(maintControlOperatingEffectiveness, maintControlOperatingEffectivenessWrk);
			maintControlOperatingEffectivenessDao.save(maintControlOperatingEffectivenessWrk);
			
		}else if (maintControlOperatingEffectiveness.getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_AUTH)) {
			maintControlOperatingEffectivenessDao.save(maintControlOperatingEffectiveness);
		}else{
			throw new Exception("invalid status");
		}
	}
	
	@Override
	public void updateMaintControlOperatingEffectiveness(MaintControlOperatingEffectiveness maintControlOperatingEffectiveness) {
		if(!maintControlOperatingEffectiveness.getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_AUTH)){
			if(maintControlOperatingEffectiveness.getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_UNAUTH)
					|| maintControlOperatingEffectiveness.getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_MOD)){
				maintControlOperatingEffectiveness.setMakerTimestamp(businessDateService.getBusinessDateTimestampByLegalEntityCode(maintControlOperatingEffectiveness.getLegalEntityCode()));
			}
			// get Db object and save into the history
			MaintControlOperatingEffectiveness maintControlOperatingEffectivenessDb=
					getControlOperatingEffByCodeAndType(maintControlOperatingEffectiveness.getLegalEntityCode(), maintControlOperatingEffectiveness.getControlOperatingEffCode(), maintControlOperatingEffectiveness.getControlOperatingEffType(), BankAuditConstant.STATUS_UNAUTH);
			if(maintControlOperatingEffectivenessDb!=null){
				maintControlOperatingEffectivenessDao.flushSession();
				MaintControlOperatingEffectivenessHst maintControlOperatingEffectivenessHst=new MaintControlOperatingEffectivenessHst();
				BeanUtils.copyProperties(maintControlOperatingEffectivenessDb, maintControlOperatingEffectivenessHst);
				maintControlOperatingEffectivenessDao.save(maintControlOperatingEffectivenessHst);
			}
			//delete the existing object rfrom work  
/*			maintCriticalityDao.deleteMaintCriticalityWrk(maintCriticalityWrk.getLegalEntityCode(),maintCriticalityWrk.getUgRoleCode());*/
			// delete the existing work maintEntityAuditSubgroupMapping 
			MaintControlOperatingEffectivenessWrk maintControlOperatingEffectivenessWrk=new MaintControlOperatingEffectivenessWrk();
			BeanUtils.copyProperties(maintControlOperatingEffectiveness, maintControlOperatingEffectivenessWrk);
			maintControlOperatingEffectivenessDao.flushSession();
			maintControlOperatingEffectivenessDao.saveOrUpdate(maintControlOperatingEffectivenessWrk);
			
		}else{
			// get Db object and save into the history
			MaintControlOperatingEffectiveness maintControlOperatingEffectivenessDb=
					getControlOperatingEffByCodeAndType(maintControlOperatingEffectiveness.getLegalEntityCode(), maintControlOperatingEffectiveness.getControlOperatingEffCode(), maintControlOperatingEffectiveness.getControlOperatingEffType(), BankAuditConstant.STATUS_AUTH);
			if(maintControlOperatingEffectivenessDb!=null){
				MaintControlOperatingEffectivenessHst maintControlOperatingEffectivenessHst=new MaintControlOperatingEffectivenessHst();
				BeanUtils.copyProperties(maintControlOperatingEffectivenessDb, maintControlOperatingEffectivenessHst);
				maintControlOperatingEffectivenessDao.save(maintControlOperatingEffectivenessHst);
			}
			//delete the existing object from both the tables  
			maintControlOperatingEffectivenessDao.deleteMaintControlOperatingEffectiveness(maintControlOperatingEffectiveness.getLegalEntityCode(), maintControlOperatingEffectiveness.getControlOperatingEffCode(), maintControlOperatingEffectiveness.getControlOperatingEffType(), BankAuditConstant.STATUS_UNAUTH);
			maintControlOperatingEffectivenessDao.flushSession();
			maintControlOperatingEffectiveness.setCheckerTimestamp(maintControlOperatingEffectiveness.getBusinessdateTimestamp());
			// save again 
			maintControlOperatingEffectivenessDao.saveOrUpdate(maintControlOperatingEffectiveness);
			 
		}
	}
		
		@Override
		public MaintControlOperatingEffectiveness getControlOperatingEffByCodeAndType(Integer legalEntityCode,String controlOperatingEffCode,
				String controlOperatingEffType,String status) {

			Map<String,Object> properties=new HashMap<String,Object>();
			properties.put("controlOperatingEffCode", controlOperatingEffCode);
			properties.put("controlOperatingEffType", controlOperatingEffType);
			properties.put("legalEntityCode", legalEntityCode);
			if(!BankAuditConstant.STATUS_AUTH.equalsIgnoreCase(status)){
				MaintControlOperatingEffectiveness maintControlOperatingEffectiveness=null;
				MaintControlOperatingEffectivenessWrk maintControlOperatingEffectivenessWrk= (MaintControlOperatingEffectivenessWrk)maintControlOperatingEffectivenessDao.getUniqueEntityByMatchingProperties(MaintControlOperatingEffectivenessWrk.class, properties);			
				if(maintControlOperatingEffectivenessWrk!=null){
					maintControlOperatingEffectiveness=new MaintControlOperatingEffectiveness();
					BeanUtils.copyProperties(maintControlOperatingEffectivenessWrk, maintControlOperatingEffectiveness);
				}
				return maintControlOperatingEffectiveness;
			}else {
				return (MaintControlOperatingEffectiveness)maintControlOperatingEffectivenessDao.getUniqueEntityByMatchingProperties(MaintControlOperatingEffectiveness.class, properties);			
			}
		}
			
		@SuppressWarnings("unchecked")
		@Override
		public List<MaintControlOperatingEffectiveness> getControlOperatingEffByType(Integer legalEntityCode,String controlOperatingEffType) {
			Map<String,Object> properties=new HashMap<String,Object>();
			properties.put("legalEntityCode", legalEntityCode);
			properties.put("controlOperatingEffType", controlOperatingEffType);
			properties.put("entityStatus", BankAuditConstant.STATUS_ACTIVE);
			return maintControlOperatingEffectivenessDao.getEntitiesByMatchingProperties(MaintControlOperatingEffectiveness.class, properties);
		}
	
		@Override
		public DataTableResponse getAllMaintControlOperatingEffectiveness(String search, Integer orderColumn, String orderDirection,
				Integer page, Integer size,Integer legalEntityCode) {
			
			return maintControlOperatingEffectivenessDao.getAllMaintControlOperatingEffectiveness( search,  orderColumn,  orderDirection,
					 page,  size,legalEntityCode);
		}

		@Override
		public Boolean isMaintControlOperatingEffectiveness(Integer legalEntityCode, String controlOperatingEffCode, String controlOperatingEffType) {
			return maintControlOperatingEffectivenessDao.isMaintControlOperatingEffectiveness( legalEntityCode,  controlOperatingEffCode,  controlOperatingEffType);
		}
		
		@Override
		public Boolean validateScore(MaintControlOperatingEffectiveness maintControlOperatingEffectiveness) {
			return maintControlOperatingEffectivenessDao.validateScore(maintControlOperatingEffectiveness);
		}
		
		@SuppressWarnings("unchecked")
		@Override
		public List<MaintControlOperatingEffectiveness> getMaintControlOperatingEffectiveness(Integer legalEntityCode) {
			
			Map<String, Object> properties=new HashMap<String,Object>();
			properties.put("legalEntityCode", legalEntityCode);
			properties.put("entityStatus", BankAuditConstant.STATUS_ACTIVE);
			
	        List<MaintControlOperatingEffectiveness> maintControlOperatingEffectivenesses =  maintControlOperatingEffectivenessDao.getEntitiesByMatchingProperties(MaintControlOperatingEffectiveness.class, properties);
	        return maintControlOperatingEffectivenesses;
		}
}
