package com.yidian.yac.newscontent.app

import android.content.Context
import android.content.Intent
import com.mob.MobSDK
import com.yidian.chameleon.*
import com.yidian.chameleon.parser.DynamicUiViewParserFactory
import com.yidian.common.ParcelizeMap
import com.yidian.common.XRouterPathConstants.Companion.NEWS_CONTENT
import com.yidian.common.XRouterPathConstants.Companion.NEWS_MAIN
import com.yidian.common.YacBaseApplication
import com.yidian.common.chameleon.ChameleonServiceManager
import com.yidian.common.chameleon.ChameleonServiceRouter
import com.yidian.xarc.xbase.utils.XLogger
import com.yidian.yac.newscontent.action.ChameleonActionExecutor
import com.yidian.yac.newscontent.ui.MainActivity
import com.yidian.yac.newscontent.ui.NewsContentActivity
import com.yidian.xpage.XPageManager
import com.yidian.xpage.node.XPageHandler
import com.yidian.xpage.node.XPageNode
import com.yidian.xpage.node.XPageNodePageType
import com.yidian.yac.core.core.YacModuleSpec
import com.yidian.yac.pm.appupdate.PmUpdateApp
import com.yidian.yac.pm.appupdate.YDAppUpdateManager

@YacModuleSpec
class NewsContentApplication: YacBaseApplication(){
    companion object{
        lateinit var context:Context
    }

    override fun onCreate() {
        super.onCreate()
        MobSDK.submitPolicyGrantResult(true, null)
        context= this
        initChameleon()
        initChameleonService()
        val logger = XLogger("apkupdate")
        logger.debugOn(BuildConfig.DEBUG)
        YDAppUpdateManager.init(PmUpdateApp(applicationContext) {
            online = BuildConfig.DEBUG //线上true、线下false
            platformId = 18//平台Id需要申请
            platform = "Android"//ios/Androiid
            appId = "com.yidian.shenghuoquan"//应用包名
            appVersion = "1.0.0"//客户端版本号
            deviceFinger = "xcfasdfadsfasdfadsfas"//设备指纹
            androidChannel = "oppo"//渠道
            this.logger = logger.subLogger("apkupdate")
        })






        XPageManager.registeredNodes(arrayListOf(
            XPageNode(NEWS_MAIN, XPageNodePageType.NATIVE, object : XPageHandler {
                override fun handler(params: Map<String, Any?>?) {
                    val intent = Intent()
                    val parcelizeMap = ParcelizeMap(params as Map<String, String?>?)
                    intent.putExtra("paramsMap", parcelizeMap)
                    intent.setClass(context, MainActivity::class.java)
                    intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
                    context.startActivity(intent)
                }
            }),
            XPageNode(NEWS_CONTENT, XPageNodePageType.NATIVE, object : XPageHandler {
                override fun handler(params: Map<String, Any?>?) {
                    val intent = Intent()
                    val parcelizeMap = ParcelizeMap(params as Map<String, String?>?)
                    intent.putExtra("paramsMap", parcelizeMap)
                    intent.setClass(context, NewsContentActivity::class.java)
                    intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
                    context.startActivity(intent)
                }
            })
        ))
    }

    private fun initChameleonService() {
        ChameleonServiceManager.registerChameleonService(ChameleonActionExecutor())
    }

    private fun initChameleon() {

        val chameleon = Chameleon.Builder()
            .addViewParserFactory(DynamicUiViewParserFactory())
            .addSetAttributeMethodMap(DefaultSetAttributeMethodProvider.getInstance().map)
            .addBindDataMethodMap(DefaultBindingDataMethodProvider.getInstance().map)
            .setJsNativeBridgeService(ChameleonServiceRouter())
            .assetPath("chameleon")
            .apiVersion("123")
            .platformId(26)
            .appId("com.cutt.zhiyue.android")
            .appVersion("1.0")
            .build()
        //todo 增加chameleon下载方法、版本获取
        chameleon.init(this)
        chameleon.isDebugMode = true
        val debugRemote = true
        if (debugRemote) {
            chameleon.setRemoteMode(false, "http://10.60.105.173:8888/")
        } else {
            chameleon.setRemoteMode(false, "")
        }
    }

}