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.MaintSeverityScaleDao;
import com.bankaudit.dto.DataTableResponse;
import com.bankaudit.model.MaintSeverityScale;
import com.bankaudit.model.MaintSeverityScaleHst;
import com.bankaudit.model.MaintSeverityScaleWrk;

@Service("maintSeverityScaleService")
@Transactional("transactionManager")
public class MaintSeverityScaleServiceImpl implements MaintSeverityScaleService{
	
	@Autowired
	MaintSeverityScaleDao maintSeverityScaleDao;
	
	@Autowired
	BusinessDateService businessDateService; 
	
	@Override
	public void createMaintSeverityScale(MaintSeverityScale maintSeverityScale)throws Exception {
		
		if(maintSeverityScale.getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_UNAUTH)){
			MaintSeverityScaleWrk maintSeverityScaleWrk =new MaintSeverityScaleWrk(); 
			BeanUtils.copyProperties(maintSeverityScale, maintSeverityScaleWrk);
			maintSeverityScaleDao.save(maintSeverityScaleWrk);
			
		}else if (maintSeverityScale.getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_AUTH)) {
			maintSeverityScaleDao.save(maintSeverityScale);
		}else{
			throw new Exception("invalid status");
		}
	}
	
	@Override
	public void updateMaintSeverityScale(MaintSeverityScale maintSeverityScale) {
		if(!maintSeverityScale.getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_AUTH)){
			if(maintSeverityScale.getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_UNAUTH)
					|| maintSeverityScale.getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_MOD)){
				maintSeverityScale.setMakerTimestamp(businessDateService.getBusinessDateTimestampByLegalEntityCode(maintSeverityScale.getLegalEntityCode()));
			}
			// get Db object and save into the history
			MaintSeverityScale maintSeverityScaleDb=
					getSeverityBySeverityCodeAndSeverityOfType(maintSeverityScale.getLegalEntityCode(), maintSeverityScale.getSeverityScaleCode(), maintSeverityScale.getSeverityScaleType(), BankAuditConstant.STATUS_UNAUTH);
			if(maintSeverityScaleDb!=null){
				maintSeverityScaleDao.flushSession();
				MaintSeverityScaleHst maintSeverityScaleHst=new MaintSeverityScaleHst();
				BeanUtils.copyProperties(maintSeverityScaleDb, maintSeverityScaleHst);
				maintSeverityScaleDao.save(maintSeverityScaleHst);
			}
			//delete the existing object rfrom work  
/*			maintCriticalityDao.deleteMaintCriticalityWrk(maintCriticalityWrk.getLegalEntityCode(),maintCriticalityWrk.getUgRoleCode());*/
			// delete the existing work maintEntityAuditSubgroupMapping 
			MaintSeverityScaleWrk maintSeverityScaleWrk=new MaintSeverityScaleWrk();
			BeanUtils.copyProperties(maintSeverityScale, maintSeverityScaleWrk);
			maintSeverityScaleDao.flushSession();
			maintSeverityScaleDao.saveOrUpdate(maintSeverityScaleWrk);
			
		}else{
			// get Db object and save into the history
			MaintSeverityScale maintSeverityScaleDb=
					getSeverityBySeverityCodeAndSeverityOfType(maintSeverityScale.getLegalEntityCode(), maintSeverityScale.getSeverityScaleCode(), maintSeverityScale.getSeverityScaleType(), BankAuditConstant.STATUS_AUTH);
			if(maintSeverityScaleDb!=null){
				MaintSeverityScaleHst maintSeverityScaleHst=new MaintSeverityScaleHst();
				BeanUtils.copyProperties(maintSeverityScaleDb, maintSeverityScaleHst);
				maintSeverityScaleDao.save(maintSeverityScaleHst);
			}
			//delete the existing object from both the tables  
			maintSeverityScaleDao.deleteMaintSeverityScale(maintSeverityScale.getLegalEntityCode(), maintSeverityScale.getSeverityScaleCode(), maintSeverityScale.getSeverityScaleType(), BankAuditConstant.STATUS_UNAUTH);
			maintSeverityScaleDao.flushSession();
			maintSeverityScale.setCheckerTimestamp(maintSeverityScale.getBusinessdateTimestamp());
			// save again 
			maintSeverityScaleDao.saveOrUpdate(maintSeverityScale);
			 
		}
	}
		
		@Override
		public MaintSeverityScale getSeverityBySeverityCodeAndSeverityOfType(Integer legalEntityCode,String severityScaleCode,
				String severityScaleType,String status) {

			Map<String,Object> properties=new HashMap<String,Object>();
			properties.put("severityScaleCode", severityScaleCode);
			//properties.put("severityScaleType", severityScaleType);
			properties.put("legalEntityCode", legalEntityCode);
			if(!BankAuditConstant.STATUS_AUTH.equalsIgnoreCase(status)){
				MaintSeverityScale maintSeverityScale=null;
				MaintSeverityScaleWrk maintSeverityScaleWrk= (MaintSeverityScaleWrk)maintSeverityScaleDao.getUniqueEntityByMatchingProperties(MaintSeverityScaleWrk.class, properties);			
				if(maintSeverityScaleWrk!=null){
					maintSeverityScale=new MaintSeverityScale();
					BeanUtils.copyProperties(maintSeverityScaleWrk, maintSeverityScale);
				}
				return maintSeverityScale;
			}else {
				return (MaintSeverityScale)maintSeverityScaleDao.getUniqueEntityByMatchingProperties(MaintSeverityScale.class, properties);			
			}
		}
			
		@SuppressWarnings("unchecked")
		@Override
		public List<MaintSeverityScale> getSeverityBySeverityOfType(Integer legalEntityCode,String severityScaleType) {
			Map<String,Object> properties=new HashMap<String,Object>();
			properties.put("legalEntityCode", legalEntityCode);
			properties.put("severityScaleType", severityScaleType);
			properties.put("entityStatus", BankAuditConstant.STATUS_ACTIVE);
			return maintSeverityScaleDao.getEntitiesByMatchingProperties(MaintSeverityScale.class, properties);
		}
	
		@Override
		public DataTableResponse getAllMaintSeverityScale(String search, Integer orderColumn, String orderDirection,
				Integer page, Integer size,Integer legalEntityCode) {
			
			return maintSeverityScaleDao.getAllMaintSeverityScale( search,  orderColumn,  orderDirection,
					 page,  size,legalEntityCode);
		}

		@Override
		public Boolean isMaintSeverityScale(Integer legalEntityCode, String severityScaleCode, String severityScaleType) {
			return maintSeverityScaleDao.isMaintSeverityScale( legalEntityCode,  severityScaleCode,  severityScaleType);
		}
		
		@Override
		public Boolean validateScore(MaintSeverityScale maintSeverityScale) {
			return maintSeverityScaleDao.validateScore(maintSeverityScale);
		}
		
		@SuppressWarnings("unchecked")
		@Override
		public List<MaintSeverityScale> getMaintProbabilityScale(Integer legalEntityCode) {
			
			Map<String, Object> properties=new HashMap<String,Object>();
			properties.put("legalEntityCode", legalEntityCode);
			properties.put("entityStatus", BankAuditConstant.STATUS_ACTIVE);
			
	        List<MaintSeverityScale> maintSeverityScales =  maintSeverityScaleDao.getEntitiesByMatchingProperties(MaintSeverityScale.class, properties);
	        return maintSeverityScales;
		}

}
