package org.micord.service;


import org.micord.enums.ConfigType;
import org.micord.utils.ConfigLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.FileNotFoundException;
import java.util.Optional;

@Service
public class ConfigService {

	private static final Logger logger = LoggerFactory.getLogger(ConfigService.class);

	@Autowired
	private ConfigLoader configLoader;

	public <T> T getConfig(ConfigType methodName, Class<T> configClass) throws FileNotFoundException {
		logger.debug("Loading configuration for method: {}", methodName);
		Optional<T> optionalConfig = configLoader.loadConfigIfModified(methodName, configClass);

		if (optionalConfig.isEmpty()) {
			throw new FileNotFoundException("Configuration for method " + methodName + " could not be loaded.");
		}

		return optionalConfig.get();
	}

}
