package seedu.taskmanager.logic.commands;

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

import seedu.taskmanager.commons.core.Config;
import seedu.taskmanager.commons.core.LogsCenter;
import seedu.taskmanager.commons.exceptions.DataConversionException;
import seedu.taskmanager.commons.util.ConfigUtil;
import seedu.taskmanager.commons.util.StringUtil;
import seedu.taskmanager.model.ReadOnlyTaskManager;
import seedu.taskmanager.model.TaskManager;
import seedu.taskmanager.model.item.ItemType;
import seedu.taskmanager.storage.StorageManager;

/**
 * Saves the program data file at the specified location
 */
public class SaveAsCommand extends Command {
    public static final String COMMAND_WORD = "saveAs";
    
    public static final String MESSAGE_ERROR_CONVERTING_FILE = "Error reading from config file: " + Config.DEFAULT_CONFIG_FILE;
    public static final String MESSAGE_SUCCESS = "File path changed! Custom file path specified: %1$s";
    public static final String MESSAGE_SAME_FILE_PATH = "File path is already saved at the specified location!";
    public static final String MESSAGE_ERROR_SAVING_FILE = "Error occured saving to file.";
    public static final String MESSAGE_USAGE = COMMAND_WORD + ": Saves task manager information to the specified storage file path. \n"
            + "Parameters: " + COMMAND_WORD + " FILEPATH \n"
            + "Example: " + COMMAND_WORD +  " " + " data/newtaskbook.xml" + "\n"
            + "Note: file path is limited to .xml format";
//            + "Note: " + COMMAND_WORD + " can be replaced by " + SHORT_COMMAND_WORD + "\n"
    
    private static final Logger logger = LogsCenter.getLogger(SaveAsCommand.class);

    private String newTaskManagerFilePath;
    
    // CONSTRUCTOR
    public SaveAsCommand(String newTaskManagerFilePath) {
        this.newTaskManagerFilePath = newTaskManagerFilePath;
    }
    
    @Override
    public CommandResult execute() {
        String defaultConfigFilePath = Config.DEFAULT_CONFIG_FILE;
        
        try {
            Config currentConfig = ConfigUtil.readConfig(defaultConfigFilePath).orElse(new Config());

            String previousFilePath = currentConfig.getTaskManagerFilePath();
            
            currentConfig.setTaskManagerFilePath(newTaskManagerFilePath);
            ConfigUtil.saveConfig(currentConfig, newTaskManagerFilePath);
            
            StorageManager previousStorage = new StorageManager(previousFilePath, currentConfig.getUserPrefsFilePath());
            StorageManager newStorage = new StorageManager(newTaskManagerFilePath, currentConfig.getUserPrefsFilePath());
            
            ReadOnlyTaskManager previousTaskManager = previousStorage.readTaskManager().orElse(new TaskManager());
            newStorage.saveTaskManager(previousTaskManager);
            

            logger.info(currentConfig.toString());
            logger.info(String.format("PRINT THIS", newTaskManagerFilePath));

            
            return new CommandResult(String.format(MESSAGE_SUCCESS, newStorage.getTaskManagerFilePath()));
            
        } catch (DataConversionException e) {
            return new CommandResult(MESSAGE_ERROR_CONVERTING_FILE);
        } catch (IOException e) {
            return new CommandResult(MESSAGE_ERROR_SAVING_FILE);
        }
    }
    
// need to use Config class to access config.json file and edit accordingly
// test to check where the file is going to be saved by using Logger.info(<configobject>.toString());
// need to edit Storage.saveUserPrefs()
// probably need to use storage manager
// Model raises a TaskManagerChangedEvent when the Address Book data are changed, instead of asking
// *  Storage to save the updates to the hard disk.
// *  EventsCenter calls handleTaskManagerChangedEvent() to save the file using Storage class
// Model stores the TaskManager data
// * 
// Storage component can save UserPref objects in json format and read it back
// * can save TaskManager data in xml format and read it back
// Certain properties of the application can be controlled (e.g App name, logging level)
// * through the configuration file (default: config.json)
}

@@ -1757,32 +1757,9 @@
 th;%0A
-    %0A    // CONSTRUCTOR
 %0A
+
     
@@ -2829,152 +2829,8 @@
     
-%0A%0A            logger.info(currentConfig.toString());%0A            logger.info(String.format(%22PRINT THIS%22, newTaskManagerFilePath));%0A%0A            
 %0A   
@@ -3174,853 +3174,12 @@
   %7D%0A
+
     %7D%0A
-    %0A// need to use Config class to access config.json file and edit accordingly%0A// test to check where the file is going to be saved by using Logger.info(%3Cconfigobject%3E.toString());%0A// need to edit Storage.saveUserPrefs()%0A// probably need to use storage manager%0A// Model raises a TaskManagerChangedEvent when the Address Book data are changed, instead of asking%0A// *  Storage to save the updates to the hard disk.%0A// *  EventsCenter calls handleTaskManagerChangedEvent() to save the file using Storage class%0A// Model stores the TaskManager data%0A// * %0A// Storage component can save UserPref objects in json format and read it back%0A// * can save TaskManager data in xml format and read it back%0A// Certain properties of the application can be controlled (e.g App name, logging level)%0A// * through the configuration file (default: config.json)%0A
 %7D%0A

Update SaveAsCommand