package com.yidian.shenghuoquan.bxbirdbusiness.app

import android.content.Intent
import com.yidian.bcommon.constant.XRouterPathConstants
import com.yidian.bcommon.app.YdBaseApplication
import com.yidian.bcommon.services.ZapServiceNameConstants
import com.yidian.news.util.ProcessUtil
import com.yidian.shenghuoquan.bxbirdbusiness.service.BPayService
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.core.zap.Zap
import timber.log.Timber

@YacModuleSpec
class XBirdBusinessApplication : YdBaseApplication() {

  override fun onCreate() {
    super.onCreate()
    if (ProcessUtil.isMainProcess(this)) {
      Timber.tag("注册").e("开始注册")
      registerXPage()
      initService()
    }
  }

  private fun registerXPage() {
    ClassMapUtil.xPageNodeMap.forEach {
      val node = XPageNode(it.key, XPageNodePageType.NATIVE, object : XPageHandler {
        override fun handler(params: Map<String, Any?>?) {
          val intent = Intent()
          if (params != null) {
            intent.putExtra(XRouterPathConstants.ParamsKey, params as HashMap)
          }
          intent.setClass(context, it.value)
          intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
          context.startActivity(intent)
          Timber.e("startActivity 开启WebView ")
        }
      })
      XPageManager.registeredNode(node)
      Timber.e("注册XPage ${it.key}")
    }
  }

  private fun initService() {
    Zap.addService(ZapServiceNameConstants.BPayService, BPayService())
  }

}
