package ub.scp2.managedBeans;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.primefaces.context.RequestContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.jsf.FacesContextUtils;

import ub.scp2.bo.TaskBo;
import ub.scp2.bo.UserBo;
import ub.scp2.entities.Task;
import ub.scp2.entities.User;

/**
 * StudentBean.java - a Bean to control the Student view
 * 
 *
 */
@ManagedBean(name="studentBean")
@ViewScoped
public class StudentBean {
	private TaskBo taskbo;
	private UserBo userbo;
	
	private User user;
	private Task task;
	private Task expiredtask;
	
    @ManagedProperty(value="#{loginBean}")
	private LoginBean loginbean;
	
	public StudentBean(){}
	
	/**
	 * Initialize the parameters required for the class
	 */
    @PostConstruct
    public void init() { 	
    	WebApplicationContext ctx =  FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
    	user = loginbean.getUser();
        taskbo = ctx.getBean(TaskBo.class);
        userbo = ctx.getBean(UserBo.class);
    }
    
    /**
     * Edit the login user
     */
    public void editUser(){
      	userbo.updateUser(user);
       	RequestContext context = RequestContext.getCurrentInstance();
       	context.execute("editUserDialog.hide()");
    }
    
    /**
     * Cancel the edition of user
     */
    public void cancelEditUser(){
		RequestContext context = RequestContext.getCurrentInstance();
		context.execute("editUserDialog.hide()");
    }
    
    /**
     * Redirect to the upload code view (newsubmission)
     * @return A type of String, This string indicates the instruction to redirect.
     */
    public String newSubmission(){
    	loginbean.setTask(task);
    	return "newsubmission?faces-redirect=true";
    }
    
    /**
     * Retrieve a list with the active task for one student
     * @return A List of Task type.
     */
    public List<Task> getActiveTaskList(){
    	return taskbo.getActiveTasksForStudent(user);
    }
    
    /**
     * Retrieve a list with the expired task for one student
     * @return A List of Task type.
     */
    public List<Task> getExpiredTaskList(){
    	return taskbo.getExpiredTasksForStudent(user);
    }
    
    /**
     * Set the private variable loginbean
     * @param loginbean A type of LoginBean
     */
	public void setLoginbean(LoginBean loginbean) {
		this.loginbean = loginbean;
	}
	
	/**
	 * Retrieve the private variable user
	 * @return A type Task
	 */
	public Task getTask() {
		return task;
	}

    /**
     * Set the private variable task
     * @param task A type of Task
     */
	public void setTask(Task task) {
		this.task = task;
	}

	/**
	 * Retrieve the private variable expiredtask
	 * @return A type Task
	 */
	public Task getExpiredtask() {
		return expiredtask;
	}

    /**
     * Set the private variable expiredtas
     * @param expiredtask A type of Task
     */
	public void setExpiredtask(Task expiredtask) {
		this.expiredtask = expiredtask;
	}
    
    
}
