package org.scify.memori;

import org.scify.memori.fx.FXAudioEngine;
import org.scify.memori.helper.MemoriConfiguration;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * Responsible for locating and retrieving project resource files
 */
public class ResourceLocator {

    private String rootDataPath;
    private String rootDataPathDefault;

    public ResourceLocator() {
        MemoriConfiguration configuration = new MemoriConfiguration();
        this.rootDataPath = "/" + configuration.getProjectProperty("DATA_PACKAGE") + "/";
        this.rootDataPathDefault = "/" + configuration.getProjectProperty("DATA_PACKAGE_DEFAULT") + "/";
    }

    /**
     * Given a path that represents a resource, tries to find if the resource is available in the current data pack
     *
     * @param filePath the path of the desired file
     * @param fileName the name of the desired file
     * @return the resource path in which the file is available (current data pack or default data pack)
     */
    public String getCorrectPathForFile(String filePath, String fileName) {
        String file = this.rootDataPath + filePath + fileName;
        URL fileURL = FXAudioEngine.class.getResource(file);
        if (fileURL == null) {
            file = this.rootDataPathDefault + filePath + fileName;
            System.out.println("Loaded default: " + file);
        }
        return file;
    }


    /**
     * Gets the content files and directories names of a resource path
     *
     * @param dirPath the desired path name
     * @return a set of names of the paths
     */
    public List<String> getResourcesFromDirectory(String dirPath) {
        try {
            List<String> filenames = new ArrayList<>();

            try (
                    InputStream in = getClass().getResourceAsStream(dirPath);
                    BufferedReader br = new BufferedReader(new InputStreamReader(in))) {
                    String resource;

                while ((resource = br.readLine()) != null) {
                    filenames.add(resource);
                }
            }

            return filenames;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

@@ -113,16 +113,164 @@
 uration;
+%0Aimport org.scify.memori.helper.PropertyHandlerImpl;%0Aimport org.scify.memori.helper.StringUtils;%0Aimport org.scify.memori.interfaces.PropertyHandler;
 %0A%0Aimport
@@ -635,16 +635,63 @@
 Default;
+%0A    private MemoriConfiguration configuration;
 %0A%0A    pu
@@ -723,36 +723,16 @@
         
-MemoriConfiguration 
 configur
@@ -961,23 +961,119 @@
  + %22/%22;%0A
+        System.err.println(%22DATA_PACK: %22 + configuration.getProjectProperty(%22DATA_PACKAGE%22));%0A
     %7D%0A%0A
+%0A%0A
     /**%0A
@@ -1188,16 +1188,92 @@
 ta pack%0A
+     * If not available, loads the corresponding file from the default pack%0A
      *%0A 
@@ -1560,24 +1560,337 @@
 fileName) %7B%0A
+        //check if there is a representation of the file in the additional data pack%0A        String additionalPackFileName = getFileNameEquivalentFromResourcePack(filePath + fileName);%0A        if(additionalPackFileName != null)%0A            fileName = additionalPackFileName;%0A        System.out.println(fileName);%0A
         Stri
@@ -2091,32 +2091,34 @@
 ath + fileName;%0A
+//
             Syst
@@ -2137,72 +2137,933 @@
 ln(%22
-Loaded default: %22 + file);%0A        %7D%0A        return file;%0A    %7D%0A
+File %22 + this.rootDataPath + filePath + fileName + %22 not found. Loaded default: %22 + file);%0A        %7D%0A        return file;%0A    %7D%0A%0A    /**%0A     * Searches for the file name in the additional resource pack that corresponds to the file name of the game%0A     * @param file the file name%0A     * @return the name of the file if found, otherwise null%0A     */%0A    private String getFileNameEquivalentFromResourcePack(String file) %7B%0A        //extract the path name (without the file) from the path given%0A//        String pathNoFile = StringUtils.substringBeforeLast(file, %22/%22);%0A        //search in the map file for the equivalent file name%0A        String dataPackFileNameNoPath = configuration.getDataPackProperty(file, this.rootDataPath + %22resources_map.properties%22);%0A        //if an equivalent file was added, return it.%0A        if(dataPackFileNameNoPath != null)%0A            return dataPackFileNameNoPath;%0A        return null;%0A    %7D
 %0A%0A  
@@ -3379,17 +3379,16 @@
 st%3C%3E();%0A
-%0A
         
@@ -3736,25 +3736,24 @@
           %7D%0A
-%0A
             

Load data from additional data pack