package com.commafeed.backend;

import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import java.util.concurrent.Future;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.commafeed.backend.dao.FeedCategoryDAO;
import com.commafeed.backend.dao.FeedDAO;
import com.commafeed.backend.dao.FeedSubscriptionDAO;
import com.commafeed.backend.dao.UserDAO;
import com.commafeed.backend.feeds.FeedRefreshWorker;
import com.commafeed.backend.model.ApplicationSettings;
import com.commafeed.backend.model.Feed;
import com.commafeed.backend.model.FeedCategory;
import com.commafeed.backend.model.FeedSubscription;
import com.commafeed.backend.model.User;
import com.commafeed.backend.model.UserRole.Role;
import com.commafeed.backend.services.ApplicationSettingsService;
import com.commafeed.backend.services.UserService;
import com.google.api.client.util.Lists;

@Startup
@Singleton
public class StartupBean {

	private static Logger log = LoggerFactory.getLogger(StartupBean.class);
	public static final String ADMIN_NAME = "admin";

	@Inject
	FeedDAO feedDAO;

	@Inject
	FeedCategoryDAO feedCategoryDAO;

	@Inject
	FeedSubscriptionDAO feedSubscriptionDAO;

	@Inject
	UserDAO userDAO;

	@Inject
	UserService userService;

	@Inject
	ApplicationSettingsService applicationSettingsService;

	@Inject
	FeedRefreshWorker worker;

	private List<Future<Void>> threads = Lists.newArrayList();

	private long startupTime;

	@PostConstruct
	private void init() {
		startupTime = Calendar.getInstance().getTimeInMillis();
		if (userDAO.getCount() == 0) {
			initialData();
		}

		ApplicationSettings settings = applicationSettingsService.get();
		for (int i = 0; i < settings.getBackgroundThreads(); i++) {
			Future<Void> thread = worker.start();
			threads.add(thread);
		}

	}

	private void initialData() {
		log.info("Populating database with default values");

		applicationSettingsService.save(new ApplicationSettings());

		User user = userService.register(ADMIN_NAME, "admin",
				Arrays.asList(Role.ADMIN, Role.USER));
		userService.register("test", "test", Arrays.asList(Role.USER));

		Feed dilbert = new Feed("http://feed.dilbert.com/dilbert/daily_strip");
		feedDAO.save(dilbert);

		Feed engadget = new Feed("http://www.engadget.com/rss.xml");
		feedDAO.save(engadget);

		Feed frandroid = new Feed("http://feeds.feedburner.com/frandroid");
		feedDAO.save(frandroid);

		FeedCategory newsCategory = new FeedCategory();
		newsCategory.setName("News");
		newsCategory.setUser(user);
		feedCategoryDAO.save(newsCategory);

		FeedCategory comicsCategory = new FeedCategory();
		comicsCategory.setName("Comics");
		comicsCategory.setUser(user);
		comicsCategory.setParent(newsCategory);
		feedCategoryDAO.save(comicsCategory);

		FeedCategory techCategory = new FeedCategory();
		techCategory.setName("Tech");
		techCategory.setUser(user);
		techCategory.setParent(newsCategory);
		feedCategoryDAO.save(techCategory);

		FeedSubscription sub = new FeedSubscription();
		sub.setCategory(comicsCategory);
		sub.setFeed(dilbert);
		sub.setTitle("Dilbert - Strips");
		sub.setUser(user);
		feedSubscriptionDAO.save(sub);

		FeedSubscription sub2 = new FeedSubscription();
		sub2.setCategory(techCategory);
		sub2.setFeed(engadget);
		sub2.setTitle("Engadget");
		sub2.setUser(user);
		feedSubscriptionDAO.save(sub2);

		FeedSubscription sub3 = new FeedSubscription();
		sub3.setFeed(frandroid);
		sub3.setTitle("Frandroid");
		sub3.setUser(user);
		feedSubscriptionDAO.save(sub3);
	}

	public long getStartupTime() {
		return startupTime;
	}

	@PreDestroy
	private void shutdown() {
		for (Future<Void> future : threads) {
			future.cancel(true);
		}
	}

}

@@ -677,196 +677,8 @@
 s;%0D%0A
-import com.commafeed.backend.model.Feed;%0D%0Aimport com.commafeed.backend.model.FeedCategory;%0D%0Aimport com.commafeed.backend.model.FeedSubscription;%0D%0Aimport com.commafeed.backend.model.User;%0D%0A
 impo
@@ -720,24 +720,24 @@
 Role.Role;%0D%0A
+
 import com.c
@@ -1929,18 +1929,16 @@
 ues%22);%0D%0A
-%0D%0A
 %09%09applic
@@ -1996,24 +1996,10 @@
 );%0D%0A
-%0D%0A%09%09User user = 
+%09%09
 user
@@ -2085,1513 +2085,8 @@
 );%0D%0A
-%09%09userService.register(%22test%22, %22test%22, Arrays.asList(Role.USER));%0D%0A%0D%0A%09%09Feed dilbert = new Feed(%22http://feed.dilbert.com/dilbert/daily_strip%22);%0D%0A%09%09feedDAO.save(dilbert);%0D%0A%0D%0A%09%09Feed engadget = new Feed(%22http://www.engadget.com/rss.xml%22);%0D%0A%09%09feedDAO.save(engadget);%0D%0A%0D%0A%09%09Feed frandroid = new Feed(%22http://feeds.feedburner.com/frandroid%22);%0D%0A%09%09feedDAO.save(frandroid);%0D%0A%0D%0A%09%09FeedCategory newsCategory = new FeedCategory();%0D%0A%09%09newsCategory.setName(%22News%22);%0D%0A%09%09newsCategory.setUser(user);%0D%0A%09%09feedCategoryDAO.save(newsCategory);%0D%0A%0D%0A%09%09FeedCategory comicsCategory = new FeedCategory();%0D%0A%09%09comicsCategory.setName(%22Comics%22);%0D%0A%09%09comicsCategory.setUser(user);%0D%0A%09%09comicsCategory.setParent(newsCategory);%0D%0A%09%09feedCategoryDAO.save(comicsCategory);%0D%0A%0D%0A%09%09FeedCategory techCategory = new FeedCategory();%0D%0A%09%09techCategory.setName(%22Tech%22);%0D%0A%09%09techCategory.setUser(user);%0D%0A%09%09techCategory.setParent(newsCategory);%0D%0A%09%09feedCategoryDAO.save(techCategory);%0D%0A%0D%0A%09%09FeedSubscription sub = new FeedSubscription();%0D%0A%09%09sub.setCategory(comicsCategory);%0D%0A%09%09sub.setFeed(dilbert);%0D%0A%09%09sub.setTitle(%22Dilbert - Strips%22);%0D%0A%09%09sub.setUser(user);%0D%0A%09%09feedSubscriptionDAO.save(sub);%0D%0A%0D%0A%09%09FeedSubscription sub2 = new FeedSubscription();%0D%0A%09%09sub2.setCategory(techCategory);%0D%0A%09%09sub2.setFeed(engadget);%0D%0A%09%09sub2.setTitle(%22Engadget%22);%0D%0A%09%09sub2.setUser(user);%0D%0A%09%09feedSubscriptionDAO.save(sub2);%0D%0A%0D%0A%09%09FeedSubscription sub3 = new FeedSubscription();%0D%0A%09%09sub3.setFeed(frandroid);%0D%0A%09%09sub3.setTitle(%22Frandroid%22);%0D%0A%09%09sub3.setUser(user);%0D%0A%09%09feedSubscriptionDAO.save(sub3);%0D%0A
 %09%7D%0D%0A

remove initial data