package com.jsc.dsp.config;

import com.jsc.dsp.utils.EsUtils;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.json.JacksonJsonParser;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.Base64;
import java.util.Base64.Decoder;
import java.util.Base64.Encoder;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Component
public class Configuration {

    @Value("${es.ip}")
    String esIp;

    @Value("${es.port}")
    Integer esPort;

    @Value("${es.username}")
    String esUsername;

    @Value("${es.password}")
    String esPassword;

    @Bean
    public JacksonJsonParser getJacksonParser() {
        return new JacksonJsonParser();
    }

    @Bean
    public Decoder getBase64() {
        return Base64.getDecoder();
    }

    @Bean
    public Encoder getEncoder() {
        return Base64.getEncoder();
    }

    @Bean
    public ExecutorService getTheadPool() {
        return Executors.newFixedThreadPool(4);
    }

    @Bean
    public RestHighLevelClient esClient() {
        return EsUtils.getElasticsearchClient(esIp, esPort, esUsername, esPassword);
    }
}
