package seedu.todo.controllers;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.joestelmach.natty.DateGroup;
import com.joestelmach.natty.Parser;

import seedu.todo.commons.EphemeralDB;
import seedu.todo.controllers.concerns.Renderer;
import seedu.todo.models.CalendarItem;
import seedu.todo.models.TodoListDB;

/**
 * @@author A0093907W
 * 
 * Controller to update a CalendarItem.
 */
public class UpdateController implements Controller {
    
    private static final String NAME = "Update";
    private static final String DESCRIPTION = "Updates a task by listed index.";
    private static final String COMMAND_SYNTAX = "update <index> <task> by <deadline>";
    
    private static final String MESSAGE_UPDATE_SUCCESS = "Item successfully updated!";
    
    private static CommandDefinition commandDefinition =
            new CommandDefinition(NAME, DESCRIPTION, COMMAND_SYNTAX); 

    public static CommandDefinition getCommandDefinition() {
        return commandDefinition;
    }

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

    @Override
    public void process(String args) {
        // TODO: Example of last minute work
        
        args = args.replaceFirst("update", "").trim();
        
        // Get index.
        System.out.println(args);
        
        Matcher matcher = Pattern.compile("^\\d+").matcher(args);
        matcher.find();
        String indexStr = matcher.group();
        int index = Integer.decode(indexStr.trim());
        
        // Parse name and date.
        args = args.replaceFirst(indexStr, "").trim();
        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());
        
        // Get record
        EphemeralDB edb = EphemeralDB.getInstance();
        CalendarItem calendarItem = edb.getCalendarItemsByDisplayedId(index);
        TodoListDB db = TodoListDB.getInstance();
        
        if (calendarItem != null) {
            calendarItem.setName(name);
            calendarItem.setCalendarDateTime(ldt);
            db.save();
        }
        
        // Re-render
        Renderer.renderIndex(db, MESSAGE_UPDATE_SUCCESS);
    }
}

@@ -1388,1326 +1388,8 @@
     
-args = args.replaceFirst(%22update%22, %22%22).trim();%0A        %0A        // Get index.%0A        System.out.println(args);%0A        %0A        Matcher matcher = Pattern.compile(%22%5E%5C%5Cd+%22).matcher(args);%0A        matcher.find();%0A        String indexStr = matcher.group();%0A        int index = Integer.decode(indexStr.trim());%0A        %0A        // Parse name and date.%0A        args = args.replaceFirst(indexStr, %22%22).trim();%0A        String%5B%5D splitted = args.split(%22( at %7C by )%22, 2);%0A        String name = splitted%5B0%5D.trim();%0A        String naturalDate = splitted%5B1%5D.trim();%0A        %0A        // Parse natural date using Natty.%0A        Parser parser = new Parser();%0A        List%3CDateGroup%3E groups = parser.parse(naturalDate);%0A        Date date = groups.get(0).getDates().get(0);%0A        LocalDateTime ldt = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());%0A        %0A        // Get record%0A        EphemeralDB edb = EphemeralDB.getInstance();%0A        CalendarItem calendarItem = edb.getCalendarItemsByDisplayedId(index);%0A        TodoListDB db = TodoListDB.getInstance();%0A        %0A        if (calendarItem != null) %7B%0A            calendarItem.setName(name);%0A            calendarItem.setCalendarDateTime(ldt);%0A            db.save();%0A        %7D%0A        %0A        // Re-render%0A        Renderer.renderIndex(db, MESSAGE_UPDATE_SUCCESS);
 %0A   

Remove old code