package org.yacos.web.admin;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.yacos.core.article.IArticleManager;
import org.yacos.core.conferences.IConferenceManager;
import org.yacos.core.exceptions.NoConferenceCreationTokenLeftException;
import org.yacos.core.users.IUserManager;
import org.yacos.core.users.User;
import org.yacos.web.system.controller.MailSenderService;
import org.yacos.web.system.session.YACOSUtils;

public class AdminController extends MultiActionController {
	private IArticleManager articleManager;
	private IUserManager userManager;
	private IConferenceManager conferenceManager;
	
	private HashSet<String> addedConferenceCreationTokens = new HashSet<String>();
	/**
	 * @return the articleManager
	 */
	public IArticleManager getArticleManager() {
		return articleManager;
	}
	/**
	 * @param articleManager the articleManager to set
	 */
	public void setArticleManager(IArticleManager articleManager) {
		this.articleManager = articleManager;
	}
	/**
	 * @return the userManager
	 */
	public IUserManager getUserManager() {
		return userManager;
	}
	/**
	 * @param userManager the userManager to set
	 */
	public void setUserManager(IUserManager userManager) {
		this.userManager = userManager;
	}
	/**
	 * @return the conferenceManager
	 */
	public IConferenceManager getConferenceManager() {
		return conferenceManager;
	}
	/**
	 * @param conferenceManager the conferenceManager to set
	 */
	public void setConferenceManager(IConferenceManager conferenceManager) {
		this.conferenceManager = conferenceManager;
	}
	
	public int addConferenceCreationToken(String login){
		conferenceManager.addConferenceCreationToken(login);
		addedConferenceCreationTokens.add(login);
		return conferenceManager.conferenceCreationTokens(login);
	}
	
	public int removeConferenceCreationToken(String login){
		try {
			conferenceManager.useConferenceCreationToken(login);
		} catch (NoConferenceCreationTokenLeftException e) {
			// We don't have to handle this exception, the count just won't change
		}
		return conferenceManager.conferenceCreationTokens(login);
	}
	
	public void sendNotifications(){
		User user;
		for(String login : addedConferenceCreationTokens){
			user = userManager.getUser(login);
			// We can't be sure of what happened on the client side, so we avoid sending mails for nothing
			if(user.getConferenceCreationTokens() > 0 ){
				MailSenderService.getInstance().sendEMail(user.getEmail(),
					"YACOS : "+user.getFirstName()+" "+user.getLastName()+" you have been granted a conference creation token",
					"You receive this email because the YACOS admin allowed you to create a new conference in the system.\n"
					+ "Your current conference creation token count is "+user.getConferenceCreationTokens()+"\n"
					+ YACOSUtils.fullURL("addConference.htm"));
			}
		}
	}
	
	public List<DWRPersonBean> getUsersList(String filter){
		List<DWRPersonBean> beans = new ArrayList<DWRPersonBean>();
		DWRPersonBean bean;
		for(User user : userManager.getUsers()){
			if (filter.equals("") ||
				user.getFirstName().toLowerCase().startsWith(filter.toLowerCase()) ||
				user.getLastName().toLowerCase().startsWith(filter.toLowerCase())){
				
				bean = new DWRPersonBean();
				bean.setLogin(user.getLogin());
				bean.setFirstName(user.getFirstName());
				bean.setLastName(user.getLastName());
				bean.setId(beans.size());
				bean.setNumTokens(conferenceManager.conferenceCreationTokens(user.getLogin()));
				beans.add(bean);
			}
		}
		return beans;
	}
	
	public ModelAndView show(HttpServletRequest request, HttpServletResponse response){
		return new ModelAndView("admin");
	}
}
