/*
 * The MIT License
 *
 * Copyright 2015 Ivar Grimstad (ivar.grimstad@gmail.com).
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package eu.agilejava.snoopee.client;

import com.fasterxml.jackson.dataformat.yaml.snakeyaml.Yaml;
import com.fasterxml.jackson.dataformat.yaml.snakeyaml.error.YAMLException;
import eu.agilejava.snoopee.SnoopEEConfigurationException;
import eu.agilejava.snoopee.SnoopEEExtensionHelper;
import eu.agilejava.snoopee.annotation.SnoopEE;
import org.eclipse.microprofile.config.inject.ConfigProperty;

import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.Dependent;
import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.inject.Inject;
import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import java.util.logging.Logger;

/**
 * CDI Producer for SnoopEEServiceClient.
 *
 * @author Ivar Grimstad (ivar.grimstad@gmail.com)
 */
@ApplicationScoped
public class SnoopEEProducer {

    private static final Logger LOGGER = Logger.getLogger("eu.agilejava.snoopee");

//    private Map<String, Object> snoopeeConfig = Collections.EMPTY_MAP;

    @ConfigProperty(name = "snoopeeService", defaultValue = "http://localhost:8081/snoopee-service/")
    @Inject
    private String serviceUrl;

    @ConfigProperty(name ="serviceName", defaultValue = "noname")
    @Inject
    private String serviceName;

    /**
     * Creates a SnoopEEServiceClient for the named service.
     *
     * @param ip The injection point
     * @return a configured SnoopEE service client
     */
    @SnoopEE
    @Produces
    @Dependent
    public SnoopEEServiceClient lookup(InjectionPoint ip) {

        final String applicationName = ip.getAnnotated().getAnnotation(SnoopEE.class).serviceName();

        LOGGER.config(() -> "producing " + applicationName);

//        String serviceUrl = "http://" + readProperty("snoopeeService", snoopeeConfig);
        LOGGER.config(() -> "Service URL: " + serviceUrl);

        return new SnoopEEServiceClient.Builder(applicationName)
                .serviceUrl(serviceUrl)
                .build();
    }

//    private String readProperty(final String key, Map<String, Object> snoopConfig) {
//
//        String property = Optional.ofNullable(System.getProperty(key))
//                .orElseGet(() -> {
//                    String envProp = Optional.ofNullable(System.getenv(key))
//                            .orElseGet(() -> {
//                                String confProp = Optional.ofNullable(snoopConfig.get(key))
//                                        .orElseThrow(() -> {
//                                            return new SnoopEEConfigurationException(key + " must be configured either in snoopee.yml or as env or system property");
//                                        })
//                                        .toString();
//                                return confProp;
//                            });
//                    return envProp;
//                });
//
//        return property;
//    }

    /**
     * Initializes the producer with the SnoopEE configuration properties.
     */
    @PostConstruct
    private void init() {

//        try {
//            Yaml yaml = new Yaml();
//            Map<String, Object> props = (Map<String, Object>) yaml.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("/snoopee.yml"));
//
//            snoopeeConfig = (Map<String, Object>) props.get("snoopee");

//            if (!SnoopEEExtensionHelper.isSnoopEnabled()) {
//                SnoopEEExtensionHelper.setServiceName(readProperty("serviceName", snoopeeConfig));
//            }

//        } catch (YAMLException e) {
//            LOGGER.config(() -> "No configuration file. Using env properties.");
//        }
    }
}

@@ -1198,256 +1198,8 @@
 t;%0A%0A
-import com.fasterxml.jackson.dataformat.yaml.snakeyaml.Yaml;%0Aimport com.fasterxml.jackson.dataformat.yaml.snakeyaml.error.YAMLException;%0Aimport eu.agilejava.snoopee.SnoopEEConfigurationException;%0Aimport eu.agilejava.snoopee.SnoopEEExtensionHelper;%0A
 impo
@@ -1242,16 +1242,16 @@
 noopEE;%0A
+
 import o
@@ -1309,47 +1309,8 @@
 y;%0A%0A
-import javax.annotation.PostConstruct;%0A
 impo
@@ -1523,87 +1523,8 @@
 ct;%0A
-import java.util.Collections;%0Aimport java.util.Map;%0Aimport java.util.Optional;%0A
 impo
@@ -1796,82 +1796,8 @@
 );%0A%0A
-//    private Map%3CString, Object%3E snoopeeConfig = Collections.EMPTY_MAP;%0A%0A
     
@@ -1942,119 +1942,8 @@
 l;%0A%0A
-    @ConfigProperty(name =%22serviceName%22, defaultValue = %22noname%22)%0A    @Inject%0A    private String serviceName;%0A%0A
     
@@ -2379,98 +2379,8 @@
 me);
-%0A%0A//        String serviceUrl = %22http://%22 + readProperty(%22snoopeeService%22, snoopeeConfig);
 %0A   
@@ -2541,16 +2541,16 @@
 iceUrl)%0A
+
         
@@ -2576,1692 +2576,7 @@
    %7D
-%0A%0A//    private String readProperty(final String key, Map%3CString, Object%3E snoopConfig) %7B%0A//%0A//        String property = Optional.ofNullable(System.getProperty(key))%0A//                .orElseGet(() -%3E %7B%0A//                    String envProp = Optional.ofNullable(System.getenv(key))%0A//                            .orElseGet(() -%3E %7B%0A//                                String confProp = Optional.ofNullable(snoopConfig.get(key))%0A//                                        .orElseThrow(() -%3E %7B%0A//                                            return new SnoopEEConfigurationException(key + %22 must be configured either in snoopee.yml or as env or system property%22);%0A//                                        %7D)%0A//                                        .toString();%0A//                                return confProp;%0A//                            %7D);%0A//                    return envProp;%0A//                %7D);%0A//%0A//        return property;%0A//    %7D%0A%0A    /**%0A     * Initializes the producer with the SnoopEE configuration properties.%0A     */%0A    @PostConstruct%0A    private void init() %7B%0A%0A//        try %7B%0A//            Yaml yaml = new Yaml();%0A//            Map%3CString, Object%3E props = (Map%3CString, Object%3E) yaml.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(%22/snoopee.yml%22));%0A//%0A//            snoopeeConfig = (Map%3CString, Object%3E) props.get(%22snoopee%22);%0A%0A//            if (!SnoopEEExtensionHelper.isSnoopEnabled()) %7B%0A//                SnoopEEExtensionHelper.setServiceName(readProperty(%22serviceName%22, snoopeeConfig));%0A//            %7D%0A%0A//        %7D catch (YAMLException e) %7B%0A//            LOGGER.config(() -%3E %22No configuration file. Using env properties.%22);%0A//        %7D%0A    %7D
 %0A%7D%0A

Clean up old config