package org.se.lab.service;

import org.apache.log4j.Logger;
import org.se.lab.data.User;
import org.se.lab.data.UserDAO;

import javax.ejb.Stateless;
import javax.inject.Inject;
import java.util.List;

@Stateless
public class UserService {
    private final Logger LOG = Logger.getLogger(UserService.class);

    @Inject
    private UserDAO dao;

	/*
     * API Operations
	 */

    public List<User> findByName(String name) {
        return null;
    }

    public void insert(User user) {
        LOG.debug("insert " + user);

        try {
            dao.insert(user);
        } catch (Exception e) {
            LOG.error("Can't insert user " + user, e);
            throw new ServiceException("Can't insert user " + user);
        }
    }

    public void delete(User user) {
        LOG.debug("delete " + user);

        try {
            dao.delete(user);
        } catch (Exception e) {
            LOG.error("Can't delete user " + user, e);
            throw new ServiceException("Can't delete user " + user);
        }
    }

    public User login(String username, String password) {
        LOG.debug("login for " + username);
        // TODO +hashing

        User user = loadUserByUsername(username);

        if(!user.getPassword().equals(password)){
            LOG.error("Password incorrect for user " + user);
            throw new ServiceException("Password incorrect for user " + user);
        }

        return user;
    }

    private User loadUserByUsername(String username) {
        try{
            return dao.findByUsername(username);
        } catch (Exception e) {
            LOG.error("Can't find user " + username, e);
            throw new ServiceException("Can't find user " + username);
        }
    }

    public void addContact(User user, User contact) {
        LOG.debug("add contact to " + user);
        // TODO
    }

    public void removeContact(User user, User contact) {
        LOG.debug("remove contact from " + user);

        // TODO
    }

    public void update(User user) {
        LOG.debug("update " + user);

        try {
            dao.update(user);
        } catch (Exception e){
            LOG.error("Can't update user " + user, e);
            throw new ServiceException("Can't update user " + user);
        }
    }

    public List<User> findAll() {
        LOG.debug("find all users");

        try {
            List<User> list = dao.findAll();
            return list;
        } catch (Exception e) {
            LOG.error("Can't find all users!", e);
            throw new ServiceException("Can't find all users!");
        }
    }

	/*
	 * TODO check if methods delete(id), findById(id) required
	 */

    public void delete(int id) {
        LOG.info("delete: " + id);

        try {
            User user = findById(id);
            dao.delete(user);
        } catch (Exception e) {
            LOG.error("Can't delete user with ID " + id, e);
            throw new ServiceException("Can't delete user with ID " + id);
        }

    }

    public User findById(int id) {
        LOG.debug("find User with id=" + id);

        try {
            return dao.findById(id);
        } catch (Exception e) {
            LOG.error("Can't find user with id " + id, e);
            throw new ServiceException("Can't find user with id " + id);
        }
    }
}

@@ -2060,32 +2060,188 @@
  TODO%0D%0A    %7D%0D%0A%0D%0A
+    public List%3CUser%3E getAllContactsBy(User user) %7B%0D%0A        LOG.debug(%22get all contacts from %22 + user);%0D%0A%0D%0A        // TODO%0D%0A        return null;%0D%0A    %7D%0D%0A%0D%0A
     public void 

add method getAllContactsBy(user)