package seedu.address.logic;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static seedu.address.commons.core.Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX;
import static seedu.address.commons.core.Messages.MESSAGE_UNKNOWN_COMMAND;

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

import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.HistoryCommand;
import seedu.address.logic.commands.ListCommand;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.UserPrefs;


public class LogicManagerTest {

    private Model model;
    private Logic logic;

    @Before
    public void setUp() {
        model = new ModelManager();
        logic = new LogicManager(model);
    }

    @Test
    public void execute_invalidCommandFormat_throwsParseException() {
        String invalidCommand = "uicfhmowqewca";
        assertParseException(invalidCommand, MESSAGE_UNKNOWN_COMMAND);
    }

    @Test
    public void execute_commandExecutionError_throwsCommandException() {
        String deleteCommand = "delete 9";
        assertCommandException(deleteCommand, MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
    }

    @Test
    public void execute_validCommand_success() {
        assertCommandSuccess(ListCommand.COMMAND_WORD, ListCommand.MESSAGE_SUCCESS, model);
    }

    @Test
    public void execute_verifyHistory_success() throws Exception {
        String validCommand = "clear";
        logic.execute(validCommand);

        String invalidCommandParse = "   adds   Bob   ";
        try {
            logic.execute(invalidCommandParse);
            fail("The expected ParseException was not thrown.");
        } catch (ParseException pe) {
            assertEquals(MESSAGE_UNKNOWN_COMMAND, pe.getMessage());
        }

        String invalidCommandExecute = "delete 1"; // address book is of size 0; index out of bounds
        try {
            logic.execute(invalidCommandExecute);
            fail("The expected CommandException was not thrown.");
        } catch (CommandException ce) {
            assertEquals(MESSAGE_INVALID_PERSON_DISPLAYED_INDEX, ce.getMessage());
        }

        String expectedMessage = String.format(HistoryCommand.MESSAGE_SUCCESS,
                String.join("\n", validCommand, invalidCommandParse, invalidCommandExecute));
        assertCommandSuccess("history", expectedMessage, model);
    }

    /**
     * Executes the command, confirms that no exceptions are thrown and that the result message is correct.
     * Also confirms that {@code expectedModel} is as specified.
     * @see #assertCommandBehavior(Class, String, String, Model)
     */
    private void assertCommandSuccess(String inputCommand, String expectedMessage, Model expectedModel) {
        assertCommandBehavior(null, inputCommand, expectedMessage, expectedModel);
    }

    /**
     * Executes the command, confirms that a ParseException is thrown and that the result message is correct.
     * @see #assertCommandBehavior(Class, String, String, Model)
     */
    private void assertParseException(String inputCommand, String expectedMessage) {
        assertCommandFailure(inputCommand, ParseException.class, expectedMessage);
    }

    /**
     * Executes the command, confirms that a CommandException is thrown and that the result message is correct.
     * @see #assertCommandBehavior(Class, String, String, Model)
     */
    private void assertCommandException(String inputCommand, String expectedMessage) {
        assertCommandFailure(inputCommand, CommandException.class, expectedMessage);
    }

    /**
     * Executes the command, confirms that the exception is thrown and that the result message is correct.
     * @see #assertCommandBehavior(Class, String, String, Model)
     */
    private void assertCommandFailure(String inputCommand, Class<?> expectedException, String expectedMessage) {
        Model expectedModel = new ModelManager(model.getAddressBook(), new UserPrefs());
        assertCommandBehavior(expectedException, inputCommand, expectedMessage, expectedModel);
    }

    /**
     * Executes the command, confirms that the result message is correct and that the expected exception is thrown,
     * and also confirms that the following two parts of the LogicManager object's state are as expected:<br>
     *      - the internal model manager data are same as those in the {@code expectedModel} <br>
     *      - {@code expectedModel}'s address book was saved to the storage file.
     */
    private void assertCommandBehavior(Class<?> expectedException, String inputCommand,
                                           String expectedMessage, Model expectedModel) {

        try {
            CommandResult result = logic.execute(inputCommand);
            assertEquals(expectedException, null);
            assertEquals(expectedMessage, result.feedbackToUser);
        } catch (CommandException | ParseException e) {
            assertEquals(expectedException, e.getClass());
            assertEquals(expectedMessage, e.getMessage());
        }

        assertEquals(expectedModel, model);
    }
}

@@ -72,45 +72,8 @@
 ls;%0A
-import static org.junit.Assert.fail;%0A
 impo
@@ -951,32 +951,49 @@
 ParseException()
+ throws Exception
  %7B%0A        Strin
@@ -1099,16 +1099,62 @@
 MMAND);%0A
+        assertHistoryCorrect(invalidCommand);%0A
     %7D%0A%0A 
@@ -1232,16 +1232,33 @@
 eption()
+ throws Exception
  %7B%0A     
@@ -1390,159 +1390,46 @@
     
-%7D%0A%0A    @Test%0A    public void execute_validCommand_success() %7B%0A        assertCommandSuccess(ListCommand.COMMAND_WORD, ListCommand.MESSAGE_SUCCESS, model
+    assertHistoryCorrect(deleteCommand
 );%0A 
@@ -1473,20 +1473,19 @@
 te_v
-erifyHistory
+alidCommand
 _suc
@@ -1529,972 +1529,167 @@
 ing 
-validCommand = %22clear%22;%0A        logic.execute(validCommand);%0A%0A        String invalidCommandParse = %22   adds   Bob   %22;%0A        try %7B%0A            logic.execute(invalidCommandParse);%0A            fail(%22The expected ParseException was not thrown.%22);%0A        %7D catch (ParseException pe) %7B%0A            assertEquals(MESSAGE_UNKNOWN_COMMAND, pe.getMessage());%0A        %7D%0A%0A        String invalidCommandExecute = %22delete 1%22; // address book is of size 0; index out of bounds%0A        try %7B%0A            logic.execute(invalidCommandExecute);%0A            fail(%22The expected CommandException was not thrown.%22);%0A        %7D catch (CommandException ce) %7B%0A            assertEquals(MESSAGE_INVALID_PERSON_DISPLAYED_INDEX, ce.getMessage());%0A        %7D%0A%0A        String expectedMessage = String.format(HistoryCommand.MESSAGE_SUCCESS,%0A                String.join(%22%5Cn%22, validCommand, invalidCommandParse, invalidCommandExecute));%0A        assertCommandSuccess(%22history%22, expectedMessage, model
+listCommand = ListCommand.COMMAND_WORD;%0A        assertCommandSuccess(listCommand, ListCommand.MESSAGE_SUCCESS, model);%0A        assertHistoryCorrect(listCommand
 );%0A 
@@ -4405,11 +4405,373 @@
 );%0A    %7D
+%0A%0A    private void assertHistoryCorrect(String... expectedCommands) throws Exception %7B%0A        CommandResult result = logic.execute(HistoryCommand.COMMAND_WORD);%0A        String expectedMessage = String.format(HistoryCommand.MESSAGE_SUCCESS,%0A                String.join(%22%5Cn%22, expectedCommands));%0A        assertEquals(expectedMessage, result.feedbackToUser);%0A    %7D
 %0A%7D%0A

Merge LogicManagerTest#execute_verifyHistory_success()