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.MaintRiskWeightageDao;
import com.bankaudit.dto.DataTableResponse;
import com.bankaudit.model.MaintRiskWeightage;
import com.bankaudit.model.MaintRiskWeightageHst;
import com.bankaudit.model.MaintRiskWeightageWrk;

@Service("maintRiskWeightageService")
@Transactional("transactionManager")
public class MaintRiskWeightageServiceImpl implements MaintRiskWeightageService{
	
	@Autowired
	MaintRiskWeightageDao maintRiskWeightageDao;
	
	@Autowired
	BusinessDateService businessDateService; 
	
	@Override
	public void createMaintRiskWeightage(MaintRiskWeightage maintRiskWeightage)throws Exception {
		
		if(maintRiskWeightage.getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_UNAUTH)){
			MaintRiskWeightageWrk maintRiskWeightageWrk =new MaintRiskWeightageWrk(); 
			BeanUtils.copyProperties(maintRiskWeightage, maintRiskWeightageWrk);
			maintRiskWeightageDao.save(maintRiskWeightageWrk);
			
		}else if (maintRiskWeightage.getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_AUTH)) {
			maintRiskWeightageDao.save(maintRiskWeightage);
		}else{
			throw new Exception("invalid status");
		}
	}
	
	@Override
	public void updateMaintRiskWeightage(MaintRiskWeightage maintRiskWeightage) {
		if(!maintRiskWeightage.getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_AUTH)){
			if(maintRiskWeightage.getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_UNAUTH)
					|| maintRiskWeightage.getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_MOD)){
				maintRiskWeightage.setMakerTimestamp(businessDateService.getBusinessDateTimestampByLegalEntityCode(maintRiskWeightage.getLegalEntityCode()));
			}
			// get Db object and save into the history
			MaintRiskWeightage maintRiskWeightageDb=
					getRiskWeightageByCodeAndType(maintRiskWeightage.getLegalEntityCode(), maintRiskWeightage.getRiskWeightageCode(), maintRiskWeightage.getRiskWeightageType(), BankAuditConstant.STATUS_UNAUTH);
			if(maintRiskWeightageDb!=null){
				maintRiskWeightageDao.flushSession();
				MaintRiskWeightageHst maintRiskWeightageHst=new MaintRiskWeightageHst();
				BeanUtils.copyProperties(maintRiskWeightageDb, maintRiskWeightageHst);
				maintRiskWeightageDao.save(maintRiskWeightageHst);
			}
			//delete the existing object rfrom work  
/*			maintCriticalityDao.deleteMaintCriticalityWrk(maintCriticalityWrk.getLegalEntityCode(),maintCriticalityWrk.getUgRoleCode());*/
			// delete the existing work maintEntityAuditSubgroupMapping 
			MaintRiskWeightageWrk maintRiskWeightageWrk=new MaintRiskWeightageWrk();
			BeanUtils.copyProperties(maintRiskWeightage, maintRiskWeightageWrk);
			maintRiskWeightageDao.flushSession();
			maintRiskWeightageDao.saveOrUpdate(maintRiskWeightageWrk);
			
		}else{
			// get Db object and save into the history
			MaintRiskWeightage maintRiskWeightageDb=
					getRiskWeightageByCodeAndType(maintRiskWeightage.getLegalEntityCode(), maintRiskWeightage.getRiskWeightageCode(), maintRiskWeightage.getRiskWeightageType(), BankAuditConstant.STATUS_AUTH);
			if(maintRiskWeightageDb!=null){
				MaintRiskWeightageHst maintRiskWeightageHst=new MaintRiskWeightageHst();
				BeanUtils.copyProperties(maintRiskWeightageDb, maintRiskWeightageHst);
				maintRiskWeightageDao.save(maintRiskWeightageHst);
			}
			//delete the existing object from both the tables  
			maintRiskWeightageDao.deleteMaintRiskWeightage(maintRiskWeightage.getLegalEntityCode(), maintRiskWeightage.getRiskWeightageCode(), maintRiskWeightage.getRiskWeightageType(), BankAuditConstant.STATUS_UNAUTH);
			maintRiskWeightageDao.flushSession();
			maintRiskWeightage.setCheckerTimestamp(maintRiskWeightage.getBusinessdateTimestamp());
			// save again 
			maintRiskWeightageDao.saveOrUpdate(maintRiskWeightage);
			 
		}
	}
		
		@Override
		public MaintRiskWeightage getRiskWeightageByCodeAndType(Integer legalEntityCode,String riskWeightageCode,
				String riskWeightageType,String status) {

			Map<String,Object> properties=new HashMap<String,Object>();
			properties.put("riskWeightageCode", riskWeightageCode);
			properties.put("riskWeightageType", riskWeightageType);
			properties.put("legalEntityCode", legalEntityCode);
			if(!BankAuditConstant.STATUS_AUTH.equalsIgnoreCase(status)){
				MaintRiskWeightage maintRiskWeightage=null;
				MaintRiskWeightageWrk maintRiskWeightageWrk= (MaintRiskWeightageWrk)maintRiskWeightageDao.getUniqueEntityByMatchingProperties(MaintRiskWeightageWrk.class, properties);			
				if(maintRiskWeightageWrk!=null){
					maintRiskWeightage=new MaintRiskWeightage();
					BeanUtils.copyProperties(maintRiskWeightageWrk, maintRiskWeightage);
				}
				return maintRiskWeightage;
			}else {
				return (MaintRiskWeightage)maintRiskWeightageDao.getUniqueEntityByMatchingProperties(MaintRiskWeightage.class, properties);			
			}
		}
			
		@SuppressWarnings("unchecked")
		@Override
		public List<MaintRiskWeightage> getRiskWeightageByType(Integer legalEntityCode,String riskWeightageType) {
			Map<String,Object> properties=new HashMap<String,Object>();
			properties.put("legalEntityCode", legalEntityCode);
			properties.put("riskWeightageType", riskWeightageType);
			properties.put("entityStatus", BankAuditConstant.STATUS_ACTIVE);
			return maintRiskWeightageDao.getEntitiesByMatchingProperties(MaintRiskWeightage.class, properties);
		}
		
		@Override
		public DataTableResponse getAllMaintRiskWeightage(String search, Integer orderColumn, String orderDirection,
				Integer page, Integer size,Integer legalEntityCode) {
			
			return maintRiskWeightageDao.getAllMaintRiskWeightage( search,  orderColumn,  orderDirection,
					 page,  size,legalEntityCode);
		}
	
		@Override
		public Boolean isMaintRiskWeightage(Integer legalEntityCode, String riskWeightageCode, String riskWeightageType) {
			return maintRiskWeightageDao.isMaintRiskWeightage( legalEntityCode,  riskWeightageCode,  riskWeightageType);
		}
			
		@Override
		public Boolean validateScore(MaintRiskWeightage maintRiskWeightage) {
			return maintRiskWeightageDao.validateScore(maintRiskWeightage);
		}

}
