package com.liangjian.dataplatform.member.config;

import com.liangjian.dataplatform.member.util.StringUtil;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.client.Client;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.apache.http.HttpHost;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

import java.net.InetAddress;
import java.net.UnknownHostException;
/**
 * @Author: 黄子奇
 * @Date: 2019/12/11
 */
@Configuration
@EnableElasticsearchRepositories
public class ElasticsearchConfig {

    @Value("${spring.data.elasticsearch.cluster-nodes}")

    private String host;

    @Value("${spring.data.elasticsearch.cluster-nodesh}")

    public String hosth;

    @Value("${spring.data.elasticsearch.cluster-name}")

    private String cluster;



//    @Bean("esclient")
//    public Client getClient1(){
//        try{
//// 设置ES实例的名称
//            Settings settings = Settings.builder().put("cluster.name", cluster).build();
//// 创建客户端，如果使用默认配置，传参为 Settings.Empty
//            TransportClient client = new PreBuiltTransportClient(settings);
//// 添加节点
//            String[] hosts = host.split(";");
//            for(String host : hosts) {
//                String ip = host.split(":")[0];
//                int port = Integer.valueOf(host.split(":")[1]);
//// 创建节点
//                TransportAddress node = new TransportAddress(InetAddress.getByName(ip), port);
//                client.addTransportAddress(node);
//            }
//            System.out.println("Elasticsearch Client 连接成功");
//            return client;
//        } catch (UnknownHostException e) {
//            e.printStackTrace();
//        }
//        return null;
//    }

//    @Bean("elasticsearchTemplate")
//    public ElasticsearchTemplate getEs(Client client){//@Qualifier("esclient")
//
//        ElasticsearchTemplate template = new ElasticsearchTemplate(client);
//        return  templ//    }


    @Bean("RestHighLevelClient1")
    public RestHighLevelClient getClient(){
        String host = "192.168.50.41";
        Integer port = 9200;
        if(!StringUtils.isEmpty(hosth)) {
            String[] hosts = hosth.split(":");
            if(hosts.length>=2)
            host = hosts[0];
            port = Integer.valueOf(hosts[1]);
        }
        RestHighLevelClient client =
                new RestHighLevelClient(RestClient.builder(new HttpHost(host, port, "http")));
        return  client;
    }
    @Bean("elasticsearchTemplate")
    public ElasticsearchRestTemplate getEs(@Qualifier("RestHighLevelClient1") RestHighLevelClient client){

        ElasticsearchRestTemplate template = new ElasticsearchRestTemplate(client);
        return  template;
    }

}
