package com.cnooc.expert.config;

import com.google.gson.Gson;
import okhttp3.OkHttpClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import java.util.concurrent.TimeUnit;

@Configuration
@Profile("pre")
public class RetrofitPreConfiguration {

    /**
     * 专家服务
     * @param gson
     * @return
     */
    @Bean("retrofitExpert")
    @Primary
    public Retrofit RetrofitExpert(Gson gson) {
        return createRetrofit("https://purb.m-bid.cnooc.com.cn/", gson);
    }

    /**
     * 工作流服务
     * @param gson
     * @return
     */
    @Bean("retrofitWorkflow")
    public Retrofit RetrofitWorkflow(Gson gson) {
        return createRetrofit("https://purb.m-bid.cnooc.com.cn/", gson);
    }

    /**
     * 平台服务
     * @param gson
     * @return
     */
    @Bean("retrofitPortal")
    public Retrofit RetrofitPortal(Gson gson) {
        return createRetrofit("https://ex.m-bid.cnooc.com.cn/", gson);
    }

    /**
     * 主体服务
     * @param gson
     * @return
     */
    @Bean("retrofitSubject")
    public Retrofit RetrofitSubject(Gson gson) {
        return createRetrofit("https://purb.m-bid.cnooc.com.cn/", gson);
    }

    /**
     * 文件服务
     * @param gson
     * @return
     */
    @Bean("retrofitFile")
    public Retrofit RetrofitFile(Gson gson) {
        return createRetrofit("https://purb.m-bid.cnooc.com.cn/", gson);
    }

    private Retrofit createRetrofit(String baseUrl, Gson gson) {
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(30, TimeUnit.SECONDS)
                .readTimeout(30, TimeUnit.SECONDS)
                .build();

        return new Retrofit.Builder()
                .baseUrl(baseUrl)
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();
    }
}
