package org.holer.common.util;

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

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class HolerConfig {
    private static Logger log = LoggerFactory.getLogger(HolerConfig.class);

    private static HolerConfig config = null;

    private Properties holerConf;


    public static HolerConfig getConfig() {
        if (null == config) {
            config = new HolerConfig();
        }
        return config;
    }


    public static HolerConfig getConfig(String path) {
        if (null == config) {
            config = new HolerConfig(path);
        }
        return config;
    }


    public HolerConfig(String path) {
        this.holerConf = new Properties();
        initConfig("org/holer/conf/holer.conf");
        initConfig(path + "holer.conf");
    }


    public HolerConfig() {
        this.holerConf = new Properties();
        initConfig("org/holer/conf/holer.conf");
        initConfig("holer.conf");
    }


    public void initConfig(String confFile) {
        InputStream is = null;

        try {
            is = HolerConfig.class.getClassLoader().getResourceAsStream(confFile);
            if (null == is) {

                log.warn("Can not find configuration file: " + confFile);
                return;
            }
            this.holerConf.load(is);
        } catch (IOException e) {

            log.error("Failed to load configuration file.");
            throw new RuntimeException(e);
        } finally {

            HolerUtil.close(is);
        }
    }


    public String strValue(String key) {
        return this.holerConf.getProperty(key);
    }


    public String strValue(String key, String defaultValue) {
        String value = strValue(key);
        if (StringUtils.isBlank(value)) {
            return defaultValue;
        }
        return value;
    }


    public Integer intValue(String key) {
        String value = this.holerConf.getProperty(key);
        if (StringUtils.isEmpty(value)) {
            return null;
        }
        if (StringUtils.isNumeric(value)) {
            return Integer.valueOf(value);
        }
        return null;
    }


    public int intValue(String key, int defaultValue) {
        Integer value = intValue(key);
        if (null == value) {
            return defaultValue;
        }
        return value.intValue();
    }


    public Boolean boolValue(String key) {
        String value = this.holerConf.getProperty(key);
        if (StringUtils.isBlank(value)) {
            return null;
        }

        if (!value.equals(Boolean.toString(false)) &&
                !value.equals(Boolean.toString(true))) {
            return null;
        }

        return Boolean.valueOf(value);
    }


    public boolean boolValue(String key, Boolean defaultValue) {
        Boolean value = boolValue(key);
        if (null == value) {
            return defaultValue.booleanValue();
        }
        return value.booleanValue();
    }
}
