package fr.openwide.core.basicapp.core.business.user.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import fr.openwide.core.basicapp.core.business.history.model.atomic.HistoryEventType;
import fr.openwide.core.basicapp.core.business.history.model.bean.HistoryLogObjectsBean;
import fr.openwide.core.basicapp.core.business.history.service.IHistoryLogService;
import fr.openwide.core.basicapp.core.business.user.dao.IUserDao;
import fr.openwide.core.basicapp.core.business.user.difference.service.IUserDifferenceService;
import fr.openwide.core.basicapp.core.business.user.model.User;
import fr.openwide.core.jpa.exception.SecurityServiceException;
import fr.openwide.core.jpa.exception.ServiceException;
import fr.openwide.core.jpa.security.business.person.service.GenericSimpleUserServiceImpl;
import fr.openwide.core.jpa.security.service.IAuthenticationService;
import fr.openwide.core.jpa.util.HibernateUtils;
import fr.openwide.core.spring.util.StringUtils;

@Service("personService")
public class UserServiceImpl extends GenericSimpleUserServiceImpl<User> implements IUserService {

	@Autowired
	private IUserDao userDao;

	@Autowired
	private IAuthenticationService authenticationService;

	@Autowired
	private IHistoryLogService historyLogService;
	
	@Autowired
	private IUserDifferenceService userDifferenceService;

	@Autowired
	public UserServiceImpl(IUserDao userDao) {
		super(userDao);
		this.userDao = userDao;
	}

	@Override
	public List<User> listByUserName(String userName) {
		return userDao.listByUserName(userName);
	}

	@Override
	public void onSignIn(User user) throws ServiceException, SecurityServiceException {
		historyLogService.log(HistoryEventType.SIGN_IN, HistoryLogObjectsBean.of(user));
	}

	@Override
	public void onSignInFail(User user) throws ServiceException, SecurityServiceException {
		historyLogService.log(HistoryEventType.SIGN_IN_FAIL, HistoryLogObjectsBean.of(user));
	}

	@Override
	public void onCreate(User user, User author) throws ServiceException, SecurityServiceException {
		historyLogService.log(HistoryEventType.CREATE, HistoryLogObjectsBean.of(user));
	}
	
	@Override
	public void setActive(User person, boolean active) throws ServiceException, SecurityServiceException {
		super.setActive(person, active);
		historyLogService.log(active ? HistoryEventType.ENABLE : HistoryEventType.DISABLE, HistoryLogObjectsBean.of(person));
	}
	
	@Override
	protected void updateEntity(User person) throws ServiceException, SecurityServiceException {
		super.updateEntity(person);
		
		historyLogService.logWithDifferences(HistoryEventType.UPDATE, HistoryLogObjectsBean.of(person),
				userDifferenceService.getMinimalDifferenceGenerator(),
				userDifferenceService);
	}

	@Override
	public User getByEmailCaseInsensitive(String email) {
		if (!StringUtils.hasText(StringUtils.lowerCase(email))) {
			return null;
		}
		return userDao.getByEmailCaseInsensitive(email);
	}

	@Override
	public User getAuthenticatedUser() {
		String userName = authenticationService.getUserName();
		if (userName == null) {
			return null;
		}
		
		return HibernateUtils.unwrap(getByUserName(userName));
	}

}

@@ -519,103 +519,8 @@
 ao;%0A
-import fr.openwide.core.basicapp.core.business.user.difference.service.IUserDifferenceService;%0A
 impo
@@ -1252,16 +1252,18 @@
 vice;%0A%09%0A
+//
 %09@Autowi
@@ -1258,32 +1258,34 @@
 %09%0A//%09@Autowired%0A
+//
 %09private IUserDi
@@ -2523,16 +2523,18 @@
 on);%0A%09%09%0A
+//
 %09%09histor
@@ -2623,16 +2623,18 @@
 erson),%0A
+//
 %09%09%09%09user
@@ -2684,16 +2684,18 @@
 ator(),%0A
+//
 %09%09%09%09user

Comment the usage of the difference generator for now