package com.yidian.shenghuoquan.bgeneralbusiness.app

import com.yidian.bcommon.BuildConfig
import com.yidian.bcommon.app.YdBaseApplication
import com.yidian.bcommon.constant.RunConfig
import com.yidian.bcommon.http.ApiSaveCookiesInterceptor
import com.yidian.bcommon.http.ApiSetCookiesInterceptor
import com.yidian.bcommon.sdk.ClientType
import com.yidian.bcommon.sdk.JudgeClientUtils
import com.yidian.bcommon.services.ZapServiceNameConstants
import com.yidian.framework.mobile.xdiamond.SecretUtil
import com.yidian.http.ServiceFactory
import com.yidian.news.util.ProcessUtil
import com.yidian.shenghuoquan.bgeneralbusiness.service.EncryptService
import com.yidian.shenghuoquan.bgeneralbusiness.service.GeneralAbilityService
import com.yidian.shenghuoquan.bgeneralbusiness.service.net.BNAHttpReqService
import com.yidian.yac.core.core.YacModuleSpec
import com.yidian.yac.core.zap.Zap

@YacModuleSpec
class BGeneralBusinessApplication : YdBaseApplication() {

  override fun onCreate() {
    super.onCreate()
    if (ProcessUtil.isMainProcess(this)) {
      initService()
      initHttp()
    }
  }

  private fun initService() {
    Zap.addService(ZapServiceNameConstants.BNAHttpReq, BNAHttpReqService())
    Zap.addService(ZapServiceNameConstants.GeneralAbilityService, GeneralAbilityService())
    Zap.addService(ZapServiceNameConstants.EncryptService, EncryptService())
  }

  /**
   * 初始化Http框架
   */
  private fun initHttp() {
    ServiceFactory.getInstance().init(true, RunConfig.API_URL, 10, 10)
    if (JudgeClientUtils.judgeClient(packageName) == ClientType.MERCHANT_B) {
      // 添加cookie拦截器
      ServiceFactory.getInstance().addInterceptor(ApiSaveCookiesInterceptor())
      ServiceFactory.getInstance().addInterceptor(ApiSetCookiesInterceptor())
    }
    // 初始化磐石SDK
    SecretUtil.init(RunConfig.IS_ENCRYPT_DEBUG_KEY, BuildConfig.DEBUG)
  }
}
