/*-
 * ~~~~~~licensing~~~~~~
 * system-commons
 * ==========
 * Copyright (C) 2020 - 2025 EmDev LLC
 * ==========
 * You may not use this file except in accordance with the License Terms of the Copyright
 * Holder located at: https://entaxy.ru/eula . All copyrights, all intellectual property
 * rights to the Software and any copies are the property of the Copyright Holder. Unless
 * it is explicitly allowed the Copyright Holder, the User is prohibited from using the
 * Software for commercial purposes to provide services to third parties.
 * 
 * The Copyright Holder hereby declares that the Software is provided on an "AS IS".
 * Under no circumstances does the Copyright Holder guarantee or promise that the
 * Software provided by him will be suitable or not suitable for the specific purposes
 * of the User, that the Software will meet all commercial and personal subjective
 * expectations of the User, that the Software will work properly, without technical
 * errors, quickly and uninterruptedly.
 * 
 * Under no circumstances shall the Copyright Holder or its Affiliates is not liable
 * to the User for any direct or indirect losses of the User, his expenses or actual
 * damage, including, downtime; loss of bussines; lost profit; lost earnings; loss
 * or damage to data, property, etc.
 * ~~~~~~/licensing~~~~~~
 */
package ru.entaxy.esb.system.common.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class PropertiesHelper {

    private static final Log LOG = LogFactory.getLog(PropertiesHelper.class.getName());

    private static final String EMPTY_STRING = "";

    private String configPath;
    private static final String karafEtcPath = System.getProperty("karaf.etc");
    private String configFile;

    private Properties properties;

    public PropertiesHelper() {
    }

    public PropertiesHelper(String configPath) {
        LOG.debug("Set custom path: " + configPath + " file: " + configFile);
        this.configPath = configPath;
    }

    public PropertiesHelper(String configPath, String configFile) {
        LOG.debug("Load properties from custom path: " + configPath + " file: " + configFile);
        this.configPath = configPath;
        this.configFile = configFile;
        loadProperties(configPath, configFile);
    }

    public PropertiesHelper(String configFile, boolean loadKarafEtc) {
        this.configFile = configFile;
        LOG.debug("Load properties from karaf etc: " + karafEtcPath + " file: " + configFile);
        if (loadKarafEtc) {
            loadProperties(karafEtcPath, configFile);
            LOG.debug("Loaded properties: " + (properties != null ? properties.size() : "null"));
        }
    }

    public Properties load() {
        if (this.configPath != null && !this.configPath.isEmpty()
                && this.configFile != null && !this.configFile.isEmpty()) {
            return loadProperties(configPath, configFile);
        } else if (this.configFile != null && !this.configFile.isEmpty()) {
            return loadProperties(karafEtcPath, configFile);
        } else {
            throw new IllegalArgumentException("configPath OR configFile NOT SETTED");
        }
    }

    protected Properties loadProperties(String path, String configFile) {
        try (InputStream input = new FileInputStream(path + File.separator + configFile)) {
            properties = new Properties();
            properties.load(input);
        } catch (IOException ex) {
            LOG.error(ex);
        }
        return properties;
    }

    public long getInteger(String name) {
        return getInteger(name, 0);
    }

    public long getInteger(String name, int defaultValue) {
        String value = this.properties.getProperty(name);
        return value != null && !EMPTY_STRING.equals(value) ? Integer.valueOf(value) : defaultValue;
    }

    public int getInteger(String name, String defaultValue) {
        return Integer.valueOf(this.properties.getProperty(name, defaultValue));
    }

    public long getLong(String name) {
        return getLong(name, 0L);
    }

    public long getLong(String name, long defaultValue) {
        String value = this.properties.getProperty(name);
        return value != null && !EMPTY_STRING.equals(value) ? Long.valueOf(value) : defaultValue;
    }

    public long getLong(String name, String defaultValue) {
        return Long.valueOf(this.properties.getProperty(name, defaultValue));
    }

    public String getString(String name) {
        return this.properties.getProperty(name, EMPTY_STRING);
    }

    public String getString(String name, String defaultValue) {
        return this.properties.getProperty(name, defaultValue);
    }

    public String[] getStringArray(String name, String[] defaultValue) {
        String value = this.properties.getProperty(name, EMPTY_STRING);
        return !value.equals(EMPTY_STRING) ? value.split(",") : defaultValue;
    }

    public String getConfigPath() {
        return configPath;
    }

    public void setConfigPath(String configPath) {
        this.configPath = configPath;
    }

    public String getConfigFile() {
        return configFile;
    }

    public void setConfigFile(String configFile) {
        this.configFile = configFile;
    }

    public Properties getProperties() {
        return properties;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

}
