package website.automate.jwebrobot.loader;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.IOFileFilter;
import org.apache.commons.io.filefilter.TrueFileFilter;
import org.apache.commons.io.filefilter.WildcardFileFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import website.automate.jwebrobot.exceptions.NonReadableFileException;
import website.automate.waml.io.model.main.Scenario;
import website.automate.waml.io.reader.WamlReader;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Service
public class PatternScenarioLoader implements ScenarioLoader {

    private static final IOFileFilter SCENARIO_FORMAT_FILTER = new WildcardFileFilter(new String[] {"**.yaml", "**.json"});

    private static final Logger LOG = LoggerFactory.getLogger(PatternScenarioLoader.class);

    private final WamlReader wamlReader;

    @Autowired
    public PatternScenarioLoader(WamlReader wamlReader){
        this.wamlReader = wamlReader;
    }

    @Override
    public List<ScenarioFile> load(Collection<String> scenarioPaths, String reportPath){
        List<ScenarioFile> scenarioFiles = new ArrayList<>();
        
        for(String scenarioPath : scenarioPaths){
            scenarioFiles.addAll(load(scenarioPath, reportPath));
        }
        
        return scenarioFiles;
    }
    
    private List<ScenarioFile> load(String scenarioPath, String reportPath){
        String currentPath = scenarioPath;
        List<ScenarioFile> loadedScenarioFiles = new ArrayList<>();

        try {
            File baseScenarioFile = new File(scenarioPath);
            File reportFile = new File(reportPath);
            String reportCanonicalPath = reportFile.exists() ? reportFile.getCanonicalPath() : null;
            
            if(baseScenarioFile.canRead()){
                if(baseScenarioFile.isDirectory()){
                    Collection<File> scenarioFiles = FileUtils.listFiles(baseScenarioFile, SCENARIO_FORMAT_FILTER, TrueFileFilter.INSTANCE);
                    for(File scenarioFile : scenarioFiles){
                        currentPath = scenarioFile.getAbsolutePath();
                        addScenarioFile(reportCanonicalPath, scenarioFile, loadedScenarioFiles);
                    }
                } else {
                    addScenarioFile(reportCanonicalPath, baseScenarioFile, loadedScenarioFiles);
                }
            } else {
                throw new NonReadableFileException(currentPath);
            }
        } catch (IOException e) {
            throw new NonReadableFileException(currentPath, e);
        }

        return loadedScenarioFiles;
    }

    private void addScenarioFile(String reportCanonicalPath, File scenarioFile, List<ScenarioFile> loadedScenarioFiles) throws IOException{
        if(!scenarioFile.getCanonicalPath().equals(reportCanonicalPath)){
           loadedScenarioFiles.add(new ScenarioFile(load(scenarioFile), scenarioFile));
        }
    }
    
    private Scenario load(File scenarioFile) throws FileNotFoundException{
        LOG.debug(MessageFormat.format("Reading scenario file {0} ...", scenarioFile.getAbsolutePath()));
        return wamlReader.read(scenarioFile);
    }
}

@@ -630,89 +630,19 @@
 .io.
-FileNotFoundException;%0Aimport java.io.IOException;%0Aimport java.text.MessageFormat
+IOException
 ;%0Aim
@@ -720,16 +720,63 @@
 .List;%0A%0A
+import static java.text.MessageFormat.format;%0A%0A
 @Service
@@ -840,16 +840,111 @@
 ader %7B%0A%0A
+    private static final String TEMPLATE_SCENARIO_LOAD_FAIL_LOG_MESSAGE = %22error: %7B0%7D %3E %7B1%7D%22;%0A%0A
     priv
@@ -1054,12 +1054,11 @@
 %22**.
-json
+yml
 %22%7D);
@@ -1094,16 +1094,19 @@
 gger LOG
+GER
  = Logge
@@ -3343,70 +3343,49 @@
 le) 
-throws FileNotFoundException%7B%0A        LOG.debug(MessageFormat.
+%7B%0A        try %7B%0A            LOGGER.debug(
 form
@@ -3458,24 +3458,28 @@
 ));%0A        
+    
 return wamlR
@@ -3500,20 +3500,242 @@
 narioFile);%0A
+        %7D catch (Exception e)%7B%0A            LOGGER.error(format(TEMPLATE_SCENARIO_LOAD_FAIL_LOG_MESSAGE,%0A                scenarioFile.getName(),%0A                %22Failed to parse scenario.%22));%0A            throw e;%0A        %7D%0A
     %7D%0A%7D%0A

Add scenario parsing log message