package net.kucoe.elvn.lang.result;

import java.io.IOException;

import net.kucoe.elvn.*;
import net.kucoe.elvn.lang.ELCommand;
import net.kucoe.elvn.util.*;

/**
 * Task changes over located task by position.
 * 
 * @author Vitaliy Basyuk
 */
public class LocateTask extends TaskResult {
    
    /**
     * Explicit note/task item
     */
    public Note item;
    
    /**
     * Position.
     */
    public final int position;
    
    /**
     * Constructs LocateTask.
     */
    public LocateTask() {
        this(null, null, 0);
    }
    
    /**
     * Constructs LocateTask.
     * 
     * @param list
     * @param text
     * @param position
     */
    public LocateTask(final String list, final String text, final int position) {
        super(list, text);
        this.position = position;
    }
    
    @Override
    public String execute(final Display display, final Config config) throws Exception {
        String currentList = display.getCurrentList();
        boolean reshow = true;
        if (ELCommand.Notes.el().equals(currentList)) {
            Note note = getNote(config);
            if (note != null) {
                if (text == null && list == null) {
                    reshow = false;
                    display.showNote(note, position);
                } else if (list != null) {
                    config.removeNote(note);
                    config.saveTask(new Task(note.getId(), list, note.getText(), false, null));
                    return forward(new SwitchListColor(ListColor.All.toString()), display, config);
                } else if (text != null) {
                    config.saveNote(new Note(note.getId(), processText(note.getText(), text)));
                }
            }
            return reshow ? forward(new SwitchNotes(), display, config) : currentList;
        }
        Task task = getTask(currentList, config);
        if (task != null) {
            if (list == null && text == null) {
                reshow = false;
                display.showTask(task, position);
            } else {
                updateTask(task, config, currentList);
            }
        }
        return reshow ? forward(new SwitchListColor(currentList), display, config) : currentList;
    }
    
    protected Note getNote(final Config config) throws IOException, JsonException {
        if (item != null) {
            return item;
        }
        java.util.List<Note> notes = config.getNotes();
        if (notes.size() >= position) {
            return notes.get(position - 1);
        }
        return null;
    }
    
    protected Task getTask(final String currentList, final Config config) throws IOException, JsonException {
        if (ELCommand.Notes.el().equals(currentList)) {
            return null;
        }
        if (item instanceof Task) {
            return (Task) item;
        }
        List list = config.getList(ListColor.color(currentList));
        if (list != null) {
            java.util.List<Task> tasks = list.getTasks();
            if (tasks.size() >= position) {
                return tasks.get(position - 1);
            }
        }
        return null;
    }
    
}

@@ -1318,32 +1318,362 @@
 te, position);%0D%0A
+                %7D else if (text != null) %7B%0D%0A                    String t = text;%0D%0A                    if (t == null) %7B%0D%0A                        t = %22%22;%0D%0A                    %7D%0D%0A                    t = list == null ? t : list + %22:%22 + t;%0D%0A                    config.saveNote(new Note(note.getId(), processText(note.getText(), t)));%0D%0A
                 
@@ -1948,149 +1948,8 @@
 );%0D%0A
-                %7D else if (text != null) %7B%0D%0A                    config.saveNote(new Note(note.getId(), processText(note.getText(), text)));%0D%0A
     

Save note first list, last