package seedu.todo.controllers;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import com.joestelmach.natty.*;

import seedu.todo.models.Event;
import seedu.todo.models.Task;
import seedu.todo.models.TodoListDB;
import seedu.todo.ui.UiManager;
import seedu.todo.ui.views.IndexView;

public class AddController implements Controller {

    @Override
    public float inputConfidence(String input) {
        // TODO
        return (input.startsWith("add")) ? 1 : 0;
    }

    @Override
    public void process(String args) {
        // TODO: Example of last minute work
        
        args = args.replaceFirst("add", "").trim();
        
        // Task or event
        boolean isTask = true; // Default to task.
        if (args.startsWith("task")) {
            isTask = true;
            args = args.replaceFirst("task", "").trim();
        } else if (args.startsWith("event")) {
            isTask = false;
            args = args.replaceFirst("event", "").trim();
        }
        
        // Parse name and date.
        String[] splitted = args.split("( at | by )", 2);
        String name = splitted[0].trim();
        String naturalDate = splitted[1].trim();
        
        // Parse natural date using Natty.
        Parser parser = new Parser();
        List<DateGroup> groups = parser.parse(naturalDate);
        Date date = groups.get(0).getDates().get(0);
        LocalDateTime ldt = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
        
        // Create and persist task / event.
        TodoListDB db = TodoListDB.getInstance();
        if (isTask) {
            Task newTask = db.createTask();
            newTask.setName(name);
            newTask.setDueDate(ldt);
            db.save();
        } else {
            Event newEvent = db.createEvent();
            newEvent.setName(name);
            newEvent.setStartDate(ldt);
            newEvent.setEndDate(ldt); // TODO
            db.save();
        }
        
        // Re-render
        IndexView view = UiManager.loadView(IndexView.class);
        view.tasks = db.getAllTasks();
        view.render();
        
    }
    
}

@@ -519,16 +519,712 @@
 0;%0A    %7D
+%0A    %0A    private int splitByKeyword(String args, boolean isTask) %7B%0A        int indexOfBy = args.lastIndexOf(%22by%22);%0A        int indexOfOn = args.lastIndexOf(%22on%22);%0A        %0A        // support for event from date to date%0A        if (!isTask) %7B%0A            int indexOfFrom = args.lastIndexOf(%22from%22);%0A            int indexOfTo = args.lastIndexOf(%22to%22);%0A            if (indexOfTo == -1 %7C%7C indexOfFrom == -1) %7B%0A                indexOfFrom = -1;%0A                indexOfTo = -1;%0A            %7D else %7B%0A                // to be updated to supported pair value of from and to%0A                return indexOfFrom;%0A            %7D%0A        %7D%0A        %0A        return Math.max(indexOfBy, indexOfOn);%0A        %0A    %7D
 %0A%0A    @O
@@ -1780,97 +1780,200 @@
     
-String%5B%5D splitted = args.split(%22( at %7C by )%22, 2);%0A        String name = splitted%5B0%5D.trim(
+int splitValue = splitByKeyword(args, isTask);%0A        String naturalDate = %22Today%22;%0A        String name = args;%0A        if (splitValue != -1) %7B%0A            name = args.substring(0, splitValue
 );%0A 
@@ -1971,38 +1971,35 @@
 Value);%0A        
-String
+   
  naturalDate = s
@@ -2001,27 +2001,63 @@
 e = 
-splitted%5B1%5D.trim();
+args.substring(splitValue);%0A        %7D%0A        %0A        
 %0A   

change controller to support floating task and parsing based on last index of keyword