package au.com.subash.controller;

import au.com.subash.entity.TodoItem;
import au.com.subash.entity.TodoList;
import au.com.subash.session.ManageTodoListFacadeRemote;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;

/**
 *
 * @author subash
 */
@Named(value = "todoBean")
@SessionScoped
public class TodoBean implements Serializable {

    @EJB
    private ManageTodoListFacadeRemote facade;
    
    private List<TodoList> todoLists;
    private TodoList selectedList;
    private String newTodoItem;

    @PostConstruct
    public void init() {
        todoLists = facade.getTodoLists();
        
        if (todoLists.size() > 0) {
            selectedList = todoLists.get(0);
        } else {
            addTodoList();
        }
    }

    /**
     * Get all lists
     * 
     * @return TodoLists
     */
    public List<TodoList> getTodoLists() {
        return todoLists;
    }

    /**
     * Set lists
     * 
     * @param todoLists TodoLists
     */
    public void setTodoLists(List<TodoList> todoLists) {
        this.todoLists = todoLists;
    }

    /**
     * Get selected list
     * 
     * @return Selected List 
     */
    public TodoList getSelectedList() {
        return selectedList;
    }

    /**
     * Set the selected list
     * 
     * @param selectedList TodoList
     */
    public void setSelectedList(TodoList selectedList) {       
        this.selectedList = todoLists.stream()
                .filter(l -> l.getId() == selectedList.getId())
                .findFirst()
                .get();
    }
    
    /**
     * Get new item
     * 
     * @return Name of new item 
     */
    public String getNewTodoItem() {
        return newTodoItem;
    }

    /**
     * Set new item 
     * 
     * @param newTodoItem Item title 
     */
    public void setNewTodoItem(String newTodoItem) {
        this.newTodoItem = newTodoItem;
    }
    
    /**
     * Add new list
     */
    public void addTodoList() {
        TodoList list = facade.addTodoList(new TodoList("Untitled"));

        if (null != list) {
            selectedList = list;
            todoLists.add(selectedList);
        }       
    }
    
    /**
     * Add item to the list
     */
    public void addTodoItem() {
        if (null == newTodoItem || newTodoItem.isEmpty()) { 
            return; 
        }
        
        TodoItem item = new TodoItem(newTodoItem, selectedList.getId());
        
        selectedList.getTodoitemCollection().add(item);
        
        TodoList updateList = updateSelectedList();
        
        if (null != updateList) {
            selectedList = updateList;
            setNewTodoItem(null);
        } 
    } 
    
    /**
     * Edit title of selected list
     * 
     * @return boolean
     */
    public TodoList updateSelectedList() {
        return facade.updateTodoList(selectedList);
    }
    
    /**
     * Remove selected list. This will remove all its items
     * 
     */
    public void removeTodoList() {

        if (!facade.removeTodoList(selectedList.getId())) {
            return;
        }
        
        todoLists.remove(selectedList);
        
        if (todoLists.size() > 0) {
            selectedList = todoLists.get(0);
        } else {
            selectedList = null;
        }
    }
    
    /**
     * Change status of the item
     * 
     * @param todoItem TodoItem
     */
    public void toogleTodoItem(TodoItem todoItem) {
        TodoItem item = selectedList.getTodoitemCollection().stream()
                .filter(t -> t.getTitle().equals(todoItem.getTitle()))
                .findFirst().get();

        if (!facade.updateTodoItem(item)) {
            item.setIscomplete(!item.getIscomplete());
        }
    }
    
    /**
     * Check if list is currently selected
     * 
     * @param list
     * @return boolean 
     */
    public boolean isSelectedList(TodoList list) {
        return selectedList.getId() == list.getId();
    }
}

@@ -28,16 +28,53 @@
 oller;%0A%0A
+import au.com.subash.entity.AppUser;%0A
 import a
@@ -671,16 +671,42 @@
 odoItem;
+%0A    private AppUser user;
 %0A%0A    @P
@@ -739,24 +739,57 @@
 id init() %7B%0A
+        user = facade.getUser();%0A
         todo
@@ -945,32 +945,32 @@
  addTodoList();%0A
-
         %7D%0A    %7D%0A
@@ -962,32 +962,171 @@
        %7D%0A    %7D%0A%0A
+    /**%0A     * Get logged in user%0A     * %0A     * @return Logged in user%0A     */%0A    public AppUser getUser() %7B%0A        return user;%0A    %7D%0A%0A
     /**%0A     * G

set user in managed bean