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.MaintLossEventsDao;
import com.bankaudit.dto.DataTableResponse;
import com.bankaudit.model.MaintLossEvents;
import com.bankaudit.model.MaintLossEventsHst;
import com.bankaudit.model.MaintLossEventsWrk;

@Service("maintLossEventsService")
@Transactional("transactionManager")
public class MaintLossEventsServiceImpl implements MaintLossEventsService {
	@Autowired
	MaintLossEventsDao maintLossEventsDao;
	
	@Autowired
	BusinessDateService businessDateService; 
	
	@Override
	public void createMaintLossEvents(MaintLossEvents maintLossEvents) throws Exception {
		
		if(maintLossEvents.getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_UNAUTH)){
			MaintLossEventsWrk maintLossEventsWrk =new MaintLossEventsWrk(); 
			BeanUtils.copyProperties(maintLossEvents, maintLossEventsWrk);
			maintLossEventsDao.save(maintLossEventsWrk);
			
		}else if (maintLossEvents.getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_AUTH)) {
			maintLossEventsDao.save(maintLossEvents);
		}else{
			throw new Exception("invalid status");
		}
	}
	
	@Override
	public void updateMaintLossEvents(MaintLossEvents maintLossEvents) {
		if(!maintLossEvents.getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_AUTH)){
			if(maintLossEvents.getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_UNAUTH)
					|| maintLossEvents.getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_MOD)){
				maintLossEvents.setMakerTimestamp(businessDateService.getBusinessDateTimestampByLegalEntityCode(maintLossEvents.getLegalEntityCode()));
			}
			// get Db object and save into the history
			MaintLossEvents maintLossEventsDb=
					getLossEventsByCodeAndType(maintLossEvents.getLegalEntityCode(), maintLossEvents.getLossEventCode(), maintLossEvents.getLossEventType(), BankAuditConstant.STATUS_UNAUTH);
			if(maintLossEventsDb!=null){
				maintLossEventsDao.flushSession();
				MaintLossEventsHst maintLossEventsHst=new MaintLossEventsHst();
				BeanUtils.copyProperties(maintLossEventsDb, maintLossEventsHst);
				maintLossEventsDao.save(maintLossEventsHst);
			}
			//delete the existing object rfrom work  
/*			maintCriticalityDao.deleteMaintCriticalityWrk(maintCriticalityWrk.getLegalEntityCode(),maintCriticalityWrk.getUgRoleCode());*/
			// delete the existing work maintEntityAuditSubgroupMapping 
			MaintLossEventsWrk maintLossEventsWrk=new MaintLossEventsWrk();
			BeanUtils.copyProperties(maintLossEvents, maintLossEventsWrk);
			maintLossEventsDao.flushSession();
			maintLossEventsDao.saveOrUpdate(maintLossEventsWrk);
			
		}else{
			// get Db object and save into the history
			MaintLossEvents maintLossEventsDb=
					getLossEventsByCodeAndType(maintLossEvents.getLegalEntityCode(), maintLossEvents.getLossEventCode(), maintLossEvents.getLossEventType(), BankAuditConstant.STATUS_AUTH);
			if(maintLossEventsDb!=null){
				MaintLossEventsHst maintLossEventsHst=new MaintLossEventsHst();
				BeanUtils.copyProperties(maintLossEventsDb, maintLossEventsHst);
				maintLossEventsDao.save(maintLossEventsHst);
			}
			//delete the existing object from both the tables  
			maintLossEventsDao.deleteMaintLossEvents(maintLossEvents.getLegalEntityCode(), maintLossEvents.getLossEventCode(), maintLossEvents.getLossEventType(), BankAuditConstant.STATUS_UNAUTH);
			maintLossEventsDao.flushSession();
			maintLossEvents.setCheckerTimestamp(maintLossEvents.getBusinessdateTimestamp());
			// save again 
			maintLossEventsDao.saveOrUpdate(maintLossEvents);
			 
		}
	}
		
		@Override
		public MaintLossEvents getLossEventsByCodeAndType(Integer legalEntityCode,String lossEventCode,
				String lossEventType,String status) {

			Map<String,Object> properties=new HashMap<String,Object>();
			properties.put("lossEventCode", lossEventCode);
			properties.put("lossEventType", lossEventType);
			properties.put("legalEntityCode", legalEntityCode);
			if(!BankAuditConstant.STATUS_AUTH.equalsIgnoreCase(status)){
				MaintLossEvents maintLossEvents=null;
				MaintLossEventsWrk maintLossEventsWrk= (MaintLossEventsWrk)maintLossEventsDao.getUniqueEntityByMatchingProperties(MaintLossEventsWrk.class, properties);			
				if(maintLossEventsWrk!=null){
					maintLossEvents=new MaintLossEvents();
					BeanUtils.copyProperties(maintLossEventsWrk, maintLossEvents);
				}
				return maintLossEvents;
			}else {
				return (MaintLossEvents)maintLossEventsDao.getUniqueEntityByMatchingProperties(MaintLossEvents.class, properties);			
			}
		}
			
			@SuppressWarnings("unchecked")
			@Override
			public List<MaintLossEvents> getLossEventsByType(Integer legalEntityCode,String lossEventType) {
				Map<String,Object> properties=new HashMap<String,Object>();
				properties.put("legalEntityCode", legalEntityCode);
				properties.put("lossEventType", lossEventType);
				properties.put("entityStatus", BankAuditConstant.STATUS_ACTIVE);
				return maintLossEventsDao.getEntitiesByMatchingProperties(MaintLossEvents.class, properties);
			}
		
			@Override
			public DataTableResponse getAllMaintLossEvents(String search, Integer orderColumn, String orderDirection,
					Integer page, Integer size,Integer legalEntityCode) {
				
				return maintLossEventsDao.getAllMaintLossEvents( search,  orderColumn,  orderDirection,
						 page,  size,legalEntityCode);
			}
	
			@Override
			public Boolean isMaintLossEvents(Integer legalEntityCode, String lossEventCode, String lossEventType) {
				return maintLossEventsDao.isMaintLossEvents( legalEntityCode,  lossEventCode,  lossEventType);
			}
			
	}
