package de.unibremen.opensores.controller;

import de.unibremen.opensores.model.Course;
import de.unibremen.opensores.model.User;
import de.unibremen.opensores.service.UserService;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import java.util.ArrayList;
import java.util.List;

/**
 * This class implements the Controller for managing the logged in user.
 */
@SessionScoped
@ManagedBean
public class UserController {

    /**
     * The currently logged in user.
     */
    private User user;

    /**
     * The UserService for database connection.
     */
    @EJB
    private UserService userService;

    @PostConstruct
    public void init() {
        user = (User) FacesContext.getCurrentInstance()
                .getExternalContext().getSessionMap().get("user");
    }

    /**
     * Logs the user out of the current session.
     * Invalidates the current session map.
     * Redirects to the login page.
     * TODO Maybe move this method to NavigationController
     * @return The of the login page.
     */
    public String logout() {
        FacesContext.getCurrentInstance()
                .getExternalContext().invalidateSession();
        return "/login.xhtml?faces-redirect=true";
    }

    /**
     * Saves the current user in the database.
     * @Pre The user has valid fields.
     */
    public void saveUser() {
        userService.update(user);
    }

    /**
     * Gets the currently logged in user.
     * @return The currently logged in user.
     */
    public User getUser() {
        return user;
    }

    /**
     * Sets the current user to a new user object.
     * @param user A new logged in user, must have valid fields.
     */
    public void setUser(User user) {
        this.user = user;
    }

    /**
     * TODO: Implement
     */
    public List<Course> getActiveCourses() {
        return null;
    }

    public List<Course> getHiddenCourses() {
        return null;
    }
}

@@ -1450,20 +1450,22 @@
  public 
-void
+String
  saveUse
@@ -1504,16 +1504,48 @@
 (user);%0A
+        return %22profile.xhtml%22;%0A
     %7D%0A%0A 

Edit saveUser() mehrod in UserController to return "profile.xhtml".