package seedu.address.logic.commands;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import seedu.address.commons.core.Config;
import seedu.address.commons.exceptions.IllegalValueException;
import seedu.address.model.task.Task;

public class SaveCommand extends Command {

	
	public static final String COMMAND_WORD = "save";

    public static final String MESSAGE_USAGE = COMMAND_WORD + ": specify location of folder to save data in.\n"
            + "Parameters: DATA_STORAGE_FOLDER_PATH"
            + "Example: " + COMMAND_WORD
            + "/Users/jim/GitHub/main/data";

    public static final String MESSAGE_SUCCESS = "New file location saved.";
    public static final String MESSAGE_FOLDER_DOES_NOT_EXIST = "This folder currently does not exist, new folder created.";
    
    private final String dirPath;

    /**
	 * Constructor
	 *
	 * @throws IllegalValueException
	 *             if any of the raw values are invalid
	 */
	public SaveCommand(String dirPath) {
		this.dirPath = dirPath;
	}
	
	@Override
	public CommandResult execute() {
		
		
		changeConfigPaths(dirPath);
		
		return new CommandResult(MESSAGE_SUCCESS);
	}

	
	
	private void copyDataFilesToNewPath(String arguments) {

		File dataPath = new File(arguments+"task.xml");
		File userPrefPath = new File(arguments+"preferences.json");
	}
	/**
     * change the path fields in the config.json file
     * @param arguments
     */
    private void changeConfigPaths(String arguments) {
    	FileReader configFileReader = null;
    	FileWriter configFileWriter = null;
		try {
			configFileReader = new FileReader(Config.DEFAULT_CONFIG_FILE);
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}
    	String currLine = "";
    	String newConfig = "";
    	
    	try {
    		BufferedReader br = new BufferedReader(configFileReader);
    		while ((currLine = br.readLine()) != null) {
    			if (currLine.contains("userPrefsFilePath")) {
        			currLine = currLine.replaceAll(currLine.substring(25, currLine.length()-2), (arguments + "preferences.json"));
    			} else if (currLine.contains("taskBookFilePath")) {
        			currLine = currLine.replaceAll(currLine.substring(24, currLine.length()-2), (arguments + "task.xml"));
    			}
    			if (!currLine.contains("}")) {
    				currLine = currLine + "\n";
    			}
    			newConfig += currLine;
    		}
    		br.close();
    	} catch (IOException e3){
    		e3.printStackTrace();
    	}

		try {
			configFileWriter = new FileWriter(Config.DEFAULT_CONFIG_FILE, false);
		} catch (IOException e2) {
			e2.printStackTrace();
		}
		
		BufferedWriter bw = new BufferedWriter(configFileWriter);
		try {
			bw.write(newConfig);
			bw.close();
		} catch (IOException e4) {
			e4.printStackTrace();
		}
    		
    }

}

@@ -850,89 +850,197 @@
 AGE_
-FOLDER_DOES_NOT_EXIST = %22This folder currently does not exist, new folder created
+PATH_IS_NOT_A_DIRECTORY = %22The path given does not refer to a folder.%22;%0A    public static final String MESSAGE_FOLDER_CANNOT_BE_CREATED = %22A new folder cannot be created with the given path
 .%22;%0A
@@ -1315,19 +1315,321 @@
 e() %7B%0A%09%09
-%0A%09%09
+File f = new File(dirPath);%0A%09%09if (!f.exists()) %7B%0A%09%09%09try %7B%0A%09%09%09%09f.mkdirs();%0A%09%09%09%7D catch (SecurityException e) %7B%0A%09%09%09%09return new CommandResult(MESSAGE_FOLDER_CANNOT_BE_CREATED);%0A%09%09%09%7D%0A%09%09%7D%0A%09%09if (!f.isDirectory())%0A%09%09%09return new CommandResult(MESSAGE_PATH_IS_NOT_A_DIRECTORY);%0A%09%09%0A%09%09copyDataFilesToNewPath(dirPath);
 %0A%09%09chang
@@ -1651,19 +1651,16 @@
 rPath);%0A
-%09%09%0A
 %09%09return
@@ -1765,17 +1765,182 @@
 ) %7B%0A
-%0A%09%09File d
+%09%09Config config = new Config();%0A%0A%09%09File oldDataPath = new File(config.getAddressBookFilePath());%0A%09%09File oldUserPrefPath = new File(config.getUserPrefsFilePath());%0A%09%09File newD
 ataP
@@ -1984,17 +1984,20 @@
 %0A%09%09File 
-u
+newU
 serPrefP
@@ -2043,17 +2043,101 @@
 son%22);%0A%09
-%7D
+%09oldDataPath.renameTo(newDataPath);%0A%09%09oldUserPrefPath.renameTo(newUserPrefPath);%0A%09%7D%0A%09
 %0A%09/**%0A  

Add SaveCommand.java