package com.commafeed.backend.services;

import java.util.Calendar;
import java.util.List;

import javax.ejb.Stateless;
import javax.inject.Inject;

import org.hibernate.Hibernate;

import com.commafeed.backend.MetricsBean;
import com.commafeed.backend.dao.FeedEntryDAO;
import com.commafeed.backend.dao.FeedEntryStatusDAO;
import com.commafeed.backend.dao.FeedSubscriptionDAO;
import com.commafeed.backend.feeds.FeedUtils;
import com.commafeed.backend.model.Feed;
import com.commafeed.backend.model.FeedEntry;
import com.commafeed.backend.model.FeedEntryContent;
import com.commafeed.backend.model.FeedEntryStatus;
import com.commafeed.backend.model.FeedSubscription;
import com.google.common.collect.Lists;

@Stateless
public class FeedUpdateService {

	@Inject
	FeedSubscriptionDAO feedSubscriptionDAO;

	@Inject
	FeedEntryDAO feedEntryDAO;

	@Inject
	FeedEntryStatusDAO feedEntryStatusDAO;

	@Inject
	MetricsBean metricsBean;

	public void updateEntry(Feed feed, FeedEntry entry,
			List<FeedSubscription> subscriptions) {

		FeedEntry foundEntry = FeedUtils.findEntry(
				feedEntryDAO.findByGuid(entry.getGuid()), entry);

		FeedEntry update = null;
		if (foundEntry == null) {
			FeedEntryContent content = entry.getContent();
			content.setTitle(FeedUtils.truncate(
					FeedUtils.handleContent(content.getTitle(), feed.getLink()),
					2048));
			content.setContent(FeedUtils.handleContent(content.getContent(),
					feed.getLink()));

			entry.setInserted(Calendar.getInstance().getTime());
			entry.getFeeds().add(feed);

			update = entry;
		} else {
			Hibernate.initialize(foundEntry.getFeeds());
			if (FeedUtils.findFeed(foundEntry.getFeeds(), feed) == null) {
				foundEntry.getFeeds().add(feed);
				update = foundEntry;
			}
		}

		if (update != null) {
			List<FeedEntryStatus> statusUpdateList = Lists.newArrayList();
			for (FeedSubscription sub : subscriptions) {
				FeedEntryStatus status = new FeedEntryStatus();
				status.setEntry(update);
				status.setSubscription(sub);
				statusUpdateList.add(status);
			}
			feedEntryDAO.saveOrUpdate(update);
			feedEntryStatusDAO.saveOrUpdate(statusUpdateList);
			metricsBean.entryUpdated(statusUpdateList.size());
		}
	}

}
