package guitests;

import static org.junit.Assert.assertTrue;
import static seedu.taskmanager.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;

import org.junit.Test;

import guitests.guihandles.EventTaskCardHandle;
import seedu.taskmanager.commons.core.Messages;
import seedu.taskmanager.logic.commands.UpdateCommand;
import seedu.taskmanager.model.task.EndTime;
import seedu.taskmanager.model.task.StartDate;
import seedu.taskmanager.model.task.StartTime;
// import seedu.taskmanager.model.category.Category;
import seedu.taskmanager.testutil.TaskBuilder;
import seedu.taskmanager.testutil.TestTask;

// TODO: reduce GUI tests by transferring some tests to be covered by lower level tests.
public class UpdateCommandTest extends TaskManagerGuiTest {

    // The list of tasks in the task list panel is expected to match this list.
    // This list is updated with every successful call to assertEditSuccess().
    TestTask[] expectedTasksList = td.getTypicalTasks();
    public TestTask updatedTask;

    // @@author A0141102H
    @Test
    public void update_allFieldsSpecified_success() throws Exception {
        String detailsToUpdate = "take a snack break ON 03/03/17 1500 TO 1600 CATEGORY";
        int taskManagerIndex = 1;

        updatedTask = new TaskBuilder().withTaskName("take a snack break").withStartDate("03/03/17")
                .withStartTime("1500").withEndDate("03/03/17").withEndTime("1600").build();

        assertUpdateSuccess(taskManagerIndex, taskManagerIndex, detailsToUpdate, updatedTask);
    }

    @Test
    public void update_notAllFieldsSpecified_success() throws Exception {
        String detailsToUpdate = "ON 31/03/17";
        int taskManagerIndex = 2;

        TestTask taskToUpdate = expectedTasksList[taskManagerIndex - 1];
        TestTask updatedTask = new TaskBuilder(taskToUpdate).withStartDate("31/03/17").withStartTime("0000")
                .withEndDate("31/03/17").withEndTime("2359").build();

        assertUpdateSuccess(taskManagerIndex, taskManagerIndex, detailsToUpdate, updatedTask);
    }
    /*
     * @Test public void update_clearCategories_success() throws Exception {
     * String detailsToUpdate = "t/"; int taskManagerIndex = 2;
     *
     * TestTask taskToUpdate = expectedTasksList[taskManagerIndex - 1]; TestTask
     * updatedTask = new TaskBuilder(taskToUpdate).withCategories().build();
     *
     * assertUpdateSuccess(taskManagerIndex, taskManagerIndex, detailsToUpdate,
     * updatedTask); }
     */

    @Test
    public void update_searchThenUpdate_success() throws Exception {
        commandBox.runCommand("SEARCH lunch");

        String detailsToUpdate = "Lunch BY 04/03/17 1400";
        int filteredTaskListIndex = 1;
        int taskManagerIndex = 2;

        TestTask taskToUpdate = expectedTasksList[taskManagerIndex - 1];
        TestTask updatedTask = new TaskBuilder(taskToUpdate).withTaskName("Lunch").build();

        assertUpdateSuccess(filteredTaskListIndex, taskManagerIndex, detailsToUpdate, updatedTask);
    }

    @Test
    public void update_missingTaskIndex_failure() {
        commandBox.runCommand("UPDATE supper");
        assertResultMessage(String.format(MESSAGE_INVALID_COMMAND_FORMAT, UpdateCommand.MESSAGE_USAGE));
    }

    @Test
    public void update_invalidTaskIndex_failure() {
        commandBox.runCommand("UPDATE 10 supper");
        assertResultMessage(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX);
    }

    // @Test
    // public void update_noFieldsSpecified_failure() {
    // commandBox.runCommand("UPDATE 1");
    // assertResultMessage(UpdateCommand.MESSAGE_NOT_UPDATED);
    // }

    @Test
    public void update_invalidValues_failure() {
        // commandBox.runCommand("UPDATE 1 *&");
        // assertResultMessage(TaskName.MESSAGE_TASKNAME_CONSTRAINTS);

        commandBox.runCommand("UPDATE 1 ON 030317");
        assertResultMessage(StartDate.MESSAGE_DATE_CONSTRAINTS);

        commandBox.runCommand("UPDATE 1 FROM thursday 1200hrs TO friday 1400");
        assertResultMessage(StartTime.MESSAGE_STARTTIME_CONSTRAINTS);

        commandBox.runCommand("UPDATE 1 FROM thursday 1200 TO friday 1300hrs");
        assertResultMessage(EndTime.MESSAGE_ENDTIME_CONSTRAINTS);

        // commandBox.runCommand("UPDATE 1 t/*&");
        // assertResultMessage(Category.MESSAGE_TAG_CONSTRAINTS);
    }

    @Test
    public void update_duplicateTask_failure() {
        commandBox.runCommand("UPDATE 3 Eat lunch at techno BY 04/03/17 1400");
        assertResultMessage(UpdateCommand.MESSAGE_DUPLICATE_TASK);
    }

    /**
     * Checks whether the updated task has the correct updated details.
     *
     * @param filteredTaskListIndex
     *            index of task to update in filtered list
     * @param taskManagerIndex
     *            index of task to update in the task manager. Must refer to the
     *            same task as {@code filteredTaskListIndex}
     * @param detailsToUpdate
     *            details to update the task with as input to the update command
     * @param updatedTask
     *            the expected task after updating the task's details
     */
    private void assertUpdateSuccess(int filteredTaskListIndex, int taskManagerIndex, String detailsToUpdate,
            TestTask updatedTask) {
        commandBox.runCommand("UPDATE " + filteredTaskListIndex + " " + detailsToUpdate);

        // confirm the new card contains the right data
        EventTaskCardHandle updatedCard = taskListPanel.navigateToTask(updatedTask.getTaskName().fullTaskName);
        assertMatching(updatedTask, updatedCard);

        // confirm the list now contains all previous tasks plus the task with
        // updated details
        expectedTasksList[taskManagerIndex - 1] = updatedTask;
        assertTrue(taskListPanel.isListMatching(expectedTasksList));
        assertResultMessage(String.format(UpdateCommand.MESSAGE_UPDATE_TASK_SUCCESS, updatedTask));
    }
}

@@ -2053,27 +2053,22 @@
   %7D%0A
-    /*%0A 
+%0A
    
- *
  @Test
+%0A   
  pub
@@ -2133,17 +2133,18 @@
  %7B%0A     
-*
+  
  String 
@@ -2166,12 +2166,26 @@
  = %22
-t/%22;
+CATEGORY%22;%0A       
  int
@@ -2210,26 +2210,20 @@
 = 2;%0A   
-  *%0A
     
- *
  TestTas
@@ -2279,16 +2279,24 @@
 ex - 1%5D;
+%0A       
  TestTas
@@ -2296,23 +2296,16 @@
 TestTask
-%0A     *
  updated
@@ -2367,29 +2367,24 @@
 uild();%0A
+%0A
      
-*%0A     *
+  
  assertU
@@ -2448,23 +2448,16 @@
 oUpdate,
-%0A     *
  updated
@@ -2466,18 +2466,14 @@
 sk);
- %7D
 %0A    
- */
+%7D
 %0A%0A  

Update JUnit V0.5