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.MaintProbabilityScaleDao;
import com.bankaudit.dto.DataTableResponse;
import com.bankaudit.model.MaintProbabilityScale;
import com.bankaudit.model.MaintProbabilityScaleHst;
import com.bankaudit.model.MaintProbabilityScaleWrk;
import com.bankaudit.util.BankAuditUtil;

@Service("maintProbabilityScaleService")
@Transactional("transactionManager")
public class MaintProbabilityScaleServiceImpl implements MaintProbabilityScaleService {
	
	@Autowired
	MaintProbabilityScaleDao maintProbabilityScaleDao;
	
	@Autowired
	BusinessDateService businessDateService; 
	
	@Override
	public void createMaintProbabilityScale(MaintProbabilityScale maintProbabilityScale)throws Exception {
		
		if(maintProbabilityScale.getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_UNAUTH)){
			MaintProbabilityScaleWrk maintProbabilityScaleWrk =new MaintProbabilityScaleWrk(); 
			BeanUtils.copyProperties(maintProbabilityScale, maintProbabilityScaleWrk);
			maintProbabilityScaleDao.save(maintProbabilityScaleWrk);
			
		}else if (maintProbabilityScale.getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_AUTH)) {
			maintProbabilityScaleDao.save(maintProbabilityScale);
		}else{
			throw new Exception("invalid status");
		}
	}
	
	@Override
	public void updateMaintProbabilityScale(MaintProbabilityScale maintProbabilityScale) {
		if(!maintProbabilityScale.getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_AUTH)){
			if(maintProbabilityScale.getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_UNAUTH)
					|| maintProbabilityScale.getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_MOD)){
				maintProbabilityScale.setMakerTimestamp(businessDateService.getBusinessDateTimestampByLegalEntityCode(maintProbabilityScale.getLegalEntityCode()));
			}
			// get Db object and save into the history
			MaintProbabilityScale maintProbabilityScaleDb=
					getProbabilityByProbabiltyCodeAndProbabilityOfType(maintProbabilityScale.getLegalEntityCode(), maintProbabilityScale.getProbabltyScaleCode(), maintProbabilityScale.getProbabltyScaleType(), BankAuditConstant.STATUS_UNAUTH);
			if(maintProbabilityScaleDb!=null){
				maintProbabilityScaleDao.flushSession();
				MaintProbabilityScaleHst maintProbabilityScaleHst=new MaintProbabilityScaleHst();
				BeanUtils.copyProperties(maintProbabilityScaleDb, maintProbabilityScaleHst);
				maintProbabilityScaleDao.save(maintProbabilityScaleHst);
			}
			//delete the existing object rfrom work  
/*			maintCriticalityDao.deleteMaintCriticalityWrk(maintCriticalityWrk.getLegalEntityCode(),maintCriticalityWrk.getUgRoleCode());*/
			// delete the existing work maintEntityAuditSubgroupMapping 
			MaintProbabilityScaleWrk maintProbabilityScaleWrk=new MaintProbabilityScaleWrk();
			BeanUtils.copyProperties(maintProbabilityScale, maintProbabilityScaleWrk);
			maintProbabilityScaleDao.flushSession();
			maintProbabilityScaleDao.saveOrUpdate(maintProbabilityScaleWrk);
			
		}else{
			// get Db object and save into the history
			MaintProbabilityScale maintProbabilityScaleDb=
					getProbabilityByProbabiltyCodeAndProbabilityOfType(maintProbabilityScale.getLegalEntityCode(), maintProbabilityScale.getProbabltyScaleCode(), maintProbabilityScale.getProbabltyScaleType(), BankAuditConstant.STATUS_AUTH);
			if(maintProbabilityScaleDb!=null){
				MaintProbabilityScaleHst maintProbabilityScaleHst=new MaintProbabilityScaleHst();
				BeanUtils.copyProperties(maintProbabilityScaleDb, maintProbabilityScaleHst);
				maintProbabilityScaleDao.save(maintProbabilityScaleHst);
			}
			//delete the existing object from both the tables  
			maintProbabilityScaleDao.deleteMaintProbabilityScale(maintProbabilityScale.getLegalEntityCode(), maintProbabilityScale.getProbabltyScaleCode(), maintProbabilityScale.getProbabltyScaleType(), BankAuditConstant.STATUS_UNAUTH);
			maintProbabilityScaleDao.flushSession();
			maintProbabilityScale.setCheckerTimestamp(maintProbabilityScale.getBusinessdateTimestamp());
			// save again 
			maintProbabilityScaleDao.saveOrUpdate(maintProbabilityScale);
			 
		}
	}
		
		@Override
		public MaintProbabilityScale getProbabilityByProbabiltyCodeAndProbabilityOfType(Integer legalEntityCode,String probabltyScaleCode,
				String probabltyScaleType,String status) {

			Map<String,Object> properties=new HashMap<String,Object>();
			properties.put("probabltyScaleCode", probabltyScaleCode);
			//properties.put("probabltyScaleType", probabltyScaleType);
			properties.put("legalEntityCode", legalEntityCode);
			if(!BankAuditConstant.STATUS_AUTH.equalsIgnoreCase(status)){
				MaintProbabilityScale maintProbabilityScale=null;
				MaintProbabilityScaleWrk maintProbabilityScaleWrk= (MaintProbabilityScaleWrk)maintProbabilityScaleDao.getUniqueEntityByMatchingProperties(MaintProbabilityScaleWrk.class, properties);			
				if(maintProbabilityScaleWrk!=null){
					maintProbabilityScale=new MaintProbabilityScale();
					BeanUtils.copyProperties(maintProbabilityScaleWrk, maintProbabilityScale);
				}
				return maintProbabilityScale;
			}else {
				return (MaintProbabilityScale)maintProbabilityScaleDao.getUniqueEntityByMatchingProperties(MaintProbabilityScale.class, properties);			
			}
		}
			
		@SuppressWarnings("unchecked")
		@Override
		public List<MaintProbabilityScale> getProbabilityByProbabilityOfType(Integer legalEntityCode,String probabltyScaleType) {
			Map<String,Object> properties=new HashMap<String,Object>();
			properties.put("legalEntityCode", legalEntityCode);
			properties.put("probabltyScaleType", probabltyScaleType);
			properties.put("entityStatus", BankAuditConstant.STATUS_ACTIVE);
			return maintProbabilityScaleDao.getEntitiesByMatchingProperties(MaintProbabilityScale.class, properties);
		}
	
		@Override
		public DataTableResponse getAllMaintProbabilityScale(String search, Integer orderColumn, String orderDirection,
				Integer page, Integer size,Integer legalEntityCode) {
			
			return maintProbabilityScaleDao.getAllMaintProbabilityScale( search,  orderColumn,  orderDirection,
					 page,  size,legalEntityCode);
		}

		@Override
		public Boolean isMaintProbabilityScale(Integer legalEntityCode, String probabilityCode, String probabltyScaleType) {
			return maintProbabilityScaleDao.isMaintProbabilityScale( legalEntityCode,  probabilityCode,  probabltyScaleType);
		}
		
		@Override
		public Boolean validateScore(MaintProbabilityScale maintProbabilityScale) {
			return maintProbabilityScaleDao.validateScore(maintProbabilityScale);
		}
		
		@SuppressWarnings("unchecked")
		@Override
		public List<MaintProbabilityScale> getMaintProbabilityScale(Integer legalEntityCode) {
			
			Map<String, Object> properties=new HashMap<String,Object>();
			properties.put("legalEntityCode", legalEntityCode);
			properties.put("entityStatus", BankAuditConstant.STATUS_ACTIVE);
			
	        List<MaintProbabilityScale> maintProbabilityScales =  maintProbabilityScaleDao.getEntitiesByMatchingProperties(MaintProbabilityScale.class, properties);
	        return maintProbabilityScales;
		}

}
