package ub.scp2.managedBeans;

import infoObjects.InfoTarea;

import java.io.IOException;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

import org.primefaces.context.RequestContext;
import org.primefaces.event.SelectEvent;
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;
import utils.RMIServices;

/**
 * 
 * TeacherViewBean.java -a Bean to control the teacher view.
 *
 */
@ManagedBean(name="teacherViewBean")
@ViewScoped
public class TeacherViewBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -707373380215050335L;
	
	private TaskBo taskbo;
	private UserBo userbo;
	private User user;
	private Task task;
	private Task expiredtask;
	private Date today;
	private boolean noStartDate;
	
    @ManagedProperty(value="#{loginBean}")
	private LoginBean loginbean;
	
	public TeacherViewBean(){}
	
	/**
	 * 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);
        task = new Task();
        today = new Date();
    }
    
    /**
     * 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()");
    }
    
    /**
     * Delete the select task from the aplication.
     * @throws NotBoundException 
     * @throws RemoteException 
     * @throws MalformedURLException 
     */
    public void deleteTask() throws MalformedURLException, RemoteException, NotBoundException{
    	if (!RMIServices.isConnected()){
        	RMIServices.connectRMI();
        }
    	InfoTarea infoTarea = new InfoTarea(task.getTaskId());
    	RMIServices.securityDeleteTask(infoTarea);
    	taskbo.deleteTask(task);
    }
    
    /**
     * Delete the select expiredtask from the aplication.
     * @throws NotBoundException 
     * @throws RemoteException 
     * @throws MalformedURLException 
     */
    public void deleteTaskExpired() throws MalformedURLException, RemoteException, NotBoundException{
    	if (!RMIServices.isConnected()){
        	RMIServices.connectRMI();
        }
    	InfoTarea infoTarea = new InfoTarea(expiredtask.getTaskId());
    	RMIServices.securityDeleteTask(infoTarea);
    	taskbo.deleteTask(expiredtask);
    }
	
    /**
     * Redirect to the new task view (newTask)
     * @return A type of String, This string indicates the instruction to redirect.
     */
	public String newTask(){
		return "newtask?faces-redirect=true";
	}
	
	/**
	 * Redirect to the add test view (addTest)
	 * @return A type of String, This string indicates the instruction to redirect.
	 */
	public String addTests(){
		//if(task.getStartDate().after(this.today)){
			loginbean.setTask(task);
			return "addtests?faces-redirect=true";
		/*}else{
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "You can only edit the tests of the tasks that have not yet been started!","You can only edit the tests of the tasks that have not yet been started!");  
            FacesContext.getCurrentInstance().addMessage(null, msg);
			return "teacher?faces-redirect=false";
		}*/
	}
	
	/**
	 * Redirect to the view report (viewReport)
	 * @return A type of String, This string indicates the instruction to redirect.
	 */
    public String viewReport(){
    	loginbean.setTask(task);
    	return "report?faces-redirect=true";
    }
    
    /**
     * Redirect to the expired task of the report view
     * @return A type of String, This string indicates the instruction to redirect.
     */
    public String viewReportExpired(){
    	loginbean.setTask(expiredtask);
    	return "report?faces-redirect=true";
    }
	
    /**
     * Retrieve a list with the active task for one user
     * @return A List of Task type.
     */
	public List<Task> getActiveTaskList(){
		return taskbo.getActiveTasksByUser(user);
	}
	
    /**
     * Retrieve a list with the expired task for one user
     * @return A List of Task type.
     */
	public List<Task> getExpiredTaskList(){
		return taskbo.getExpiredTasksByUser(user);
	}
	
	public void setLoginbean(LoginBean loginbean) {
		this.loginbean = loginbean;
	}
	
	public void setTask(Task task){
		this.task = task;
	}
	
	public Task getTask(){
		return task;
	}

	public Task getExpiredtask() {
		return expiredtask;
	}

	public void setExpiredtask(Task expiredtask) {
		this.expiredtask = expiredtask;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Date getToday() {
		return today;
	}

	public void setToday(Date today) {
		this.today = today;
	}

	public boolean isNoStartDate() {
		return noStartDate;
	}

	public void setNoStartDate(boolean noStartDate) {
		this.noStartDate = noStartDate;
	}	

}

