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 at.irian.ankorman.sample2.viewmodel.animal.helper.Data;
import at.irian.ankorman.sample2.viewmodel.animal.helper.Paginator;
import com.fasterxml.jackson.annotation.JsonIgnore;

public class TaskListModel extends ViewModelBase {

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

    @JsonIgnore
    private final TaskRepository taskRepository;

    @JsonIgnore
    private ViewModelProperty<String> tabName;

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

    private Data<Task> tasks;

    // XXX: Did not work using a list
    //private List<Task> tasks = new ArrayList<Task>();

    public TaskListModel(Ref viewModelRef, TaskRepository taskRepository, ViewModelProperty<String> tabName) {
        super(viewModelRef);
        this.tabName = tabName;

        this.taskRepository = taskRepository;

        this.filter.set(Filter.all.toString());
        this.itemsLeft.set(String.valueOf(taskRepository.getTasks().size())); // XXX: Weird type error if not using string
        this.tasks = fetchTasksData(new Paginator(0, Integer.MAX_VALUE));

    }

    @ChangeListener(pattern = {
            "**.<TaskListModel>.itemsLeft",
            "**.<TaskListModel>.filter"
    })
    public void reloadTasks() {
        tasks = fetchTasksData(tasks.getPaginator());
        thisRef().append("tasks").setValue(tasks);
    }

    @ActionListener(name = "newTodo")
    public void newTodo(@Param("title") final String title) {
        LOG.info("Add new task to task repository");

        Task task = new Task(title);
        taskRepository.saveTask(task);

        // XXX: Setting values to refs here causes (sometimes!?) exceptions

        //int currItemsLeft = Integer.parseInt(itemsLeft.get());
        //thisRef().append("itemsLeft").setValue(String.valueOf(currItemsLeft + 1));

        // XXX: Difference?
        //itemsLeft.set(String.valueOf(currItemsLeft + 1));
    }

    private Data<Task> fetchTasksData(Paginator paginator) {
        paginator.reset();
        Filter filterEnum = Filter.valueOf(filter.get());
        return taskRepository.searchTasks(filterEnum, paginator.getFirst(), paginator.getMaxResults());
    }

    public ViewModelProperty<String> getItemsLeft() {
        return itemsLeft;
    }

    public void setItemsLeft(ViewModelProperty<String> itemsLeft) {
        this.itemsLeft = itemsLeft;
    }

    public Data<Task> getTasks() {
        return tasks;
    }

    public void setTasks(Data<Task> tasks) {
        this.tasks = tasks;
    }

    public ViewModelProperty<String> getFilter() {
        return filter;
    }

    public void setFilter(ViewModelProperty<String> filter) {
        this.filter = filter;
    }

}

@@ -985,16 +985,62 @@
 sLeft;%0A%0A
+    // TODO: Use a list instead of Data%3CTask%3E%0A
     priv
@@ -1618,25 +1618,24 @@
 AX_VALUE));%0A
-%0A
     %7D%0A%0A    @
@@ -1792,21 +1792,73 @@
         
-tasks
+LOG.info(%22reloading tasks%22);%0A        Data%3CTask%3E tasksData
  = fetch
@@ -1938,16 +1938,20 @@
 ue(tasks
+Data
 );%0A    %7D

fix reloading data on filter click