package com.epimorphics.registry.webapi.oauth2;

import org.apache.commons.io.filefilter.SuffixFileFilter;

import java.io.File;
import java.io.FileFilter;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Properties;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Exposes OAuth2 configuration.
 */
public class OAuth2Service {
	private static final String CONFIG_LOC = "/opt/ldregistry/config/oauth.conf";
	private static final String PROVIDERS_DIR = "/opt/ldregistry/config/oauth2/provider";

	private final Logger log = LoggerFactory.getLogger(OAuth2Service.class);
	private final OAuth2Config config = createConfig();

	private OAuth2Config createConfig() {
		try {
			FileReader reader = new FileReader(CONFIG_LOC);
			try {
				Properties props = readProperties(reader);
				Collection<OAuth2Provider> providers = getProviders();

				return new OAuth2ConfigProperties(props, providers);
			} catch (IOException ioe) {
				throw new RuntimeException("Unable to read OAuth2 configuration", ioe);
			}
		} catch (IOException ioe) {
			log.info("OAuth configuration file not found at " + CONFIG_LOC + ". OAuth2 login will not be available: " + ioe.getMessage());
			return null; // TODO empty impl?
		}
	}

	private Properties readProperties(FileReader reader) throws IOException {
		Properties props = new Properties();
		try {
			props.load(reader);
		} finally {
			reader.close();
		}

		return props;
	}

	private Collection<OAuth2Provider> getProviders() {
		File providersDir = new File(PROVIDERS_DIR);
		File[] providerFiles = providersDir.listFiles((FileFilter)new SuffixFileFilter("properties"));

		return Arrays.asList(providerFiles).stream().map(file -> {
			String path = file.getAbsolutePath();
			try {
				FileReader reader = new FileReader(path);
				log.info("Configuring OAuth2 provider at " + path);
				Properties props = readProperties(reader);

				return new OAuth2ProviderProperties(props);
			} catch (IOException ioe) {
				throw new RuntimeException("Unable to read OAuth2 provider specs at " + path, ioe);
			}
		}).collect(Collectors.toList());
	}

	public OAuth2Config getConfig() {
		return config;
	}
}

@@ -464,122 +464,179 @@
 ate 
-static final String CONFIG_LOC = %22/opt/ldregistry/config/oauth.conf%22;%0A%09private static final String PROVIDERS_DIR =
+final Logger log = LoggerFactory.getLogger(OAuth2Service.class);%0A%09private final OAuth2Config config;%0A%0A%09public OAuth2Service() %7B%0A%09%09this(%22/opt/ldregistry/config/oauth.conf%22,
  %22/o
@@ -676,174 +676,209 @@
 der%22
+)
 ;%0A
-%0A%09private final Logger log = LoggerFactory.getLogger(OAuth2Service.class);%0A%09private final OAuth2Config config = createConfig();%0A%0A%09private OAuth2Config createConfig(
+%09%7D%0A%0A%09public OAuth2Service(String configLoc, String providersDir) %7B%0A%09%09this.config = createConfig(configLoc, providersDir);%0A%09%7D%0A%0A%09private OAuth2Config createConfig(String configLoc, String providersDir
 ) %7B%0A
@@ -923,26 +923,25 @@
 eReader(
-CONFIG_LOC
+configLoc
 );%0A%09%09%09tr
@@ -1047,16 +1047,28 @@
 oviders(
+providersDir
 );%0A%0A%09%09%09%09
@@ -1321,18 +1321,17 @@
 %22 + 
-CONFIG_LOC
+configLoc
  + %22
@@ -1687,16 +1687,38 @@
 oviders(
+String providersDirLoc
 ) %7B%0A%09%09Fi
@@ -1748,21 +1748,23 @@
 ile(
-PROVIDERS_DIR
+providersDirLoc
 );%0A%09

REFACTOR Service instantiation to paramaterize config location