package com.yidian.shenghuoquan.bgoods.app

import android.content.Intent
import com.yidian.bcommon.XRouterPathConstants
import com.yidian.bcommon.YdBaseApplication
import com.yidian.news.util.ProcessUtil
import com.yidian.shenghuoquan.bgoods.service.GoodsPublishService
import com.yidian.shenghuoquan.bgoods.service.SelectCategoryService
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

@YacModuleSpec
class CommodityApplication : YdBaseApplication() {

  override fun onCreate() {
    super.onCreate()
    if (ProcessUtil.isMainProcess(this)) {
      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)
        }
      })
      XPageManager.registeredNode(node)
    }
  }

  private fun initService() {
    Zap.addService(SelectCategoryService.serviceName, SelectCategoryService())
    Zap.addService(GoodsPublishService.SERVICE_NAME, GoodsPublishService())
  }

}
