/*
 * Copyright 2012-2022 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.boot.actuate.autoconfigure.tracing.zipkin;

import io.opentelemetry.exporter.zipkin.ZipkinSpanExporter;
import zipkin2.Span;
import zipkin2.codec.BytesEncoder;
import zipkin2.reporter.AsyncReporter;
import zipkin2.reporter.Reporter;
import zipkin2.reporter.Sender;
import zipkin2.reporter.brave.ZipkinSpanHandler;
import zipkin2.reporter.urlconnection.URLConnectionSender;

import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

/**
 * Configurations for Zipkin. Those are imported by {@link ZipkinAutoConfiguration}.
 *
 * @author Moritz Halbritter
 */
class ZipkinConfigurations {

	@Configuration(proxyBeanMethods = false)
	@EnableConfigurationProperties(ZipkinProperties.class)
	static class SenderConfiguration {

		@Bean
		@ConditionalOnMissingBean
		@ConditionalOnClass(URLConnectionSender.class)
		Sender urlConnectionSender(ZipkinProperties properties) {
			return URLConnectionSender.newBuilder().connectTimeout((int) properties.getConnectTimeout().getSeconds())
					.readTimeout((int) properties.getReadTimeout().getSeconds()).endpoint(properties.getEndpoint())
					.build();
		}

		@Bean
		@ConditionalOnMissingBean
		@ConditionalOnBean(RestTemplateBuilder.class)
		@ConditionalOnMissingClass("zipkin2.reporter.urlconnection.URLConnectionSender")
		Sender restTemplateSender(ZipkinProperties properties, RestTemplateBuilder restTemplateBuilder) {
			RestTemplate restTemplate = restTemplateBuilder.setConnectTimeout(properties.getConnectTimeout())
					.setReadTimeout(properties.getReadTimeout()).build();
			return new ZipkinRestTemplateSender(properties.getEndpoint(), restTemplate);
		}

	}

	@Configuration(proxyBeanMethods = false)
	static class ReporterConfiguration {

		@Bean
		@ConditionalOnMissingBean
		@ConditionalOnBean(Sender.class)
		Reporter<Span> spanReporter(Sender sender, BytesEncoder<Span> encoder) {
			return AsyncReporter.builder(sender).build(encoder);
		}

	}

	@Configuration(proxyBeanMethods = false)
	@ConditionalOnClass(ZipkinSpanHandler.class)
	static class BraveConfiguration {

		@Bean
		@ConditionalOnMissingBean
		@ConditionalOnBean(Reporter.class)
		ZipkinSpanHandler zipkinSpanHandler(Reporter<Span> spanReporter) {
			return (ZipkinSpanHandler) ZipkinSpanHandler.newBuilder(spanReporter).build();
		}

	}

	@Configuration(proxyBeanMethods = false)
	@ConditionalOnClass(ZipkinSpanExporter.class)
	static class OpenTelemetryConfiguration {

		@Bean
		@ConditionalOnMissingBean
		@ConditionalOnBean(Sender.class)
		ZipkinSpanExporter zipkinSpanExporter(BytesEncoder<Span> encoder, Sender sender) {
			return ZipkinSpanExporter.builder().setEncoder(encoder).setSender(sender).build();
		}

	}

}

@@ -1255,91 +1255,8 @@
 an;%0A
-import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;%0A
 impo
@@ -1510,16 +1510,70 @@
 ration;%0A
+import org.springframework.context.annotation.Import;%0A
 import o
@@ -1814,16 +1814,239 @@
  false)%0A
+%09@Import(%7B UrlConnectionSenderConfiguration.class, RestTemplateSenderConfiguration.class %7D)%0A%09static class SenderConfiguration %7B%0A%0A%09%7D%0A%0A%09@Configuration(proxyBeanMethods = false)%0A%09@ConditionalOnClass(URLConnectionSender.class)%0A
 %09@Enable
@@ -2099,32 +2099,45 @@
 )%0A%09static class 
+UrlConnection
 SenderConfigurat
@@ -2182,31 +2182,25 @@
 Bean
-%0A%09%09@ConditionalOnClass(
+(Sender.class)%0A%09%09
 URLC
@@ -2218,24 +2218,8 @@
 nder
-.class)%0A%09%09Sender
  url
@@ -2501,44 +2501,53 @@
 %7D%0A%0A%09
-%09@Bean%0A%09%09@ConditionalOnMissingBean%0A%09
+%7D%0A%0A%09@Configuration(proxyBeanMethods = false)%0A
 %09@Co
@@ -2557,20 +2557,21 @@
 tionalOn
-Bean
+Class
 (RestTem
@@ -2579,108 +2579,239 @@
 late
-Builder.class)%0A%09%09@ConditionalOnMissingClass(%22zipkin2.reporter.urlconnection.URLConnectionSender%22)%0A%09%09
+.class)%0A%09@EnableConfigurationProperties(ZipkinProperties.class)%0A%09static class RestTemplateSenderConfiguration %7B%0A%0A%09%09@Bean%0A%09%09@ConditionalOnMissingBean(Sender.class)%0A%09%09@ConditionalOnBean(RestTemplateBuilder.class)%0A%09%09ZipkinRestTemplate
 Send
@@ -2860,17 +2860,21 @@
 perties,
- 
+%0A%09%09%09%09
 RestTemp
@@ -3311,16 +3311,21 @@
 lass)%0A%09%09
+Async
 Reporter

Move @ConditionalOnClass from methods to inner classes