package seedu.task.logic.commands;

import java.io.IOException;
import java.util.logging.Logger;

import seedu.task.MainApp;
import seedu.task.alerts.ClearCommandAlert;
import seedu.task.commons.core.Config;
import seedu.task.commons.core.LogsCenter;
import seedu.task.commons.util.ConfigUtil;
import seedu.task.commons.util.StringUtil;
import seedu.task.model.TaskManager;
import seedu.task.alerts.ChangeDirectoryCommandAlert;

public class ChangeDirectoryCommand extends Command {

    public static final String COMMAND_WORD = "cd";

    public static final String MESSAGE_USAGE = COMMAND_WORD
            + ": Changes the location where the tasks in the Task Manager are saved. "
            + "Paramaters: cd {file location}/" + "Example: " + COMMAND_WORD + "  cd data/";

    public static final String MESSAGE_SUCCESS = "Directory Changed";
    public static final String MESSAGE_FAILURE = "Illegal directory name given, please use a different directory name and try again";

    private final String newDirectory;
    private Config newConfig = new Config();
    private String configFilePathUsed;
    private static final String DEFAULT_TASK_MANAGER_XML_FILE_NAME = "taskmanager.xml";
    private static final Logger logger = LogsCenter.getLogger(MainApp.class);

    /**
     * Convenience constructor using raw values.
     */
    public ChangeDirectoryCommand(String newDirectory) {
        newDirectory = newDirectory.trim();
        this.newDirectory = newDirectory;
    }

    @Override
    public CommandResult execute() {

        if (isValidDirectory(newDirectory)) {

            newConfig.setTaskManagerFilePath(directoryAddXmlExtension(newDirectory));
            configFilePathUsed = Config.DEFAULT_CONFIG_FILE;
            try {
                ConfigUtil.saveConfig(newConfig, configFilePathUsed);
                displayRestartAlert();
                return new CommandResult(MESSAGE_SUCCESS);

            } catch (IOException e) {
                logger.warning("Failed to save config file : " + StringUtil.getDetails(e));
                return new CommandResult(MESSAGE_FAILURE);
            }
        } else {
            return new CommandResult(MESSAGE_FAILURE);
        }

    }

    /*
     * Returns false if directory name contains any illegal characters
     */
    public static boolean isValidDirectory(String directory) {
        if (directory.contains("?")) {
            return false;
        }
        if (!String.valueOf(directory.charAt(directory.length() - 1)).equals("/")) {
            return false;
        }
        return true;
    }

    /*
     * append a "taskmanager.xml" string to the directory name
     */
    public String directoryAddXmlExtension(String directory) {
        return directory + DEFAULT_TASK_MANAGER_XML_FILE_NAME;
    }

    public void displayRestartAlert() {
        if (ChangeDirectoryCommandAlert.changeDirectoryCommand()) {
            System.exit(0);
        } else {
            System.exit(0);
        }

    }

}

@@ -44,27 +44,224 @@
 java.io.
-IOException
+BufferedReader;%0Aimport java.io.BufferedWriter;%0Aimport java.io.File;%0Aimport java.io.FileNotFoundException;%0Aimport java.io.FileReader;%0Aimport java.io.FileWriter;%0Aimport java.io.IOException;%0Aimport java.util.Map
 ;%0Aimport
@@ -560,24 +560,99 @@
 askManager;%0A
+import javafx.application.Application;%0Aimport javafx.application.Platform;%0A
 import seedu
@@ -1247,16 +1247,138 @@
 again%22;%0A
+    public static final String MESSAGE_ERROR = %22Error in creating new directory, please try again with another directory%22;
 %0A    pri
@@ -1658,16 +1658,68 @@
 .class);
+%0A    private boolean xmlDataTransferSuccess = false;
 %0A%0A    /*
@@ -2021,24 +2021,221 @@
 rectory)) %7B%0A
+            xmlDataTransferSuccess = transferXmlDataToNewFile(newDirectory);%0A        %7D else %7B%0A            return new CommandResult(MESSAGE_FAILURE);%0A        %7D%0A%0A        if (xmlDataTransferSuccess) %7B
 %0A           
@@ -2458,16 +2458,17 @@
 hUsed);%0A
+%0A
         
@@ -2643,16 +2643,20 @@
 to save 
+new 
 config f
@@ -2843,25 +2843,24 @@
 ;%0A        %7D%0A
-%0A
     %7D%0A%0A    /
@@ -2931,16 +2931,89 @@
 aracters
+ or if the%0A     * end of the directory does not contain the character %22/%22
 %0A     */
@@ -3710,10 +3710,1043 @@
 %0A    %7D%0A%0A
+    public boolean transferXmlDataToNewFile(String newDirectory) %7B%0A        try %7B%0A            String line;%0A            File createNewDirectory = new File(newDirectory);%0A            File createNewXmlFile = new File(newDirectory + DEFAULT_TASK_MANAGER_XML_FILE_NAME);%0A            BufferedReader reader = new BufferedReader(new FileReader(%22data/taskmanager.xml%22));%0A            createNewDirectory.mkdir();%0A            createNewXmlFile.createNewFile();%0A            BufferedWriter writer = new BufferedWriter(%0A                    new FileWriter(newDirectory + DEFAULT_TASK_MANAGER_XML_FILE_NAME));%0A            System.out.println(reader.toString() + %22readers%22 + newDirectory + %22newDir%22);%0A            while ((line = reader.readLine()) != null) %7B%0A                writer.write(line);%0A            %7D%0A            reader.close();%0A            writer.close();%0A            return true;%0A        %7D catch (IllegalStateException %7C IOException e) %7B%0A            System.out.println(%22The file could not be found%22);%0A            return false;%0A        %7D%0A%0A    %7D%0A%0A
 %7D%0A

Create a new Buffered Reader and Writer to transfer XML data from one file to another