package com.quyunshuo.androidbaseframemvvm.common.di

import com.module.pay.base.FirstApi
import com.module.pay.base.SecondApi
import com.quyunshuo.androidbaseframemvvm.base.BuildConfig
import com.quyunshuo.androidbaseframemvvm.base.constant.VersionStatus
import com.quyunshuo.androidbaseframemvvm.common.constant.NetBaseUrlConstant
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import okhttp3.logging.HttpLoggingInterceptor.Level.BODY
import okhttp3.logging.HttpLoggingInterceptor.Level.NONE
import retrofit2.Retrofit
import retrofit2.converter.scalars.ScalarsConverterFactory
import java.util.concurrent.TimeUnit
import javax.inject.Named
import javax.inject.Singleton

/**
 * 全局作用域的网络层的依赖注入模块
 *
 * @author Qu Yunshuo hilt
 * @since 6/4/21 8:58 AM
 *
 * 1. 定义 Qualifier 注解
 * kotlin
 * @Qualifier
 * @Retention(AnnotationRetention.RUNTIME)
 * annotation class FirstApi
 *
 * @Qualifier
 * @Retention(AnnotationRetention.RUNTIME)
 * annotation class SecondApi
 *
 *
 * 2. 在 Hilt Module 中提供 Retrofit 实例
 * kotlin
 * @Module
 * @InstallIn(SingletonComponent::class)
 * object NetworkModule {
 *
 *     @FirstApi
 *     @Provides
 *     @Singleton
 *     fun provideFirstRetrofit(): Retrofit {
 *         return Retrofit.Builder()
 *             .baseUrl("https://api.first-domain.com/")
 *             .client(OkHttpClient.Builder().build())
 *             .addConverterFactory(GsonConverterFactory.create())
 *             .build()
 *     }
 *
 *     @SecondApi
 *     @Provides
 *     @Singleton
 *     fun provideSecondRetrofit(): Retrofit {
 *         return Retrofit.Builder()
 *             .baseUrl("https://api.second-domain.com/")
 *             .client(OkHttpClient.Builder().build())
 *             .addConverterFactory(GsonConverterFactory.create())
 *             .build()
 *     }
 *
 *     // 提供对应的 Service 接口实例
 *     @Provides
 *     @Singleton
 *     fun provideFirstService(@FirstApi retrofit: Retrofit): FirstService {
 *         return retrofit.create(FirstService::class.java)
 *     }
 *
 *     @Provides
 *     @Singleton
 *     fun provideSecondService(@SecondApi retrofit: Retrofit): SecondService {
 *         return retrofit.create(SecondService::class.java)
 *     }
 * }
 */
@Module
@InstallIn(SingletonComponent::class)
class DINetworkModule {

    /**
     * [OkHttpClient]依赖提供方法
     *
     * @return OkHttpClient
     */

    @Singleton
    @Provides
    fun provideOkHttpClient(): OkHttpClient {
        // 日志拦截器部分
        val logInterceptor = HttpLoggingInterceptor().setLevel(BODY)

        return OkHttpClient.Builder()
            .connectTimeout(15L * 1000L, TimeUnit.MILLISECONDS)
            .readTimeout(20L * 1000L, TimeUnit.MILLISECONDS)
            .addInterceptor(logInterceptor)
            .retryOnConnectionFailure(true)
            .build()
    }

    /**
     * 项目主要服务器地址的[Retrofit]依赖提供方法
     *
     * @param okHttpClient OkHttpClient OkHttp客户端
     * @return Retrofit
     */
    @FirstApi
    @Singleton
    @Provides
    fun provideMainRetrofit(okHttpClient: OkHttpClient): Retrofit {
        return Retrofit.Builder()
            .baseUrl(NetBaseUrlConstant.MAIN_URL)
//            .addConverterFactory(GsonConverterFactory.create())
            .addConverterFactory(ScalarsConverterFactory.create())
            .client(okHttpClient)
            .build()
    }
    @SecondApi
    @Singleton
    @Provides
    fun provideDevRetrofit(okHttpClient: OkHttpClient): Retrofit {
        return Retrofit.Builder()
            .baseUrl("https://api.simboss.com/")
//            .addConverterFactory(GsonConverterFactory.create())
            .addConverterFactory(ScalarsConverterFactory.create())
            .client(okHttpClient)
            .build()
    }
}