package org.jpos.tcpay;

import org.apache.commons.lang3.StringUtils;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class ApplicationProperties {
    public  Properties properties = new Properties();

    public ApplicationProperties() {
        try (InputStream is = Files.newInputStream(Paths.get("cfg/application.properties"))) {
            properties.load(is);

        } catch (IOException e) {
            // No required
        }
    }

    public Map<String, Object> getPersistenceProperties() {
        Map<String, Object> configOverrides = new HashMap<>();
        String driver = properties.getProperty("javax.persistence.jdbc.driver");
        String url = properties.getProperty("javax.persistence.jdbc.url");
        String user = properties.getProperty("javax.persistence.jdbc.user");
        String password = properties.getProperty("javax.persistence.jdbc.password");
        String dialect = properties.getProperty("hibernate.dialect");
        String showSql = properties.getProperty("hibernate.show_sql");
        String formatSql = properties.getProperty("hibernate.format_sql");
        String hbm2ddl = properties.getProperty("hibernate.hbm2ddl.auto");
        if (!StringUtils.isEmpty(driver)) {
            configOverrides.put("javax.persistence.jdbc.driver", driver);
        }

        if (!StringUtils.isEmpty(url)) {
            configOverrides.put("javax.persistence.jdbc.url", url);
        }

        if (!StringUtils.isEmpty(user)) {
            configOverrides.put("javax.persistence.jdbc.user", user);
        }

        if (!StringUtils.isEmpty(password)) {
            configOverrides.put("javax.persistence.jdbc.password", password);
        }

        if (!StringUtils.isEmpty(dialect)) {
            configOverrides.put("hibernate.dialect", dialect);
        }

        if (!StringUtils.isEmpty(showSql)) {
            configOverrides.put("hibernate.show_sql", showSql);
        }

        if (!StringUtils.isEmpty(formatSql)) {
            configOverrides.put("hibernate.format_sql", formatSql);
        }

        if (!StringUtils.isEmpty(hbm2ddl)) {
            configOverrides.put("hibernate.hbm2ddl.auto", hbm2ddl);
        }

        return configOverrides;
    }

}
