package com.bankaudit.service;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jboss.logging.Logger;
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.UserLeavePlannerDao;
import com.bankaudit.dto.DataTableResponse;
import com.bankaudit.model.UserLeavePlanner;
import com.bankaudit.model.UserLeavePlannerHst;
import com.bankaudit.model.UserLeavePlannerWrk;
import com.bankaudit.util.ServiceStatus;


@Service("userLeavePlannerService")
@Transactional("transactionManager")
public class UserLeavePlannerServiceImpl implements UserLeavePlannerService {
	
	/**
	 * The User Leave Planner dao is autowired and make methods available from dao
	 * layer .
	 */
	@Autowired
	UserLeavePlannerDao userLeavePlannerDao;
	
	/**
	 * The business date service is autowired and make methods available from
	 * dao layer .
	 */
	@Autowired
	BusinessDateService businessDateService;
	
	static final Logger logger = Logger.getLogger(UserLeavePlannerServiceImpl.class);
	
	@Override
	public void createUserLeave(UserLeavePlanner userLeavePlanner) throws Exception{
		
		if(userLeavePlanner.getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_UNAUTH)){
			UserLeavePlannerWrk userLeavePlannerWrk=new UserLeavePlannerWrk(); 
			BeanUtils.copyProperties(userLeavePlanner, userLeavePlannerWrk);
			userLeavePlannerDao.save(userLeavePlannerWrk);
			
		}else if (userLeavePlanner.getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_AUTH)) {
			userLeavePlannerDao.save(userLeavePlanner);
		}else{
			throw new Exception("invalid status");
		}
	
	}

	/* (non-Javadoc)
	 * @see com.bankaudit.service.UserLeavePlannerService#updateUserLeave(com.bankaudit.model.UserLeavePlanner)
	 */
	@Override
	public void updateUserLeave(UserLeavePlanner userLeavePlanner) {

		if(!userLeavePlanner.getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_AUTH)){ // Section for Update, Reject
			
			// get Db object and save into the history
			UserLeavePlanner userLeavePlannerDtls=getUserLeaveById(userLeavePlanner.getLegalEntityCode(),userLeavePlanner.getId(), userLeavePlanner.getPrevStatus());
			if(userLeavePlannerDtls!=null){
				userLeavePlannerDao.flushSession();
				UserLeavePlannerHst userLeavePlannerHst=new UserLeavePlannerHst();
				BeanUtils.copyProperties(userLeavePlannerDtls, userLeavePlannerHst);
				userLeavePlannerDao.save(userLeavePlannerHst);
			}
			
			userLeavePlanner.setBusinessdateTimestamp(businessDateService.getBusinessDateTimestampByLegalEntityCode(userLeavePlanner.getLegalEntityCode()));
			userLeavePlanner.setMakerTimestamp(userLeavePlanner.getBusinessdateTimestamp());
			userLeavePlanner.setSysdateTimestamp(new Timestamp(System.currentTimeMillis()));
			
			UserLeavePlannerWrk userLeavePlannerWrk=new UserLeavePlannerWrk();
			BeanUtils.copyProperties(userLeavePlanner, userLeavePlannerWrk);
			userLeavePlannerDao.flushSession();
			userLeavePlannerDao.saveOrUpdate(userLeavePlannerWrk);
			
		}else{
			// get Db object and save into the history
			UserLeavePlanner userLeavePlannerDtls=getUserLeaveById(userLeavePlanner.getLegalEntityCode(),userLeavePlanner.getId(), userLeavePlanner.getPrevStatus());
			if(userLeavePlannerDtls!=null){
				UserLeavePlannerHst userLeavePlannerHst=new UserLeavePlannerHst();
				BeanUtils.copyProperties(userLeavePlannerDtls, userLeavePlannerHst);
				userLeavePlannerDao.save(userLeavePlannerHst);
			}
			//delete the existing object from both the tables  
			userLeavePlannerDao.deleteUserLeavePlanner(userLeavePlanner.getLegalEntityCode(),userLeavePlanner.getId(), BankAuditConstant.STATUS_UNAUTH); // deleting the Record from Work table
			userLeavePlannerDao.flushSession();
			userLeavePlanner.setCheckerTimestamp(userLeavePlanner.getBusinessdateTimestamp());
			// save again 
			//userLeavePlannerDao.saveOrUpdate(userLeavePlanner); // Insert or Update to the Mst table, based on the whether it is Authorizing for 1st time or there on.
			logger.info("AUTH userLeavePlanner .."+userLeavePlanner);
			userLeavePlannerDao.saveOrUpdate(userLeavePlanner);
		}
		
	}

	/* (non-Javadoc)
	 * @see com.bankaudit.service.UserLeavePlannerService#getAllUserLeaves(java.lang.Integer, java.lang.String, java.lang.Integer, java.lang.String, java.lang.Integer, java.lang.Integer)
	 */
	@Override
	public DataTableResponse getAllUserLeaves(Integer legalEntityCodeStr,String search, Integer orderColumn, String orderDirection, Integer page,
			Integer size) {
		return userLeavePlannerDao.getAllUserLeaves( legalEntityCodeStr, search,  orderColumn,  orderDirection,  page, size);
	}

	/* (non-Javadoc)
	 * @see com.bankaudit.service.UserLeavePlannerService#getUserLeave(java.lang.Integer, java.lang.String)
	 */
	@Override
	public UserLeavePlanner getUserLeaveById(Integer legalEntityCodeStr,Integer id,  String status) {
		Map<String,Object> properties=new HashMap<String, Object >();
		properties.put("legalEntityCode",legalEntityCodeStr);
		properties.put("id",id);
				
		if(!BankAuditConstant.STATUS_AUTH.equalsIgnoreCase(status)){
		
			UserLeavePlanner userLeavePlanner=null;
			UserLeavePlannerWrk userLeavePlannerWrk=(UserLeavePlannerWrk)userLeavePlannerDao.getUniqueEntityByMatchingProperties(UserLeavePlannerWrk.class, properties);	
			if(userLeavePlannerWrk!=null){
				userLeavePlanner=new UserLeavePlanner();
				BeanUtils.copyProperties(userLeavePlannerWrk, userLeavePlanner);
			}
			return userLeavePlanner;
			
		}else {
			return (UserLeavePlanner)userLeavePlannerDao.getUniqueEntityByMatchingProperties(UserLeavePlanner.class, properties);		
		}
		
	}
	
	/* (non-Javadoc)
	 * @see com.bankaudit.service.UserLeavePlannerService#getUserLeave(java.lang.Integer, java.lang.String)
	 */
	@Override
	public List<UserLeavePlanner> getByUserIdLeaveType(Integer legalEntityCodeStr,String userId,String leaveType){
		return userLeavePlannerDao.getByUserIdLeaveType( legalEntityCodeStr, userId, leaveType);		
	}
	
	/* (non-Javadoc)
	 * @see com.bankaudit.service.MaintHolidayService#validateHoliday(com.bankaudit.model.MaintHoliday)
	 */
	@Override
	public ServiceStatus validateUserLeaves(UserLeavePlanner userLeavePlanner) {
		return (ServiceStatus)userLeavePlannerDao.validateUserLeaves(userLeavePlanner);
	}
	
	@Override
	public void deleteUserLevaePlanByID(Integer legalEntityCode, Integer id, String userId) {
		userLeavePlannerDao.deleteUserLeavePlanner(legalEntityCode, id, BankAuditConstant.STATUS_REJ);
	}
}
