package linenux.command;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.time.LocalDateTime;
import java.util.ArrayList;

import org.junit.Before;
import org.junit.Test;

import linenux.command.result.CommandResult;
import linenux.model.Reminder;
import linenux.model.Schedule;
import linenux.model.Task;

/**
 * JUnit test for list command.
 */
public class ViewCommandTest {
    private Schedule schedule;
    private ViewCommand viewCommand;

    @Before
    public void setupListCommand() {
        this.schedule = new Schedule();
        this.viewCommand = new ViewCommand(this.schedule);
    }

    public void setupTaskWithAndWithoutReminders() {
        this.schedule.addTask(new Task("Task"));
        this.schedule.addTask(new Task("Found"));
        ArrayList<Task> tasks = this.schedule.getTaskList();
        ArrayList<Reminder> reminders = tasks.get(1).getReminders();
        reminders.add(new Reminder("Attend Workshop 1", LocalDateTime.of(2016, 1, 1, 17, 0)));
        reminders.add(new Reminder("Attend Workshop 2", LocalDateTime.of(2016, 2, 1, 17, 0)));
        reminders.add(new Reminder("Attend Workshop 3", LocalDateTime.of(2016, 3, 1, 17, 0)));
    }

    public CommandResult setupMultipleHelloTaskAndExecuteAmbiguousCommand() {
        this.schedule.addTask(new Task("hello it's me"));
        this.schedule.addTask(new Task("hello from the other side"));
        return this.viewCommand.execute("view hello");
    }

    @Test
    public void testRespondToViewWithKeywords() {
        assertTrue(this.viewCommand.respondTo("view keyword"));
    }

    @Test
    public void testCaseInsensitiveRespondToView() {
        assertTrue(this.viewCommand.respondTo("ViEw keyword"));
    }

    @Test
    public void testDoesNotRespondToOtherCommands() {
        assertFalse(this.viewCommand.respondTo("notview"));
    }

    @Test
    public void testCommandResultWhenNoMatchFound() {
        this.schedule.addTask(new Task("asdjkahsdkjhasjdkh"));
        CommandResult result = this.viewCommand.execute("view that nasty todo");
        assertEquals("Cannot find \"that nasty todo\".", result.getFeedback());
    }

    @Test
    public void testCommandResultWhenExactlyOneTaskWithNoRemindersFound() {
        this.setupTaskWithAndWithoutReminders();
        CommandResult result = this.viewCommand.execute("view Task");
        assertEquals("Task" + '\n' + "Reminders:" + '\n' + "There are no reminders found!", result.getFeedback());
    }

    @Test
    public void testCommandResultWhenExactlyOneTaskWithRemindersFound() {
        this.setupTaskWithAndWithoutReminders();
        CommandResult result = this.viewCommand.execute("view Found");
        assertEquals("Found\n" + "Reminders:\n"
            + "1. Attend Workshop 1 (On 2016-01-01 5:00PM)\n"
            + "2. Attend Workshop 2 (On 2016-02-01 5:00PM)\n"
            + "3. Attend Workshop 3 (On 2016-03-01 5:00PM)", result.getFeedback());
    }

    @Test
    public void testCommandResultWhenMultipleMatchesFound() {
        CommandResult result = this.setupMultipleHelloTaskAndExecuteAmbiguousCommand();
        assertEquals("Which one? (1-2)\n1. hello it's me\n2. hello from the other side", result.getFeedback());
    }
}

@@ -3296,11 +3296,1781 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void testAwaitingUserResponse() %7B%0A        assertFalse(this.viewCommand.awaitingUserResponse());%0A        this.setupMultipleHelloTaskAndExecuteAmbiguousCommand();%0A        assertTrue(this.viewCommand.awaitingUserResponse());%0A    %7D%0A%0A    @Test%0A    public void testUserResponseCancel() %7B%0A        this.setupMultipleHelloTaskAndExecuteAmbiguousCommand();%0A        CommandResult result = this.viewCommand.userResponse(%22cancel%22);%0A        assertEquals(%22OK! Not viewing any task.%22, result.getFeedback());%0A        assertFalse(this.viewCommand.awaitingUserResponse());%0A    %7D%0A%0A    @Test%0A    public void testUserResponseValidIndex() %7B%0A        this.setupMultipleHelloTaskAndExecuteAmbiguousCommand();%0A        CommandResult result = this.viewCommand.userResponse(%221%22);%0A        assertEquals(%22hello it's me%5CnReminders:%5CnThere are no reminders found!%22, result.getFeedback());%0A    %7D%0A%0A    @Test%0A    public void testUserResponseInvalidIndex() %7B%0A        this.setupMultipleHelloTaskAndExecuteAmbiguousCommand();%0A        CommandResult result = this.viewCommand.userResponse(%223%22);%0A        String expectedResponse = %22That's not a valid index. Enter a number between 1 and 2:%5Cn%22 +%0A                %221. hello it's me%5Cn2. hello from the other side%22;%0A        assertEquals(expectedResponse, result.getFeedback());%0A    %7D%0A%0A    @Test%0A    public void testUserResponseInvalidResponse() %7B%0A        this.setupMultipleHelloTaskAndExecuteAmbiguousCommand();%0A        CommandResult result = this.viewCommand.userResponse(%22notindex%22);%0A        String expectedResponse = %22I don't understand %5C%22notindex%5C%22.%5Cn%22 + %22Enter a number to indicate which task to delete.%5Cn%22 +%0A                %221. hello it's me%5Cn2. hello from the other side%22;%0A        assertEquals(expectedResponse, result.getFeedback());%0A    %7D
 %0A%7D%0A

Add tests for userResponse