package com.bankaudit.service;

import java.util.Date;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.jboss.logging.Logger;
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.MaintActionItemsDao;
import com.bankaudit.dto.DataTableResponse;
import com.bankaudit.dto.MaintActionItemDto;
import com.bankaudit.model.MaintActionItem;
import com.bankaudit.model.MaintActionItemWrk;

@Service("maintActionItemsService")
@Transactional("transactionManager")
public class MaintActionItemsServiceImpl implements MaintActionItemsService {

	static final Logger logger = Logger.getLogger(MaintActionItemsService.class);
	
	@Autowired
	MaintActionItemsDao maintActionItemsDao; 
	
	@Autowired
	BusinessDateService businessDateService;
	
	@Override
	public DataTableResponse getAllActionItemList(Integer legalEntityCode, String search, Integer orderColumn, String orderDirection, 
			Integer page, Integer size) { 
		return maintActionItemsDao.getAllActionItemList(legalEntityCode, search, orderColumn, orderDirection, page, size);
	}

	@Override
	public DataTableResponse getActionItemListByauditTypeCode(Integer legalEntityCode, String auditTypeCode, String status, String search, Integer orderColumn, String orderDirection, 
			Integer page, Integer size) { 
		return maintActionItemsDao.getActionItemListByauditTypeCode(legalEntityCode, auditTypeCode, status, search, orderColumn, orderDirection, page, size);
	}	

	@Override
	public List<MaintActionItemWrk> getAllActionItemListByauditTypeCode(Integer legalEntityCode, String auditTypeCode,  String status){
		return maintActionItemsDao.getAllActionItemListByauditTypeCode(legalEntityCode, auditTypeCode, status);
	}
	
	@Override
	public String saveActionItem(MaintActionItemDto maintActionItemDto){
		try{
			List<MaintActionItemWrk> maintActionItemWrkListToSave = maintActionItemDto.getMaintActionItemWrkList();
			
			for (MaintActionItemWrk maintActionItemWrk : maintActionItemWrkListToSave) { 
				if((maintActionItemWrk.getMasterId() == null && BankAuditConstant.STATUS_DEL.equalsIgnoreCase(maintActionItemWrk.getStatus())) 
						|| (maintActionItemWrk.getMasterId() != null && BankAuditConstant.STATUS_REJECT_DEL.equalsIgnoreCase(maintActionItemWrk.getStatus()))){
						maintActionItemsDao.deleteFromMaintActionItemWrk(maintActionItemWrk.getLegalEntityCode(), maintActionItemWrk.getAuditTypeCode(), maintActionItemWrk.getId());
				}
				else if(!BankAuditConstant.STATUS_AUTH.equalsIgnoreCase(maintActionItemWrk.getStatus())){
					if((maintActionItemWrk.getMasterId() != null && maintActionItemWrk.getMasterId() > 0)
							&& (maintActionItemWrk.getId() == null || maintActionItemWrk.getId() == 0 )){				
						MaintActionItemWrk maintActionItemWrkData =	maintActionItemsDao.getModifiedDataFromWrk(maintActionItemWrk.getLegalEntityCode(), maintActionItemWrk.getAuditTypeCode(),
								maintActionItemWrk.getMasterId());
						if(maintActionItemWrkData !=null){
							maintActionItemWrk.setId(maintActionItemWrkData.getId());	 
						} 	
					} 
					maintActionItemsDao.flushSession();
					maintActionItemWrk.setMakerTimestamp(businessDateService.getBusinessDateTimestampByLegalEntityCode(maintActionItemDto.getLegalEntityCode()));
					maintActionItemWrk.setSysdateTimestamp(new Date());
					maintActionItemWrk.setBusinessdateTimestamp(businessDateService.getBusinessDateTimestampByLegalEntityCode(maintActionItemDto.getLegalEntityCode()));
					maintActionItemWrk.setMaker(maintActionItemDto.getMaker());
					maintActionItemsDao.saveOrUpdate(maintActionItemWrk);	
				}				
			} 
			//update maker to latest user
			if(maintActionItemWrkListToSave!=null && maintActionItemWrkListToSave.size()>0){
				maintActionItemsDao.updateMaker(maintActionItemDto.getLegalEntityCode(), maintActionItemDto.getAuditTypeCode(), maintActionItemDto.getMaker(),
						BankAuditConstant.STATUS_UNAUTH);
			}
		}catch (Exception e) {
			logger.error("saveActionItem :: "+e);
			return BankAuditConstant.FAILED;
		}
		return BankAuditConstant.SUCCESS;
	}
	
	/*@Override
	public void deleteActionItemById(Integer legalEntityCode, String auditTypeCode, Integer id, String status, String userId){
		if(BankAuditConstant.STATUS_AUTH.equalsIgnoreCase(status)){
			List<MaintActionItemWrk> maintActionItemWrkList = maintActionItemsDao.getAllActionItemListByauditTypeCode(legalEntityCode, auditTypeCode, 
					BankAuditConstant.STATUS_UNAUTH);
			if(maintActionItemWrkList==null || maintActionItemWrkList.size()==0){
				maintActionItemsDao.copyDataFromMasterToWrk(legalEntityCode, auditTypeCode, userId);
			}
		}
		maintActionItemsDao.deleteActionItemById(legalEntityCode, auditTypeCode, id, status);
	}*/
	
	@Override
	public String authorizeActionItem(MaintActionItemDto maintActionItemDto) {
		 
		Integer legalEntityCode = maintActionItemDto.getLegalEntityCode(); 
		String auditTypeCode =  maintActionItemDto.getAuditTypeCode(); 
		String checker = maintActionItemDto.getChecker();
		 
		try{ 
			List<MaintActionItemWrk> maintActionItemWrkList = maintActionItemDto.getMaintActionItemWrkList();
			String maker = "";
			if(BankAuditConstant.STATUS_AUTH.equalsIgnoreCase(maintActionItemDto.getStatus())){ 
				
				for (MaintActionItemWrk maintActionItemWrk : maintActionItemWrkList) {
					if(!BankAuditConstant.STATUS_DEL.equalsIgnoreCase(maintActionItemWrk.getStatus())){ 
						maintActionItemWrk.setChecker(checker);
						maintActionItemWrk.setCheckerTimestamp(businessDateService.getBusinessDateTimestampByLegalEntityCode(legalEntityCode));
						maintActionItemWrk.setAuthRejRemarks(maintActionItemDto.getAuthRejRemarks());
						maintActionItemWrk.setStatus(BankAuditConstant.STATUS_AUTH);  
						
						MaintActionItem maintActionItem = new MaintActionItem();					
						BeanUtils.copyProperties(maintActionItem, maintActionItemWrk);
						maintActionItem.setId(null);
						maintActionItem.setId(maintActionItemWrk.getMasterId()); 
						
						maintActionItemsDao.saveOrUpdate(maintActionItem);	 						
					}
					else{
						//Soft Delete from master table after authorize the selected records
						if(maintActionItemWrk.getMasterId() != null && maintActionItemWrk.getMasterId() > 0){
							maintActionItemsDao.deleteFromMaintActionItem(legalEntityCode, auditTypeCode, maintActionItemWrk.getMasterId());
						} 
					}
					//Permanent Delete from work table after authorize the selected records
					maker = maintActionItemWrk.getMaker();
					maintActionItemsDao.deleteFromMaintActionItemWrk(legalEntityCode, auditTypeCode, maintActionItemWrk.getId());
				}	 
				
				//update maker to latest user
				if(maintActionItemWrkList!=null && maintActionItemWrkList.size()>0){
					maintActionItemsDao.updateMaker(maintActionItemDto.getLegalEntityCode(), maintActionItemDto.getAuditTypeCode(), maker,
							BankAuditConstant.STATUS_AUTH);
				}				
				return BankAuditConstant.SUCCESS;
			}else if(BankAuditConstant.STATUS_REJ.equalsIgnoreCase(maintActionItemDto.getStatus())){ 
				for (MaintActionItemWrk maintActionItemWrk : maintActionItemWrkList) {
					
					/*if(maintActionItemWrk.getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_DEL)
							|| maintActionItemWrk.getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_MOD)){
						maintActionItemWrk.setStatus(BankAuditConstant.STATUS_REJ+maintActionItemWrk.getStatus());
					}else{*/
					    maintActionItemWrk.setStatus(BankAuditConstant.STATUS_REJ);
					//}					
					maintActionItemsDao.saveOrUpdate(maintActionItemWrk);
				}
				return BankAuditConstant.SUCCESS;
			} 			
		}catch (Exception e) {
			logger.error("authorizeActionItem :: "+e);
			return BankAuditConstant.FAILED;
		}
		return BankAuditConstant.FAILED;
	}
 	 
}
