package seedu.jimi.logic.commands;

import java.io.IOException;

import seedu.jimi.commons.core.Config;
import seedu.jimi.commons.exceptions.DataConversionException;
import seedu.jimi.commons.util.ConfigUtil;
import seedu.jimi.model.ReadOnlyTaskBook;
import seedu.jimi.model.TaskBook;
import seedu.jimi.storage.StorageManager;

/**
 *  
 * @author Wei Yin 
 * 
 * Sets save directory for the tasks and events in Jimi.
 */
public class SaveAsCommand extends Command {

    public static final String COMMAND_WORD = "saveas";

    public static final String MESSAGE_USAGE = 
            COMMAND_WORD + ": Set a new save directory for all your tasks and events in Jimi.\n"
            + "Parameters: FILEPATH/FILENAME.xml \n"
            + "Example: " + COMMAND_WORD + " C:/dropbox/taskbook.xml";

    public static final String MESSAGE_SUCCESS = "Save directory changed: %1$s";
    public static final String MESSAGE_CONFIG_FILE_NOT_FOUND = "Config file is not found. ";
    public static final String MESSAGE_UPDATING_SAVE_DIR = "There is an error updating the new save directory.";
    public static final String MESSAGE_DUPLICATE_SAVE_DIRECTORY = "New save directory is the same as the old save directory.";
    
    
    private String taskBookFilePath;
    
    private static String configFilePath = Config.DEFAULT_CONFIG_FILE;

    /**
     * Empty constructor for stub usage
     */
    public SaveAsCommand() {}
    
    /**
     * Convenience constructor using raw values.
     */
    public SaveAsCommand(String filePath)  {
        this.taskBookFilePath = filePath;
    }
    
    public static void setConfigFilePath(String newConfigFilePath) {
        configFilePath = newConfigFilePath;
    }
    
    @Override
    public CommandResult execute() {
        try {
            Config config = ConfigUtil.readConfig(configFilePath).orElse(new Config());
            
            String oldTaskBookFilePath = config.getTaskBookFilePath();
            if (oldTaskBookFilePath.equals(taskBookFilePath)) {
                indicateAttemptToExecuteIncorrectCommand();
                return new CommandResult(String.format(MESSAGE_DUPLICATE_SAVE_DIRECTORY));
            }
            config.setTaskBookFilePath(taskBookFilePath);
            
            ConfigUtil.saveConfig(config, configFilePath);
            
            StorageManager oldStorage = new StorageManager(oldTaskBookFilePath, config.getUserPrefsFilePath());
            StorageManager newStorage = new StorageManager(taskBookFilePath, config.getUserPrefsFilePath());
            
            ReadOnlyTaskBook oldTaskBook = oldStorage.readTaskBook(oldTaskBookFilePath).orElse(new TaskBook());
            
            newStorage.saveTaskBook(oldTaskBook);
            
            return new CommandResult(String.format(MESSAGE_SUCCESS, config.getTaskBookFilePath()));
        } catch (DataConversionException e) {
            return new CommandResult(String.format(MESSAGE_CONFIG_FILE_NOT_FOUND));
        } catch (IOException e) {
            return new CommandResult(String.format(MESSAGE_UPDATING_SAVE_DIR));
        }
    }

    @Override
    public boolean isValidCommandWord(String commandWord) {
        return commandWord.equals(COMMAND_WORD);
    }
}

@@ -711,16 +711,208 @@
 NAME.xml
+ or FILEPATH%5C%5CFILENAME.xml %5Cn%22%0A            + %22Jimi rejects these characters in file paths: * & %25 ? : %3Cspace%3E %5C%22 %3C %3E . %5Cn%22%0A            + %22and these characters in file names: / : * %5C%22 %3C %3E ? %7C %5C%5C
  %5Cn%22%0A   
@@ -1158,121 +1158,8 @@
  %22;%0A
-    public static final String MESSAGE_UPDATING_SAVE_DIR = %22There is an error updating the new save directory.%22;%0A
     
@@ -3133,33 +3133,22 @@
 mat(
-MESSAGE_UPDATING_SAVE_DIR
+e.getMessage()
 ));%0A

update usage message