package com.adealink.weparty.webview

import android.util.Log
import com.adealink.frame.data.json.froJsonErrorNull
import com.adealink.frame.data.json.toJsonErrorNull
import com.adealink.frame.util.getHost
import com.adealink.frame.util.getPrimaryDomain
import com.adealink.weparty.webview.jsbridge.config.IJSBridgeConfig
import com.adealink.weparty.config.GlobalConfigType
import com.adealink.weparty.config.IGlobalConfigListener
import com.adealink.weparty.config.globalConfigManager
import com.adealink.weparty.network.CDN_HOST
import com.adealink.weparty.network.DEBUG_HOST_CN
import com.adealink.weparty.network.RELEASE_HOSTS
import com.adealink.weparty.network.WEB_PRIMARY_DOMAINS
import com.adealink.weparty.webview.constant.TAG_WEB_VIEW_WHITE_HOST
import java.util.*
import java.util.concurrent.ConcurrentHashMap

class JSBridgeConfig : IJSBridgeConfig, IGlobalConfigListener {

    private val whiteUrlSet = Collections.newSetFromMap(ConcurrentHashMap<String, Boolean>())
    private val blackUrlSet = Collections.newSetFromMap(ConcurrentHashMap<String, Boolean>())

    init {
        whiteUrlSet.addAll(froJsonErrorNull<Set<String>>(JSBridgePrefs.whiteHostSet) ?: setOf())
        // TODO: 白名单
//        globalConfigManager.addListener(GlobalConfigType.GLOBAL_JS_HOST_WHITELIST, this)
//        addWhiteConfig(
//            globalConfigManager.getConfig(GlobalConfigType.GLOBAL_JS_HOST_WHITELIST)?.getOrNull(0)
//        )
        //hardcode 白名单域名
        addWhiteList(RELEASE_HOSTS)
        addWhiteList(listOf(CDN_HOST))
        addWhiteList(listOf(DEBUG_HOST_CN))
        addWhiteList(WEB_PRIMARY_DOMAINS)
    }

    override fun onConfigGet(configType: GlobalConfigType, config: List<String>) {
//        if (configType == GlobalConfigType.GLOBAL_JS_HOST_WHITELIST) {
//            addWhiteConfig(config.getOrNull(0))
//        }
    }

    private fun addWhiteConfig(config: String?) {
        val jsHostWhiteList = froJsonErrorNull<List<String>>(config)
        if (jsHostWhiteList != null) {
            addWhiteList(jsHostWhiteList)
        }
    }

    override fun addWhiteList(hosts: List<String>) {
        Log.i(TAG_WEB_VIEW_WHITE_HOST, "addWhiteList:$hosts")
        val added = whiteUrlSet.addAll(hosts.filter { it.isNotEmpty() })
        if (added) {
            JSBridgePrefs.whiteHostSet = toJsonErrorNull(whiteUrlSet) ?: ""
        }
    }

    override fun addBlackList(hosts: List<String>) {
        blackUrlSet.addAll(hosts.filter { it.isNotEmpty() })
    }

    override fun isWhite(url: String): Boolean {
        val host = getHost(url)
        val primaryDomain = getPrimaryDomain(url)
        val whiteHost = whiteUrlSet.find { it == primaryDomain || it == host }
        Log.d(TAG_WEB_VIEW_WHITE_HOST, "isWhite, url:$url, primaryDomain:$primaryDomain, whiteHost:$whiteHost")
        return whiteHost != null || WebModule.getOfflineH5Urls().contains(url)
    }

    override fun isBlack(url: String): Boolean {
        return blackUrlSet.contains(getHost(url))
    }

}