package com.capgemini.framework.teamcenter.authent;

import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * Utility class required for loading properties used by teamcenter session authentication.
 **/
@Slf4j
public final class AuthentPropLoader {

    private static final String KURT_PROP_FILENAME = "/reporting.properties";
    private static Properties tcProperties;

    /**
     * Constructor.
     **/
    private AuthentPropLoader() {
        // Utility classes should not have a public constructor
    }

    /**
     * Retrieve the properties and load them if needed.
     *
     * @return Property content of file
     **/
    private static synchronized Properties getPropLoader() {
        // Check if the property file has been already loaded
        if (tcProperties == null) {
            tcProperties = new Properties();
            try (InputStream thePropStream = readPropertiesFile()) {
                tcProperties.load(thePropStream);
            } catch (IOException e) {
                log.error("Unable to load properties file", e);
            }
        }
        return tcProperties;
    }

    // Try to access file in the configuration folder of JBOSS, else use by default the file provided in EAR
    private static InputStream readPropertiesFile() {
        String theJbossConfPath = System.getenv("JBOSS_HOME") + "/standalone/configuration" + KURT_PROP_FILENAME;
        boolean isConfFile = new File(theJbossConfPath).exists();
        if (isConfFile) {
            log.debug("JBOSS configuration file has been found in: " + theJbossConfPath);
            try {
                return new FileInputStream(theJbossConfPath);
            } catch (FileNotFoundException e) {
                log.error("Unable to load file: " + theJbossConfPath, e);
            }
        }
        // default: JBoss file does not exist or not readable
        log.warn("EAR file will be used, no configuration file found in: " + theJbossConfPath);
        return AuthentPropLoader.class.getResourceAsStream(KURT_PROP_FILENAME);
    }

    /**
     * Getter for the property value corresponding to the property key.
     *
     * @param aPropKey Property key reference
     * @return Property value
     */
    public static String getString(final String aPropKey) {
        return getPropLoader().getProperty(aPropKey);
    }

    /**
     * Returns an array of symbol-separated values.
     *
     * @param aPropKey  the property name
     * @param separator the separator symbol
     * @return an array of Strings. Empty array if an empty string was found
     */
    public static String[] getStringArray(final String aPropKey, String separator) {
        String raw = System.getProperty(aPropKey);
        if (raw == null || raw.isEmpty()) {
            log.warn("No property array found in tc properties for key " + aPropKey);
            return new String[0];
        }
        List<String> result = new ArrayList<>();
        for (String tmp : raw.split(separator)) {
            if (!tmp.isEmpty()) {
                result.add(tmp);
            }
        }
        return result.toArray(new String[result.size()]);
    }
}
