package org.opencds.cqf.ruler.plugin.cdshooks;

import java.util.concurrent.atomic.AtomicReference;

import com.google.gson.JsonArray;

import org.opencds.cqf.ruler.plugin.cdshooks.providers.ProviderConfiguration;
import org.opencds.cqf.ruler.plugin.cql.CqlProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import ca.uhn.fhir.jpa.starter.annotations.OnDSTU3Condition;
import ca.uhn.fhir.jpa.starter.annotations.OnR4Condition;

@Configuration
@ConditionalOnProperty(prefix = "hapi.fhir.cdshooks", name = "enabled", havingValue = "true", matchIfMissing = true)
public class CdsHooksConfig {

	@Autowired
	AutowireCapableBeanFactory beanFactory;

	@Bean
	public CdsHooksProperties cdsHooksProperties() {
		return new CdsHooksProperties();
	}

	@Bean
	public ProviderConfiguration providerConfiguration(CdsHooksProperties cdsProperties, CqlProperties cqlProperties) {
		return new ProviderConfiguration(cdsProperties, cqlProperties);
	}

	@Bean(name = "globalCdsServiceCache")
	public AtomicReference<JsonArray> cdsServiceCache() {
		return new AtomicReference<>();
	}

	@Bean
	@Primary
	@Conditional(OnDSTU3Condition.class)
	public ca.uhn.fhir.cql.dstu3.provider.LibraryResolutionProviderImpl libraryResolutionProviderDstu3() {
		return new ca.uhn.fhir.cql.dstu3.provider.LibraryResolutionProviderImpl();
	}

	@Bean
	@Conditional(OnDSTU3Condition.class)
	public ServletRegistrationBean<org.opencds.cqf.ruler.plugin.cdshooks.dstu3.CdsHooksServlet> cdsHooksRegistrationBeanDstu3() {
		org.opencds.cqf.ruler.plugin.cdshooks.dstu3.CdsHooksServlet cdsHooksServlet = new org.opencds.cqf.ruler.plugin.cdshooks.dstu3.CdsHooksServlet();
		beanFactory.autowireBean(cdsHooksServlet);

		ServletRegistrationBean<org.opencds.cqf.ruler.plugin.cdshooks.dstu3.CdsHooksServlet> registrationBean = new ServletRegistrationBean<org.opencds.cqf.ruler.plugin.cdshooks.dstu3.CdsHooksServlet>();
		registrationBean.setServlet(cdsHooksServlet);
		registrationBean.addUrlMappings("/cds-services/*");
		registrationBean.setLoadOnStartup(1);
		return registrationBean;
	}

	@Bean
	@Primary
	@Conditional(OnR4Condition.class)
	public ca.uhn.fhir.cql.r4.provider.LibraryResolutionProviderImpl libraryResolutionProviderR4() {
		return new ca.uhn.fhir.cql.r4.provider.LibraryResolutionProviderImpl();
	}

	@Bean
	@Conditional(OnR4Condition.class)
	public ServletRegistrationBean<org.opencds.cqf.ruler.plugin.cdshooks.r4.CdsHooksServlet> cdsHooksRegistrationBeanR4() {
		org.opencds.cqf.ruler.plugin.cdshooks.r4.CdsHooksServlet cdsHooksServlet = new org.opencds.cqf.ruler.plugin.cdshooks.r4.CdsHooksServlet();
		beanFactory.autowireBean(cdsHooksServlet);

		ServletRegistrationBean<org.opencds.cqf.ruler.plugin.cdshooks.r4.CdsHooksServlet> registrationBean = new ServletRegistrationBean<org.opencds.cqf.ruler.plugin.cdshooks.r4.CdsHooksServlet>();
		registrationBean.setServlet(cdsHooksServlet);
		registrationBean.addUrlMappings("/cds-services/*");
		registrationBean.setLoadOnStartup(1);
		return registrationBean;
	}
}

@@ -724,63 +724,8 @@
 ion;
-%0Aimport org.springframework.context.annotation.Primary;
 %0A%0Aim
@@ -1485,248 +1485,8 @@
 %09%7D%0A%0A
-%09@Bean%0A%09@Primary%0A%09@Conditional(OnDSTU3Condition.class)%0A%09public ca.uhn.fhir.cql.dstu3.provider.LibraryResolutionProviderImpl libraryResolutionProviderDstu3() %7B%0A%09%09return new ca.uhn.fhir.cql.dstu3.provider.LibraryResolutionProviderImpl();%0A%09%7D%0A%0A
 %09@Be
@@ -2178,32 +2178,32 @@
 adOnStartup(1);%0A
+
 %09%09return registr
@@ -2221,236 +2221,8 @@
 %09%7D%0A%0A
-%09@Bean%0A%09@Primary%0A%09@Conditional(OnR4Condition.class)%0A%09public ca.uhn.fhir.cql.r4.provider.LibraryResolutionProviderImpl libraryResolutionProviderR4() %7B%0A%09%09return new ca.uhn.fhir.cql.r4.provider.LibraryResolutionProviderImpl();%0A%09%7D%0A%0A
 %09@Be

Fix bug in cdshooks config