package ub.scp2.boImpl;

import java.util.List;

import ub.scp2.bo.SubmissionBo;
import ub.scp2.bo.UserBo;
import ub.scp2.dao.SubmissionDao;
import ub.scp2.dao.UserDao;
import ub.scp2.entities.User;

/**
 * UserBoImpl.java -this class implements the UserBo. This is the Business Object of the User entity
 * 
 *
 */
public class UserBoImpl implements UserBo, java.io.Serializable {
	private UserDao userdao;
	private SubmissionDao submissiondao;
	
	public User getUserByUsername(String username){
		return userdao.getUserByUsername(username);
	}

	public void setUserdao(UserDao userdao) {
		this.userdao = userdao;
	}
	public void setSubmissiondao(SubmissionDao submissiondao) {
		this.submissiondao = submissiondao;
	}
	

	public boolean cleanDataBase(User currentUser){
		if(currentUser.getRole().getName().equalsIgnoreCase("ROLE_ADMIN")){
			userdao.cleanDataBase();
			return true;
		}
		return false;
	}
	public boolean deleteUser(User userToDelete, User currentUser) {
    	if (userToDelete.getRole().getName().equals("ROLE_ADMIN")){
    		//An admin cannot delete himself
    		if (!(currentUser.getUsername().equals(userToDelete.getUsername()))){
    			userdao.deleteUser(userToDelete);
    			return true;
    		}
    	}
    	
    	if (userToDelete.getRole().getName().equals("ROLE_TEACHER")){
	    	userdao.deleteUser(userToDelete);
	    	return true;
    	}
    	
    	if (userToDelete.getRole().getName().equals("ROLE_STUDENT")){
    		//Delete all submissions of the student
    		submissiondao.deleteSubmissionsByUser(userToDelete);
    		
    		//Delete the student
    		userdao.deleteUser(userToDelete);
    		return true;
    	}
    	return false;
		
	}

	public List<User> getUsers() {
		return userdao.getUsers();
	}
	
	public List<User> getAdmins() {
		return userdao.getAdmins();
	}
	
	public void saveUser(User user){
		userdao.saveUser(user);
	}
	public boolean usernameExists(String username){
		return userdao.usernameExists(username);
	}

	public void updateUser(User user) {
		userdao.updateUser(user);
		
	}
	
}
