package ch.polgrabia.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;

public class Program {
    private static final Logger logger = LoggerFactory.getLogger(Program.class);
    public static final String ANSITERMLOGGER_PROPERTIES = "ansitermlogger.properties";

    public static void main(String[] args) {
        loadPropertiesFromPath(
                Paths.get(System.getenv("LOCALAPPDATA"), "ansitermlogger", ANSITERMLOGGER_PROPERTIES));
        loadPropertiesFromPath(
                Paths.get(".", ANSITERMLOGGER_PROPERTIES).toAbsolutePath());

        logger.trace("Hello World");
        logger.debug("Hello World");
        logger.info("Hello World");
        logger.warn("Hello World");
        logger.error("Hello World");
    }

    private static void loadPropertiesFromPath(Path propertiesPath) {
        Path absolutePath = propertiesPath.toAbsolutePath();
        File appPropertiesFile = new File(absolutePath.toString());
        if (appPropertiesFile.exists()) {
            try (InputStream is = new FileInputStream(appPropertiesFile)) {
                Properties properties = new Properties();
                properties.load(is);
                AnsiTermLoggerServiceProvider.getInstance().loadProperties(properties);
            } catch (IOException e) {
                throw new IllegalStateException("I couldn't have read properties from file: " + absolutePath);
            }
        }
    }
}
