package jp.gr.java_conf.shioyang.polyhedraltodolist;

import com.google.api.services.tasks.model.Task;
import com.google.api.services.tasks.model.TaskList;

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

public class PolyTodoListImpl implements PolyTodoList {
    static Comparator<PolyTodoItem> comparator;

    static {
        comparator = new PolyTodoItemComparator();
    }

    /*
    String id
    String title
    */
    TaskList taskList;
    List<PolyTodoItem> localList;
    int color;
    boolean isPolyTodoList = true;

    public PolyTodoListImpl(TaskList taskList) {
        this.taskList = taskList;
        this.localList = new ArrayList<>();
        if (taskList != null) {
            // TODO
        }
    }

    public PolyTodoListImpl(TaskList taskList, List<Task> tasks, int color) {
        this.taskList = taskList;
        this.localList = new ArrayList<>();
        // TODO: Set color
        this.color = color;
        if (taskList != null && tasks != null) {
            String listId = taskList.getId();
            for (Task task : tasks) {
                if (task.getTitle().isEmpty())
                    break;
                PolyTodoItem polyTodoItem = new PolyTodoItemImpl(task, listId, color);
                localList.add(polyTodoItem);
                isPolyTodoList = isPolyTodoList && polyTodoItem.isPolyTodoItem(); // If there is a non PolyTodoItem, isPolyTodoList is false.
            }
        }
    }

    @Override
    public  boolean isPolyTodoList() {
        return isPolyTodoList;
    }

    @Override
    public void loadTodoItems() {

    }

    @Override
    public List<PolyTodoItem> getLocalList() {
        return localList;
    }

    @Override
    public void addNewTodo(int previousId) {

    }

    @Override
    public void addNewTodoPosition(int position) {

    }

    @Override
    public void addNewTodoFirst() {

    }

    @Override
    public Task addNewTodoLast(int globalPosition) {
        return createTodoItem(globalPosition, localList.size(), "");
    }

    @Override
    public String getId() {
        return taskList.getId();
    }

    @Override
    public String getTaskId(int position) {
        String taskId = "";
        PolyTodoItem polyTodoItem = localList.get(position);
        if (polyTodoItem != null) {
            taskId = polyTodoItem.getId();
        }
        return taskId;
    }

    @Override
    public PolyTodoItem getPreviousTask(PolyTodoItem item) {
        PolyTodoItem previousTask = null;
        String previousTaskId = getPreviousTaskId(item);
        if (!previousTaskId.isEmpty()) {
            previousTask = localList.get(localList.indexOf(previousTaskId));
        }
        return previousTask;
    }

    @Override
    public String getPreviousTaskId(PolyTodoItem item) {
        String foundPreviousTaskId = "";
        if (item != null) {
            String taskId = item.getId();
            for (int i = 1; i < localList.size(); i++) {
                String previousTaskId = localList.get(i - 1).getId();
                String currentTaskId = localList.get(i).getId();
                if (currentTaskId.equals(taskId)) {
                    foundPreviousTaskId = previousTaskId;
                    break;
                }
            }
        }
        return foundPreviousTaskId;
    }

    @Override
    public String getTitle() {
        return taskList.getTitle();
    }

    @Override
    public void done(int id) {

    }

    @Override
    public void updateTitle(int id) {

    }

    @Override
    public void moveUpTask(String id) {

    }

    @Override
    public void moveDownTask(String id) {

    }

    @Override
    public void delete(int id) {

    }

    private Task createTodoItem(int globalPosition, int localPosition, String justTitle) {
        Task task = new Task();
        task.setTitle(PolyUtil.formatTaskTitle(globalPosition, localPosition, justTitle));
        PolyTodoItem polyTodoItem = new PolyTodoItemImpl(task, getId(), color);
        localList.add(polyTodoItem);
        return task;
    }
}

@@ -2509,360 +2509,26 @@
 tem 
-previousTask = null;%0A        String previousTaskId = getPreviousTaskId(item);%0A        if (!previousTaskId.isEmpty()) %7B%0A            previousTask = localList.get(localList.indexOf(previousTaskId));%0A        %7D%0A        return previousTask;%0A    %7D%0A%0A    @Override%0A    public String getPreviousTaskId(PolyTodoItem item) %7B%0A        String foundPreviousTaskId = %22%22
+rtnTodoItem = null
 ;%0A  
@@ -2568,28 +2568,28 @@
      String 
-task
+item
 Id = item.ge
@@ -2668,22 +2668,28 @@
         
-String
+PolyTodoItem
  previou
@@ -2689,22 +2689,16 @@
 previous
-TaskId
  = local
@@ -2712,24 +2712,16 @@
 t(i - 1)
-.getId()
 ;%0A      
@@ -2744,20 +2744,16 @@
  current
-Task
 Id = loc
@@ -2802,20 +2802,16 @@
 (current
-Task
 Id.equal
@@ -2812,20 +2812,20 @@
 .equals(
-task
+item
 Id)) %7B%0A 
@@ -2843,35 +2843,27 @@
         
-foundPreviousTaskId
+rtnTodoItem
  = previ
@@ -2861,30 +2861,24 @@
 m = previous
-TaskId
 ;%0A          
@@ -2955,27 +2955,211 @@
 urn 
-foundPreviousTaskId
+rtnTodoItem;%0A    %7D%0A%0A    @Override%0A    public String getPreviousTaskId(PolyTodoItem item) %7B%0A        PolyTodoItem previous = getPreviousTask(item);%0A        return (previous != null) ? previous.getId() : %22%22
 ;%0A  

Update implementation of getPreviousTask and getPreviousTaskId