package de.qaware.theo.mc.data;

import de.qaware.theo.mc.model.Metadata;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Logger;

/**
 * The reader for the properties file.
 * @author s.wittke
 */
public class PropertiesReader {

    public static final String AUTOGENERATED = "Autogenerated properties file by mission control.";
    private Map<String, String> propertiesMap;
    private Metadata metadata;

    public static final Logger LOGGER = Logger.getLogger(PropertiesReader.class.getName());

    public PropertiesReader(Metadata metadata) {
        propertiesMap = new HashMap<>();
        this.metadata = metadata;
    }


    /**
     * Read the properties file specified by the metadata.
     * If the file could not be found, generate the file with empty values.
     * @return a map containing all key value pairs if the file was found, a map containing the keys and empty values otherwise
     * @throws IOException
     */
    public Map<String, String> read() throws IOException {
        try {
            readFile();
        }
        catch (FileNotFoundException e) {
            generatePropertiesFile();
        }
        return propertiesMap;
    }

    private void generatePropertiesFile() throws IOException {
        LOGGER.info("Currently there is no propertiesMap file. Creating one.");

        Properties properties = getProperties();
        writeProperties(properties);
    }

    private Properties getProperties() {
        Properties properties = new Properties();
        propertiesMap.clear();
        for (String key : metadata.getKeys()) {
            if (metadata.getKeys().contains(key)) {
                properties.setProperty(key, "");
                propertiesMap.put(key, "");
            }
        }
        return properties;
    }

    private void writeProperties(Properties properties) throws IOException {
        OutputStream out = null;
        try {
            File file = new File(metadata.getFileName());
            out = new FileOutputStream(file);
            properties.store(out, AUTOGENERATED);
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }

    private void readFile() throws IOException {
        InputStreamReader in = null;
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream(metadata.getFileName());
            in = new InputStreamReader(inputStream, "UTF-8");
            Properties properties = new Properties();
            properties.load(in);

            for (String key : properties.stringPropertyNames()) {
                propertiesMap.put(key, properties.getProperty(key));
            }
        }
        finally {
            if (in != null) {
                in.close();
                inputStream.close();
            }
        }
    }
}

@@ -263,16 +263,19 @@
 s file.%0A
+ *%0A
  * @auth
@@ -870,16 +870,23 @@
 values.%0A
+     *%0A
      * @
@@ -2171,16 +2171,24 @@
        %7D
+%0A       
  finally
@@ -2704,24 +2704,84 @@
 yNames()) %7B%0A
+                if (metadata.getKeys().contains(key)) %7B%0A    
             
@@ -2836,16 +2836,35 @@
 y(key));
+%0A%0A                %7D
 %0A       

Add properties only when contained in metadata