/*-
 * ~~~~~~licensing~~~~~~
 * system-commons
 * ==========
 * Copyright (C) 2020 - 2021 EmDev LLC
 * ==========
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ~~~~~~/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;
    }

}
