package at.irian.ankorman.sample2.viewmodel.task;

import at.irian.ankor.annotation.ActionListener;
import at.irian.ankor.annotation.ChangeListener;
import at.irian.ankor.annotation.Param;
import at.irian.ankor.ref.Ref;
import at.irian.ankor.viewmodel.ViewModelBase;
import at.irian.ankor.viewmodel.ViewModelProperty;
import at.irian.ankorman.sample2.domain.task.Task;
import at.irian.ankorman.sample2.server.TaskRepository;
import com.fasterxml.jackson.annotation.JsonIgnore;

import java.util.ArrayList;
import java.util.List;

public class TaskListModel extends ViewModelBase {

    private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger(TaskListModel.class);

    private enum TasksCategory {
        all,
        uncomplete,
        complete;
    }

    @JsonIgnore
    private final TaskRepository taskRepository;

    //@JsonIgnore
    //private ViewModelProperty<String> tabName;

    private List<Task> tasks = new ArrayList<Task>();

    private ViewModelProperty<String> filter;
    private ViewModelProperty<Integer> itemsLeft;

    public TaskListModel(Ref viewModelRef, TaskRepository taskRepository, ViewModelProperty<String> tabName) {
        super(viewModelRef);
        this.taskRepository = taskRepository;
        //this.tabName = tabName;
        this.filter.set(TasksCategory.all.toString());
        this.itemsLeft.set(0);
    }

    @ChangeListener(pattern = "**.<TaskListModel>.filter")
    public void onFilterChanged() {
        // TODO: reload list
    }

    @ActionListener(name = "newTodo")
    public void newTodo(@Param("title") final String title) {
        LOG.info("save action");

        Task task = new Task(title);
        taskRepository.saveTask(task);
        // itemsLeft.set(itemsLeft.get() + 1); // ???
    }
}

@@ -1006,16 +1006,17 @@
 filter;%0A
+%0A
     priv
@@ -1041,15 +1041,14 @@
 rty%3C
-Integer
+String
 %3E it
@@ -1363,9 +1363,11 @@
 set(
-0
+%220%22
 );%0A 
@@ -1713,16 +1713,17 @@
 (task);%0A
+%0A
         
@@ -1729,50 +1729,393 @@
  // 
-itemsLeft.set(itemsLeft.get()
+XXX: fixed weired type bug by changing type from integer to string%0A        int currItemsLeft = Integer.parseInt(itemsLeft.get());%0A        itemsLeft.set(String.valueOf(currItemsLeft
  + 1)
-; // ???
+);%0A    %7D%0A%0A    public ViewModelProperty%3CString%3E getItemsLeft() %7B%0A        return itemsLeft;%0A    %7D%0A%0A    public void setItemsLeft(ViewModelProperty%3CString%3E itemsLeft) %7B%0A        this.itemsLeft = itemsLeft;
 %0A   

fix weird type bug by changeing viewmodelproperty from integer to string