package harmony.mastermind.logic.commands;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;


//@@author A0124797R
/**
 * Imports a txt file of commands to add tasks automatically
 */
public class ImportCommand extends Command {
    public static final String COMMAND_WORD = "import";

    public static final String MESSAGE_USAGE = COMMAND_WORD
                                               + ": Reads file and add all task from file into Mastermind\n"
                                               + "Parameters: File location\n"
                                               + "Example: "
                                               + COMMAND_WORD
                                               + " Users/newUser/Desktop/file.txt";

    public static final String MESSAGE_READ_SUCCESS = "Read success on imported file";
    public static final String MESSAGE_READ_FAILURE = "Invalid file path: %1$s";
    public static final String MESSAGE_IMPORT_SUCCESS = "Import success %1$s tasks added";
    public static final String MESSAGE_IMPORT_FAILURE = "Import failure: %1$s tasks added \nInvalid lines: %2$s";
    
    private String fileToImport;
    private ArrayList<String> lstOfCmd;
    
    public ImportCommand(String filePath) {
        this.fileToImport = filePath.trim();
        lstOfCmd = new ArrayList<String>();
    }
    
    @Override
    public CommandResult execute() {
        assert fileToImport != null;
        assert lstOfCmd != null;

        try {
            BufferedReader br = model.importFile(fileToImport);
            
            String line;
            
            while ((line = br.readLine()) != null) {
                lstOfCmd.add(line);
            }
            
        } catch (IOException e) {
            return new CommandResult(COMMAND_WORD, String.format(MESSAGE_READ_FAILURE, fileToImport));
        }
        
        return new CommandResult(COMMAND_WORD , MESSAGE_READ_SUCCESS);
    }
    
    /** gets the list of commands for adding tasks */
    public ArrayList<String> getTaskToAdd() {
        return this.lstOfCmd;
    }

}

@@ -83,27 +83,76 @@
 java.io.
-IOException
+File;%0Aimport java.io.IOException;%0Aimport java.nio.file.Files
 ;%0Aimport
@@ -1543,16 +1543,454 @@
 != null;
+%0A        %0A        File file = new File(fileToImport);%0A        try  %0A        %7B  %0A           String fileType = Files.probeContentType(file.toPath());  %0A           System.out.println(fileType);%0A        %7D  %0A        catch (IOException ioException)  %0A        %7B  %0A           System.out.println(  %0A                %22ERROR: Unable to determine file type for %22 + fileToImport  %0A                   + %22 due to exception %22 + ioException);  %0A        %7D  
 %0A%0A      

update import