package com.mogo.cloud.network

import android.util.ArrayMap
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory

/**
 *
 * @Author:         wujifei
 * @CreateDate:     1/19/21 11:21 PM
 * @Description:    获取Retrofit实例
 */
object RetrofitFactory {
    private val sRpcServiceMap: MutableMap<String, Retrofit?> = ArrayMap()
    private val sRpcNoAdapterServiceMap: MutableMap<String, Retrofit?> = ArrayMap()

    @Synchronized
    fun getInstance(baseUrl: String): Retrofit? {
        var target = sRpcServiceMap[baseUrl]
        if (target == null) {
            target = Retrofit.Builder()
                    .client(OkHttpFactory.okHttpClient)
                    .baseUrl(baseUrl)
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .addConverterFactory(GsonConverterFactory.create())
                    .build()
            sRpcServiceMap[baseUrl] = target
            WeakNetworkEventFactory.setListener(WeakNetworkManager.listener)
        }
        return target
    }

    @Synchronized
    fun getInstanceNoCallAdapter(baseUrl: String): Retrofit? {
        var target = sRpcNoAdapterServiceMap[baseUrl]
        if (target == null) {
            target = Retrofit.Builder()
                    .client(OkHttpFactory.okHttpClient)
                    .baseUrl(baseUrl)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build()
            sRpcNoAdapterServiceMap[baseUrl] = target
            WeakNetworkEventFactory.setListener(WeakNetworkManager.listener)
        }
        return target
    }


}