package eu.dnetlib.functionality.rating.app;

import java.util.List;
import org.apache.log4j.Logger;
import eu.dnetlib.api.DriverServiceException;
import eu.dnetlib.api.functionality.RatingService;
import eu.dnetlib.api.functionality.RatingServiceException;
import eu.dnetlib.domain.ServiceIdentity;
import eu.dnetlib.domain.enabling.Notification;
import eu.dnetlib.domain.functionality.Rating;
import eu.dnetlib.functionality.rating.dao.RatingDAO;
import eu.dnetlib.functionality.rating.dao.RatingDAOException;
import gr.uoa.di.driver.app.DriverServiceImpl;

public class RatingServiceImpl extends DriverServiceImpl implements RatingService {
	private RatingDAO ratingDAO;
	private Logger logger = Logger.getLogger(this.getClass());

	/**
	 * Set the rating DAO.
	 * @param ratingDAO the new rating DAO to use
	 */
	public void setRatingDAO(RatingDAO ratingDAO) {
		this.ratingDAO = ratingDAO;
	}
	
	@Override
	public List<Rating> getTopDocuments(int limit) throws RatingServiceException {
		try {
			List<Rating> result = ratingDAO.getTopDocuments(limit);
			logger.info("top " + limit + " documents retrieved successfully");
			return result;
		} catch (RatingDAOException e) {
			logger.error("error retrieving top " + limit + " documents");
			throw new RatingServiceException(e);
		}
	}

	@Override
	public List<Rating> getTopRatings(int limit) throws RatingServiceException {
		try {
			List<Rating> result = ratingDAO.getTopRatings(limit);
			logger.info("top " + limit + " ratings retrieved successfully");
			return result;
		} catch (RatingDAOException e) {
			logger.error("error retrieving top " + limit + " ratings");
			throw new RatingServiceException(e);
		}
	}

	@Override
	public void rate(String userId, String documentId, float score) throws RatingServiceException {
		try {
			ratingDAO.rate(new Rating(userId, documentId, score));
			logger.info("document " + documentId + " rated successfully with score " + score + " by user " + userId);
		} catch (RatingDAOException e) {
			logger.error("error rating document " + documentId + " with score " + score + " for user " + userId);
			throw new RatingServiceException(e);
		}
	}

	@Override
	public List<Rating> searchRatingsByDocument(String documentId) throws RatingServiceException {
		try {
			List<Rating> result = ratingDAO.searchByDocument(documentId);
			logger.info("ratings of document " + documentId + " retrieved successfully");
			return result;
		} catch (RatingDAOException e) {
			logger.error("error retrieving ratings of document " + documentId);
			throw new RatingServiceException(e);
		}
	}

	@Override
	public List<Rating> searchRatingsByUser(String userId) throws RatingServiceException {
		try {
			List<Rating> result = ratingDAO.searchByUser(userId);
			logger.info("ratings of user " + userId + " retrieved successfully");
			return result;
		} catch (RatingDAOException e) {
			logger.error("error retrieving ratings of user " + userId);
			throw new RatingServiceException(e);
		}
	}

	@Override
	public ServiceIdentity identify() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void notify(Notification notification) throws DriverServiceException {
		// TODO Auto-generated method stub
		
	}	
}
