package com.ydl.ydlcommon.base.config

import android.content.Context
import com.ydl.ydlcommon.base.BaseApp
import com.ydl.ydlcommon.utils.YDLPreconditions
import com.ydl.ydlnet.YDLHttpUtils
import okhttp3.OkHttpClient

/**
 * Created by haorui on 2019-08-29 .
 * Des: 模块配置类，使用建造者模式注入外部配置
 */
class GlobalConfig private constructor(var builder: Builder) {
    var appFrom: String = YDLConstants.FROM_YDL
    var appEnv: String = YDLConstants.ENV_PROD
    var appDebug: Boolean = false

    init {
        this.appFrom = builder.appFrom
        this.appEnv = builder.appEnv
        this.appDebug = builder.appDebug
    }

    fun initEnv() {
        builder.initEnv(BaseApp.instance)
    }

    class Builder() {

        private var okHttpClient: OkHttpClient? = null
        var appFrom: String = YDLConstants.FROM_YDL
        var appEnv: String = YDLConstants.ENV_PROD
        var appBaseURL: String? = null
        var multipleUrl: HashMap<String, String> = hashMapOf()
        var appDebug: Boolean = false

        fun setOkHttpClient(okHttpClient: OkHttpClient) {
            this.okHttpClient = okHttpClient
        }

        fun setBaseURL(baseURL: String) {
            this.appBaseURL = baseURL
        }

        fun setDebug(isDebug: Boolean) {
            this.appDebug = isDebug
        }

        /**
         * 设置除了默认的BaseUrl的其他URL
         */
        fun setMultipleUrlMap(multipleUrl: HashMap<String, String>): Builder {
            this.multipleUrl.putAll(multipleUrl);
            return this
        }

        fun addUrl(domainName: String, domainUrl: String): Builder {
            YDLPreconditions.checkEmpty(domainName, "%s cannot be empty", "domainName")
            YDLPreconditions.checkEmpty(domainUrl, "%s cannot be empty", "domainUrl")
            multipleUrl[domainName] = domainUrl
            return this
        }

        fun setFrom(@YDLConstants.AppFrom appFrom: String): Builder {
            this.appFrom = appFrom
            return this
        }

        fun setEnv(@YDLConstants.AppEnv appEnv: String): Builder {
            this.appEnv = appEnv
            return this
        }

        fun build(): GlobalConfig {
            return GlobalConfig(this)
        }

        fun initEnv(context: Context) {
            HttpConfig.getInstance().initEnv(context, appEnv)
            val apiFactory = YDLHttpUtils
                .getInstance()
                .init(context)
                .config()
                .setBaseUrl(appBaseURL ?: HttpConfig.PHP_BASE_URL)
                .addUrl(YDL_DOMAIN_JAVA, HttpConfig.JAVA_BASE_URL)
                .addUrl(YDL_DOMAIN_COUPON_URL, HttpConfig.SERVER_COUPON_URL)
                .setMultipleUrlMap(multipleUrl)

            if (okHttpClient == null) {
                okHttpClient = HttpConfig.getInstance().createOkHttp(context, appFrom, appDebug)
            }

            apiFactory.setOkClient(okHttpClient)
        }
    }

    companion object {
        fun builder(): Builder {
            return Builder()
        }
    }
}
