package com.mxkj.monitor.config;

import co.elastic.clients.elasticsearch.ElasticsearchAsyncClient;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import com.mxkj.monitor.engine.DataEngine;
import com.mxkj.monitor.engine.impl.ElasticSearchDataEngine;
import lombok.Data;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;


@Data
@Configuration
@ConfigurationProperties(prefix = "spring.elasticsearch")
@ConditionalOnProperty(prefix = "spring.elasticsearch",  name = "enable", havingValue = "true")
public class ElasticSearchConfig {

    private String hostname;

    private String port;

    private String username;

    private String password;

    /**
     *  同步客户端
     * @return
     */
    @Bean
    public ElasticsearchClient elasticsearchClient(){
        ElasticsearchTransport transport = buildTransferPort();
        return new ElasticsearchClient(transport);
    }

    /**
     *  异步客户端
     * @return
     */
    @Bean
    public ElasticsearchAsyncClient elasticSearchAsyncClient(){
        ElasticsearchTransport transport = buildTransferPort();
        // And create the API client
        return new ElasticsearchAsyncClient(transport);
    }

    @Bean
    @Primary
    @ConditionalOnBean(value = ElasticsearchClient.class)
    public DataEngine getElasticDataEngine(){
        return new ElasticSearchDataEngine();
    }

    private ElasticsearchTransport buildTransferPort(){
        final CredentialsProvider credentialsProvider =
                new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials(username, password));

        RestClientBuilder builder = RestClient.builder(
                new HttpHost(hostname, Integer.valueOf(port)))
                .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                    @Override
                    public HttpAsyncClientBuilder customizeHttpClient(
                            HttpAsyncClientBuilder httpClientBuilder) {
                        return httpClientBuilder
                                .setDefaultCredentialsProvider(credentialsProvider);
                    }
                });

        RestClient restClient = builder.build();

        // Create the transport with a Jackson mapper
        return new RestClientTransport(
                restClient, new JacksonJsonpMapper());
    }

}
