package com.yidian.shenghuoquan

import android.content.Context
import android.content.res.Configuration
import com.yidian.common.YacBaseApplication
import com.yidian.common.utils.StorageManager
import com.yidian.common.utils.XLogUtil
import com.yidian.commonutil.CommonUtilLifecycleModule
import com.yidian.news.util.*
import com.yidian.xarc.bridge.FlutterBridge
import com.yidian.xarc.service.XEventService
import com.yidian.xarc.services.XPageService
import com.yidian.xarc.xbrid.*
import com.yidian.xpage.XPageManager
import com.yidian.yac.core.core.YacAppSpec
import com.yidian.yac.core.zap.Zap
import org.json.JSONObject
import service.FlutterNodesSyncService
import service.VirtualFlutterNodeService

@YacAppSpec
class YdMainApplication : YacBaseApplication() {
    private val module: CommonUtilLifecycleModule by lazy {
        CommonUtilLifecycleModule(
            BuildConfig.DEBUG,
            this,
            ".CommonInfrastructure"
        )
    }
    private val onlyNativeProject: Boolean = BuildConfig.ONLY_NATIVE_PROJECT
    private val isDebug: Boolean = BuildConfig.DEBUG
    override fun onCreate() {
        super.onCreate()
        XLogUtil.debugOn(BuildConfig.DEBUG)
        if (ProcessUtil.isMainProcess(this)) {
            context = this
            Zap.init(context) {}
            XPageManager.init(context)
            initXBridManager()
            initCommonUtils()
            if (!onlyNativeProject) FlutterBridge.init()
            loadService()
            StorageManager.init(this)
        }
    }

    private fun initXBridManager() {
        XBridManager.init(
            XBridApp(context) {
                online = false
                appId = "com.yidian.shenghuoquan"
                platformId = 16
                appVersion = "0.0.0.1"
                deviceFinger = "1234567"
                androidChannel = "oppo"
                assetRoot = "www"
            }
        )
        XBridManager.registerXPageInvoker(object : OnXPageInvoke {
            override fun onCall(identifier: String, xWebView: XWebView, request: XPageRequest) {
                val params = mutableMapOf<String, Any>()
                (request.params as JSONObject).let { reParams ->
                    reParams.keys().forEach {
                        params[it] = reParams[it]
                    }
                }
                when (request.action) {
                    "push" -> XPageManager.push(request.name, params)
                    "pop" -> XPageManager.pop(params)
                    "popTo" -> XPageManager.popTo(request.name, params)
                    "popToRoot" -> XPageManager.popToRoot(params)
                }
            }
        })
        XBridManager.registerServiceInvoker(object : OnServiceInvoke {
            override fun onCall(
                identifier: String, // the identifier assigned when webview created
                xWebView: XWebView, // webivew 加载的 js 发起的服务调用请求
                request: Request, // 具体的服务请求参数，分为 name 和 params 两个部分
                response: Response // 业务方需要借助 response 来回应服务调用结果
            ) {
                var params = request.params as JSONObject
                XLogUtil.e("zhb-yd", "JS传递参数：$params")
                when (request.name) {
                    "/test/a" -> {

                    }
                }
            }
        })
    }

    override fun attachBaseContext(base: Context?) {
        super.attachBaseContext(base)
        if (ProcessUtil.isMainProcess(this)) {
            module.onBaseContextAttached(base)
        }
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        if (ProcessUtil.isMainProcess(this)) {
            module.onConfigurationChanged(newConfig)
        }
    }

    private fun initCommonUtils() {
        module.onCreate()
        ContextUtil.init(this)
        DensityUtil.init(this)
        PackageUtils.init(this)
        DensityUtil.init(this)
    }


    private fun loadService() {
        if (!onlyNativeProject) {
            Zap.addService(VirtualFlutterNodeService.name, VirtualFlutterNodeService())
            Zap.addService(FlutterNodesSyncService.name, FlutterNodesSyncService())
            Zap.addService(XEventService.name, XEventService())
        }
        Zap.addService(XPageService.name, XPageService())
    }
}