/*
 * 
 */
package com.bankaudit.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

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.RiskScorePercentageDao;
import com.bankaudit.dto.DataTableResponse;
import com.bankaudit.model.RiskScorePercentage;
import com.bankaudit.model.RiskScorePercentageHst;
import com.bankaudit.model.RiskScorePercentageWrk;

/**
 * The Class {@link RiskScorePercentageServiceImpl} Implements the business
 * logic method.
 *
 * @author amit.patel
 * @version 1.0
 */
@Service("riskScorePercentageService")
@Transactional("transactionManager")
public class RiskScorePercentageServiceImpl implements RiskScorePercentageService {

	/**
	 * The risk score percentage dao is autowired and make methods available
	 * from dao layer .
	 */
	@Autowired
	RiskScorePercentageDao riskScorePercentageDao;
	
	/**
	 * The business date service is autowired and make methods available from
	 * dao layer .
	 */
	@Autowired
	BusinessDateService businessDateService;
	@SuppressWarnings("unchecked")
	@Override
	public List<RiskScorePercentage> getRiskScorePercentages() {
		return riskScorePercentageDao.getAll(RiskScorePercentage.class);
	}

	/* (non-Javadoc)
	 * @see com.bankaudit.service.RiskScorePercentageService#getRiskScorePercentagesExceptThis(java.lang.Integer, java.lang.String, java.lang.String)
	 */
	@Override
	public List<RiskScorePercentage> getRiskScorePercentagesExceptThis(Integer legalEntityCode, String auditTypeCode,
			String riskCategoryCode) {

		return riskScorePercentageDao.getRiskScorePercentagesExceptThis( legalEntityCode,  auditTypeCode,
				 riskCategoryCode);
	}


	/* (non-Javadoc)
	 * @see com.bankaudit.service.RiskScorePercentageService#getRiskScorePercentage(java.lang.Integer, java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public RiskScorePercentage getRiskScorePercentage(Integer legalEntityCode, String riskCategoryCode,
			String auditTypeCode,String status) {
		Map<String , Object> properties=new HashMap<String, Object>();
		properties.put("legalEntityCode", legalEntityCode);
		properties.put("riskCategoryCode", riskCategoryCode);
		properties.put("auditTypeCode", auditTypeCode);
		
		
		if(!BankAuditConstant.STATUS_AUTH.equalsIgnoreCase(status)){
			
			RiskScorePercentage riskScorePercentage=null;
			
			RiskScorePercentageWrk riskScorePercentageWrk=(RiskScorePercentageWrk)riskScorePercentageDao.getUniqueEntityByMatchingProperties(RiskScorePercentageWrk.class, properties);

			if(riskScorePercentageWrk!=null){
				riskScorePercentage=new RiskScorePercentage();
				BeanUtils.copyProperties(riskScorePercentageWrk, riskScorePercentage);
			}
			
			return riskScorePercentage;
			
		}else {
			return (RiskScorePercentage)riskScorePercentageDao.getUniqueEntityByMatchingProperties(RiskScorePercentage.class, properties);
		}
		
	}
	
	/* (non-Javadoc)
	 * @see com.bankaudit.service.RiskScorePercentageService#getRiskScorePercentages(java.lang.Integer, java.lang.String, java.lang.Integer, java.lang.String, java.lang.Integer, java.lang.Integer)
	 */
	@Override
	public DataTableResponse getRiskScorePercentages(Integer legalEntityCode, String search, Integer orderColumn,
			String orderDirection, Integer page, Integer size) {
		return riskScorePercentageDao.getRiskScorePercentages( legalEntityCode,  search,  orderColumn,
				 orderDirection,  page,  size);
	}
	
	/* (non-Javadoc)
	 * @see com.bankaudit.service.RiskScorePercentageService#getRiskScorePercentageforUpdate(java.lang.Integer, java.lang.String, java.lang.String)
	 */
	@Override
	public List<RiskScorePercentage> getRiskScorePercentageforUpdate(Integer legalEntityCode, String auditTypeCode,String status){
		Map<String,Object> properties=new HashMap<String,Object>();
		properties.put("auditTypeCode", auditTypeCode);
		properties.put("legalEntityCode", legalEntityCode);
		if(!BankAuditConstant.STATUS_AUTH.equalsIgnoreCase(status)){
			List temp2=new ArrayList<>();
			List<RiskScorePercentageWrk> temp1=riskScorePercentageDao.getEntitiesByMatchingProperties(RiskScorePercentageWrk.class, properties);
			
			List result = temp1.stream()
					.filter(o1 -> !o1.getStatus().equalsIgnoreCase("D"))
					.sorted((o1, o2)->Integer.compare( o1.getMinimunPer().intValue(),o2.getMinimunPer().intValue()))
					.collect(Collectors.toList());
			
			/*if(result!=null){
				for (RiskScorePercentageWrk riskScorePercentageWrk : result) {
					if(!riskScorePercentageWrk.getStatus().equalsIgnoreCase("D")){
						temp2.add(riskScorePercentageWrk);
					}
				}
			}*/
			//return temp2;		
			return result;
		}else {
			List<RiskScorePercentage> temp1= riskScorePercentageDao.getEntitiesByMatchingProperties(RiskScorePercentage.class, properties);	
			
			return temp1.stream()
					.filter(o1 -> !o1.getStatus().equalsIgnoreCase("D"))
					.sorted((o1, o2)->Integer.compare( o1.getMinimunPer().intValue(),o2.getMinimunPer().intValue()))
					.collect(Collectors.toList());
		}
	}

	/* (non-Javadoc)
	 * @see com.bankaudit.service.RiskScorePercentageService#createRiskScorePercentage(java.util.List)
	 */
	@Override
	public void createRiskScorePercentage(List<RiskScorePercentage> riskScorePercentages)throws Exception {
		
		if(riskScorePercentages.get(0).getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_UNAUTH)){
			RiskScorePercentageWrk riskScorePercentageWrk=null;
			for (RiskScorePercentage riskScorePercentage : riskScorePercentages) {
				riskScorePercentageWrk=new RiskScorePercentageWrk();
				BeanUtils.copyProperties(riskScorePercentage,riskScorePercentageWrk);
				riskScorePercentageDao.save(riskScorePercentageWrk);
			}
			
		}else if (riskScorePercentages.get(0).getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_AUTH)) {
			for (RiskScorePercentage riskScorePercentage : riskScorePercentages) {
				riskScorePercentageDao.save(riskScorePercentage);
			}
		}else{
			throw new Exception("invalid status");
		}
	}

	/* (non-Javadoc)
	 * @see com.bankaudit.service.RiskScorePercentageService#updateRiskScorePercentages(java.util.List)
	 */
	@Override
	public void updateRiskScorePercentages(List<RiskScorePercentage> riskScorePercentages) {
		if(!riskScorePercentages.get(0).getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_AUTH)){
			// get Db object and save into the history
			List<RiskScorePercentage> scorePercentagesDb=
					getRiskScorePercentageforUpdate(riskScorePercentages.get(0).getLegalEntityCode(), 
							riskScorePercentages.get(0).getAuditTypeCode(),BankAuditConstant.STATUS_UNAUTH);
			if(scorePercentagesDb!=null && !scorePercentagesDb.isEmpty()){
				RiskScorePercentageHst riskScorePercentageHst=null;
				for (Object riskScorePercentage : scorePercentagesDb) {
					riskScorePercentageHst=new RiskScorePercentageHst();
					BeanUtils.copyProperties(riskScorePercentage,riskScorePercentageHst);
					riskScorePercentageDao.save(riskScorePercentageHst);
				}
				
			}
			// update or save wrk 
			RiskScorePercentageWrk riskScorePercentageWrk=null;
			for (RiskScorePercentage riskScorePercentage : riskScorePercentages) {
				riskScorePercentageWrk=new RiskScorePercentageWrk();
				BeanUtils.copyProperties(riskScorePercentage,riskScorePercentageWrk);
				riskScorePercentageDao.flushSession();
				riskScorePercentageWrk.setMakerTimestamp(businessDateService.getBusinessDateTimestampByLegalEntityCode(riskScorePercentageWrk.getLegalEntityCode()));
				riskScorePercentageDao.saveOrUpdate(riskScorePercentageWrk);
			}
			
		}else{
			// get Db object and save into the history
			List<RiskScorePercentage> scorePercentagesDb=
					getRiskScorePercentageforUpdate(riskScorePercentages.get(0).getLegalEntityCode(), 
							riskScorePercentages.get(0).getAuditTypeCode(),BankAuditConstant.STATUS_AUTH);
			if(scorePercentagesDb!=null && !scorePercentagesDb.isEmpty()){
				RiskScorePercentageHst riskScorePercentageHst=null;
				for (Object riskScorePercentage : scorePercentagesDb) {
					riskScorePercentageHst=new RiskScorePercentageHst();
					BeanUtils.copyProperties(riskScorePercentage,riskScorePercentageHst);
					riskScorePercentageDao.save(riskScorePercentageHst);
				}
				
			}
			
			riskScorePercentageDao.deleteRiskScorePercentages(riskScorePercentages.get(0).getLegalEntityCode(), 
					riskScorePercentages.get(0).getAuditTypeCode(),BankAuditConstant.STATUS_UNAUTH);
			riskScorePercentageDao.deleteRiskScorePercentages(riskScorePercentages.get(0).getLegalEntityCode(), 
					riskScorePercentages.get(0).getAuditTypeCode(),BankAuditConstant.STATUS_AUTH);
			
			
			for (RiskScorePercentage riskScorePercentage : riskScorePercentages) {
				riskScorePercentageDao.flushSession();
				riskScorePercentageDao.save(riskScorePercentage);
			}
			 
		}
	}

	/* (non-Javadoc)
	 * @see com.bankaudit.service.RiskScorePercentageService#isRiskScorePercentage(java.lang.Integer, java.lang.String)
	 */
	@Override
	public Boolean isRiskScorePercentage(Integer legalEntityCode, String auditTypeCode) {
		
		return  riskScorePercentageDao.isRiskScorePercentage( legalEntityCode,  auditTypeCode);
	}
	
	@Override
	public RiskScorePercentage getRiskCategoryOnPercentage(Integer legalEntityCode, String auditTypeCode, Integer riskScore) {
		
		return  riskScorePercentageDao.getRiskCategoryOnPercentage( legalEntityCode,  auditTypeCode, riskScore);
	}

	@Override
	public Boolean isRiskCategoryCode(Integer legalEntityCode, String auditTypeCode, String riskCategoryCode) {
		return riskScorePercentageDao.isRiskCategoryCode( legalEntityCode,  auditTypeCode,  riskCategoryCode);
	}

}
