package au.com.subash.controller;

import au.com.subash.entity.TodoItem;
import au.com.subash.entity.TodoList;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

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

    private List<TodoList> todoLists;
    private TodoList selectedList;
    private String newTodoItem;

    public TodoBean() {
        todoLists = new ArrayList();
        
        ArrayList<TodoItem> todoItems1 = new ArrayList();
        todoItems1.add(new TodoItem("Work on assignment", true));
        todoItems1.add(new TodoItem("Call sujan"));
        
                
        ArrayList<TodoItem> todoItems2 = new ArrayList();
        todoItems2.add(new TodoItem("Make some breakfast"));
        todoItems2.add(new TodoItem("Go to Caufield"));
        
        todoLists.add(new TodoList("list 1", todoItems1));
        todoLists.add(new TodoList("list 2", todoItems2));
        
        selectedList = todoLists.get(0);
    }

    public List<TodoList> getTodoLists() {
        return todoLists;
    }

    public void setTodoLists(List<TodoList> todoLists) {
        this.todoLists = todoLists;
    }

    public TodoList getSelectedList() {
        return selectedList;
    }

    public void setSelectedList(TodoList selectedList) {       
        this.selectedList = todoLists.stream()
                .filter(l -> l.getTitle().equals(selectedList.getTitle()))
                .findFirst()
                .get();
    }
    
    public String getNewTodoItem() {
        return newTodoItem;
    }

    public void setNewTodoItem(String newTodoItem) {
        this.newTodoItem = newTodoItem;
    }
    
    /**
     * Add new todo list
     */
    public void addTodoList() {
        selectedList = new TodoList("Untitled");
        todoLists.add(selectedList);
    }
    
    public void addTodoItem() {
        if (null == newTodoItem || newTodoItem.isEmpty()) { 
            return; 
        }
        
        selectedList.getTodoItems().add(new TodoItem(newTodoItem));
        setNewTodoItem(null);
    } 
    
    public void removeTodoList() {
        todoLists.remove(selectedList);
        
        if (todoLists.size() > 0) {
            selectedList = todoLists.get(0);
        } else {
            selectedList = null;
        }
    }
    
    public void toogleTodoItem(TodoItem todoItem) {
        TodoItem item = selectedList.getTodoItems().stream()
                .filter(t -> t.getTitle().equals(todoItem.getTitle()))
                .findFirst().get();

        item.setIsComplete(item.getIsComplete());
    }
}

@@ -1108,32 +1108,102 @@
 .get(0);%0A    %7D%0A%0A
+    /**%0A     * Get all lists%0A     * %0A     * @return TodoLists%0A     */%0A
     public List%3C
@@ -1254,32 +1254,107 @@
 doLists;%0A    %7D%0A%0A
+    /**%0A     * Set lists%0A     * %0A     * @param todoLists TodoLists%0A     */%0A
     public void 
@@ -1445,79 +1445,247 @@
     
-public TodoList getSelectedList() %7B%0A        return selectedList;%0A    %7D%0A
+/**%0A     * Get selected list%0A     * %0A     * @return Selected List %0A     */%0A    public TodoList getSelectedList() %7B%0A        return selectedList;%0A    %7D%0A%0A    /**%0A     * Set the selected list%0A     * %0A     * @param selectedList TodoList%0A     */
 %0A   
@@ -1939,75 +1939,235 @@
     
-public String getNewTodoItem() %7B%0A        return newTodoItem;%0A    %7D%0A
+/**%0A     * Get new item%0A     * %0A     * @return Name of new item %0A     */%0A    public String getNewTodoItem() %7B%0A        return newTodoItem;%0A    %7D%0A%0A    /**%0A     * Set new item %0A     * %0A     * @param newTodoItem Item title %0A     */
 %0A   
@@ -2294,13 +2294,8 @@
 new 
-todo 
 list
@@ -2424,32 +2424,76 @@
 st);%0A    %7D%0A    %0A
+    /**%0A     * Add item to the list%0A     */%0A
     public void 
@@ -2715,24 +2715,108 @@
     %7D %0A    %0A
+    /**%0A     * Remove selected list. This will remove all its items%0A     * %0A     */%0A
     public v
@@ -3035,24 +3035,113 @@
 %0A    %7D%0A    %0A
+    /**%0A     * Change status of the item%0A     * %0A     * @param todoItem TodoItem%0A     */%0A
     public v

add doc block