package org.yacos.web.system.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.view.RedirectView;
import org.yacos.core.conferences.IConferenceManager;
import org.yacos.web.system.session.SessionService;

public class ChooseConferenceController implements Controller {
	
	private IConferenceManager conferenceManager;

	public IConferenceManager getConferenceManager() {
		return conferenceManager;
	}
	public void setConferenceManager(IConferenceManager conferenceManager) {
		this.conferenceManager = conferenceManager;
	}
	
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		Map<String, Object> model = new HashMap<String, Object>();
		
		// get IDConf if exist
		if (request.getParameter("idConf") != null)
		{
			try
			{
				int idConf = Integer.parseInt(request.getParameter("idConf"));
				
				if(conferenceManager.exists(idConf))
				{
					// Set idConf in session context
					SessionService.getInstance().setCurrentConferenceId(idConf);
					
					return new ModelAndView("main");
				}
				else
				{
					model.put("error", "1");
					return new ModelAndView(new RedirectView("listConference.htm"), model);
				}
			}
			catch(NumberFormatException e)
			{
				model.put("error", "3");
				return new ModelAndView(new RedirectView("listConference.htm"), model);
			}
		
		}
		else
		{
			model.put("error", "2");
			return new ModelAndView(new RedirectView("listConference.htm"), model);
		}

	}
}
