package io.senslog.core;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

public final class ApplicationInfo {

    private static final Logger logger = Logger.getLogger(ApplicationInfo.class.getSimpleName());

    private static final long startAppEpoch;
    private static final Properties properties;

    static {
        startAppEpoch = System.currentTimeMillis();
        properties = loadVersionProperties("version.txt");
    }

    private static Properties loadVersionProperties(String fileName) {
        ClassLoader classLoader = ApplicationInfo.class.getClassLoader();
        InputStream inputStream = classLoader.getResourceAsStream(fileName);
        if (inputStream != null) {
            try {
                Properties properties = new Properties();
                properties.load(inputStream);
                return properties;
            } catch (IOException e) {
                logger.log(Level.SEVERE, e.getMessage());
                return new Properties();
            }
        } else {
            return new Properties();
        }
    }

    public static long uptime() {
        return System.currentTimeMillis() - startAppEpoch;
    }

    public static String appVersion() {
        return properties.getProperty("version", "unknown");
    }

    public static String buildVersion() {
        return properties.getProperty("build.date", "unknown");
    }
}
