package com.qimai.android.fetch.retrofit

import android.content.Context
import com.qimai.android.fetch.gson.GsonAdapter
import okhttp3.HttpUrl
import okhttp3.OkHttpClient
import retrofit2.CallAdapter
import retrofit2.Converter
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

/**
 * @author wangwei

 * @version 1.1.1
 * @date 2019-08-13
 * @Description
 * @email wangwei_5521@163.com
 **/
class RetrofitBuilder private constructor() {
    var baseUrl: String? = null

    var callAdapterFactory: MutableList<CallAdapter.Factory> = mutableListOf()

    var converterFactory: MutableList<Converter.Factory> = mutableListOf()

    var okHttpClient: OkHttpClient? = null

    var muHostUrl = mutableMapOf<String, HttpUrl>()

    companion object {
        val instance: RetrofitBuilder by lazy {
            RetrofitBuilder()
        }
    }


    class Builder {
        private var baseUrl: String? = null

        private var callAdapterFactory: MutableList<CallAdapter.Factory> = mutableListOf()

        private var converterFactory: MutableList<Converter.Factory> = mutableListOf()

        private var okHttpClient: OkHttpClient? = null
        private var muHostUrl = mutableMapOf<String, HttpUrl>()
        private var context: Context? = null

        fun baseUrl(baseUrl: String) = apply {
            this.baseUrl = baseUrl
        }

        fun callAdapterFactory(callaAdapterFactory: MutableList<CallAdapter.Factory>) = apply {
            this.callAdapterFactory = callaAdapterFactory
        }

        fun context(context: Context) = apply {
            this.context = context
        }

        fun convertFactory(conventFactorys: MutableList<Converter.Factory>) = apply {
            this.converterFactory = conventFactorys
        }

        fun okhttpClient(okHttpClient: OkHttpClient) = apply {
            this.okHttpClient = okHttpClient
        }

        fun muHostUrl(muHost: MutableMap<String, HttpUrl>) = apply {
            this.muHostUrl + muHost
        }


        private fun doDefault(retrofitBuilder: RetrofitBuilder) {
            if (retrofitBuilder.converterFactory.isEmpty()) {
                retrofitBuilder.converterFactory.add(GsonConverterFactory.create(GsonAdapter.buildGson()))
            }
        }

        fun build(): Retrofit {

            val retrofitBuilder = RetrofitBuilder()
            retrofitBuilder.baseUrl = this.baseUrl
            retrofitBuilder.callAdapterFactory = this.callAdapterFactory
            retrofitBuilder.okHttpClient = this.okHttpClient
            retrofitBuilder.converterFactory = this.converterFactory
            retrofitBuilder.muHostUrl + this.muHostUrl
            doDefault(retrofitBuilder)
            val builder = Retrofit.Builder()
            callAdapterFactory.forEach {
                builder.addCallAdapterFactory(it)
            }
            converterFactory.forEach {
                builder.addConverterFactory(it)
            }
            return builder
                .baseUrl(baseUrl)
                .client(okHttpClient)
                .build()
        }
    }
}