/*
 * 
 */
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.MaintAuditActivityDao;
import com.bankaudit.dto.DataTableResponse;
import com.bankaudit.model.MaintAuditActivity;
import com.bankaudit.model.MaintAuditActivityWrk;
import com.bankaudit.model.MaintAuditActivityHst;

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

	/**
	 * The maint audit activity dao is autowired and make methods available from
	 * dao layer .
	 */
	@Autowired
	MaintAuditActivityDao maintAuditActivityDao;
	
	/**
	 * The business date service is autowired and make methods available from
	 * dao layer .
	 */
	@Autowired
	BusinessDateService businessDateService;
	
	/**
	 * The sequence appender service is autowired and make methods available
	 * from dao layer .
	 */
	@Autowired
	SequenceAppenderService sequenceAppenderService;


	/* (non-Javadoc)
	 * @see com.bankaudit.service.MaintAuditActivityService#createMaintAuditActivity(com.bankaudit.model.MaintAuditActivity)
	 */
	@Override
	public void createMaintAuditActivity(MaintAuditActivity maintAuditActivity) throws Exception {
		
	if(maintAuditActivity.getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_UNAUTH)){
		
		MaintAuditActivityWrk maintAuditActivityWrk =new MaintAuditActivityWrk();
		BeanUtils.copyProperties(maintAuditActivity, maintAuditActivityWrk);
		maintAuditActivityDao.save(maintAuditActivityWrk);
		
	}else if (maintAuditActivity.getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_AUTH)) {
		maintAuditActivityDao.save(maintAuditActivity);
	}else {
		throw new Exception("invalid status");
	}
}


	/* (non-Javadoc)
	 * @see com.bankaudit.service.MaintAuditActivityService#updateMaintAuditActivity(com.bankaudit.model.MaintAuditActivity)
	 */
	@Override
	public void updateMaintAuditActivity(MaintAuditActivity maintAuditActivity) {

		
		if(!maintAuditActivity.getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_AUTH)){

			if(maintAuditActivity.getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_UNAUTH)
					|| maintAuditActivity.getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_MOD)){
				maintAuditActivity.setMakerTimestamp(businessDateService.getBusinessDateTimestampByLegalEntityCode(maintAuditActivity.getLegalEntityCode()));
			}

			// get Db object and save into the history
			MaintAuditActivity maintAuditActivityDb=getMaintAuditActivity(maintAuditActivity.getLegalEntityCode(), maintAuditActivity.getActivityId(), BankAuditConstant.STATUS_UNAUTH);
			if(maintAuditActivityDb!=null){
				MaintAuditActivityHst MaintAuditActivityHst=new MaintAuditActivityHst();
				BeanUtils.copyProperties(maintAuditActivityDb, MaintAuditActivityHst);
				maintAuditActivityDao.save(MaintAuditActivityHst);
			}
			
			MaintAuditActivityWrk maintAuditActivityWrk=new MaintAuditActivityWrk();
			BeanUtils.copyProperties(maintAuditActivity, maintAuditActivityWrk);
			maintAuditActivityDao.flushSession();
			maintAuditActivityDao.saveOrUpdate(maintAuditActivityWrk);
			
		}else{

			// get Db object and save into the history
			MaintAuditActivity maintAuditActivityDb=getMaintAuditActivity(maintAuditActivity.getLegalEntityCode(), maintAuditActivity.getActivityId(), BankAuditConstant.STATUS_AUTH);
			if(maintAuditActivityDb!=null){
				maintAuditActivityDb.setEntityStatus(BankAuditConstant.STATUS_EXPIRE);
				maintAuditActivityDao.update(maintAuditActivityDb);
				maintAuditActivityDao.flushSession();
			}

			// delete from work
			maintAuditActivityDao.deleteMaintAuditActivity(maintAuditActivity.getLegalEntityCode(),maintAuditActivity.getActivityId(),BankAuditConstant.STATUS_UNAUTH);
			
			// save again 
			maintAuditActivityDao.flushSession();
			maintAuditActivity.setActivityId(sequenceAppenderService.getAutoSequenceId());
			maintAuditActivityDao.save(maintAuditActivity);
			
		}
	
	}


	/* (non-Javadoc)
	 * @see com.bankaudit.service.MaintAuditActivityService#getMaintAuditActivity(java.lang.Integer, java.lang.String, java.lang.String)
	 */
	@Override
	public MaintAuditActivity getMaintAuditActivity(Integer legalEntityCode, String activityId,String status) {
		Map<String,Object> properties=new HashMap<String, Object>();
		properties.put("legalEntityCode", legalEntityCode);
		properties.put("activityId", activityId);
		
		if(!BankAuditConstant.STATUS_AUTH.equalsIgnoreCase(status)){
			MaintAuditActivity maintAuditActivity=null;
			MaintAuditActivityWrk maintAuditActivityWrk =(MaintAuditActivityWrk) maintAuditActivityDao.getUniqueEntityByMatchingProperties(MaintAuditActivityWrk.class, properties);
			if(maintAuditActivityWrk!=null){
				maintAuditActivity=new MaintAuditActivity();
				BeanUtils.copyProperties(maintAuditActivityWrk, maintAuditActivity);
			}
			return maintAuditActivity;
		}else {
			return (MaintAuditActivity) maintAuditActivityDao.getUniqueEntityByMatchingProperties(MaintAuditActivity.class, properties);
		}
		
		
	}


	/* (non-Javadoc)
	 * @see com.bankaudit.service.MaintAuditActivityService#getMaintAuditActivities(java.lang.Integer, java.lang.String, java.lang.Integer, java.lang.String, java.lang.Integer, java.lang.Integer)
	 */
	@Override
	public DataTableResponse getMaintAuditActivities(Integer legalEntityCode, String search, Integer orderColumn,
			String orderDirection, Integer page, Integer size) {
		return maintAuditActivityDao.getMaintAuditActivities( legalEntityCode ,  search,  orderColumn,
				 orderDirection,  page,  size);
	}


	/* (non-Javadoc)
	 * @see com.bankaudit.service.MaintAuditActivityService#getMaintAuditActivity(java.lang.Integer)
	 */
	@Override
	public List<MaintAuditActivity> getMaintAuditActivity(Integer legalEntityCode) {
		Map<String,Object> properties=new HashMap<String, Object>();
		properties.put("legalEntityCode", legalEntityCode);
		properties.put("entityStatus", "A");
		return maintAuditActivityDao.getEntitiesByMatchingProperties(MaintAuditActivity.class, properties);
	}


	/* (non-Javadoc)
	 * @see com.bankaudit.service.MaintAuditActivityService#isMaintAuditActivity(java.lang.Integer, java.lang.String)
	 */
	@Override
	public Boolean isMaintAuditActivity(Integer legalEntityCode, String activityCode) {
		
		return maintAuditActivityDao.isMaintAuditActivity( legalEntityCode,  activityCode);
	}

	@Override
	public void deleteActivityByID(Integer legalEntityCode, String activityId, String userId) {
		maintAuditActivityDao.deleteMaintAuditActivity(legalEntityCode, activityId, BankAuditConstant.STATUS_REJ);
	}

}
