package seedu.tasklist.logic.commands;

import java.io.IOException;
import java.nio.file.InvalidPathException;
import java.nio.file.Paths;

public class SaveCommand extends Command {
    
    public static final String COMMAND_WORD = "save";
    
    public static final String MESSAGE_USAGE = COMMAND_WORD + ": Change the directory and file name of the data that is being saved.\n"
            + "Parameters: FILE_PATH/FILE_NAME.xml\n"
            + "Example: " + COMMAND_WORD + " data/mytasklist.xml";
    
    public static final String MESSAGE_SUCCESS = "File is successfully saved to: %1$s";
    public static final String MESSAGE_INVALID_PATH = "Invalid file path: %1$s";
    public static final String MESSAGE_FAILURE = "Unable to save file at specified path: %1$s";
    
    private final String filePath;
    private static final String XML = ".xml";
    
    public SaveCommand(String filePath){
        this.filePath = filePath;
    }
    
    @Override
    public CommandResult execute() {
        assert filePath != null; 
        assert model != null;
        
        if (!isValidPath(filePath)) {
            return new CommandResult(String.format(MESSAGE_INVALID_PATH, filePath));
        }

        try{
            model.saveTaskList(filePath);
            return new CommandResult(String.format(MESSAGE_SUCCESS, filePath));
        }catch (IOException e){
            return new CommandResult(String.format(MESSAGE_FAILURE, filePath));
        }
        
    }
    

    /**
     * Returns true if the given path is a valid path
     */
    private boolean isValidPath(String path) {
        try {
            Paths.get(path);
            return isXML(path);
        } catch (InvalidPathException e) {
            return false;
        }
    }

    /**
     * Returns true if the given path ends with the .xml extension.
     */
    private static boolean isXML(String path) {
        return path.endsWith(XML);
    }


}

@@ -176,21 +176,17 @@
 mmand %7B%0A
-    
 %0A
+
     publ
@@ -231,20 +231,16 @@
 %22save%22;%0A
-    
 %0A    pub
@@ -287,24 +287,36 @@
 COMMAND_WORD
+%0A           
  + %22: Change
@@ -497,29 +497,25 @@
 klist.xml%22;%0A
-    
 %0A
+
     public s
@@ -767,21 +767,17 @@
  %251$s%22;%0A
-    
 %0A
+
     priv
@@ -849,20 +849,16 @@
 %22.xml%22;%0A
-    
 %0A    pub
@@ -889,16 +889,17 @@
 ilePath)
+ 
 %7B%0A      
@@ -932,21 +932,17 @@
 ;%0A    %7D%0A
-    
 %0A
+
     @Ove
@@ -1016,17 +1016,16 @@
 != null;
- 
 %0A       
@@ -1047,25 +1047,17 @@
 = null;%0A
-        
 %0A
+
         
@@ -1193,16 +1193,17 @@
      try
+ 
 %7B%0A      
@@ -1327,16 +1327,17 @@
        %7D
+ 
 catch (I
@@ -1349,16 +1349,17 @@
 ption e)
+ 
 %7B%0A      
@@ -1446,27 +1446,15 @@
   %7D%0A
-        
 %0A
+
     %7D%0A
-    
 %0A%0A  

correct checkstyle errors