package com.shenghuoquan.business

import android.app.Activity
import android.content.Context
import android.content.res.Configuration
import android.os.Bundle
import com.orhanobut.hawk.Hawk
import com.yidian.common.YdBaseApplication
import com.yidian.common.http.ApiSaveCookiesInterceptor
import com.yidian.common.http.ApiSetCookiesInterceptor
import com.yidian.common.services.AppUpgradeService
import com.yidian.common.services.ImageService
import com.yidian.common.services.XEventService
import com.yidian.common.services.XPageService
import com.yidian.common.services.http.MBHttpReqService
import com.yidian.common.utils.UMConfigUtils
import com.yidian.commonutil.CommonUtilLifecycleModule
import com.yidian.http.ServiceFactory
import com.yidian.news.util.ProcessUtil
import com.yidian.xpage.XPageManager
import com.yidian.yac.core.core.YacAppSpec
import com.yidian.yac.core.zap.Zap
import timber.log.Timber


@YacAppSpec
class YdSubwayApplication : YdBaseApplication() {

  override fun onCreate() {
    super.onCreate()
    if (ProcessUtil.isMainProcess(this)) {
      context = this
      // 先初始化日志防止调用时报初始化异常
      if (BuildConfig.DEBUG) {
        Timber.plant(Timber.DebugTree())
      }
      initCommonUtils()
      Zap.init(this) {}
      XPageManager.init(this)
      loadService()
      registerActivityLifecycleCallbacks(activityLifecycle)
      UMConfigUtils.preInit(context)
    }
  }

  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()
    Hawk.init(this).build()
    initHttp()
  }

  private val module: CommonUtilLifecycleModule by lazy {
    CommonUtilLifecycleModule(
      com.yidian.xarc.xbrid.BuildConfig.DEBUG,
      this,
      ".CommonInfrastructure"
    )
  }

  private val activityLifecycle = object : ActivityLifecycleCallbacks {

    override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
      activityList.add(activity)
    }

    override fun onActivityStarted(activity: Activity) {

    }

    override fun onActivityResumed(activity: Activity) {

    }

    override fun onActivityPaused(activity: Activity) {

    }

    override fun onActivityStopped(activity: Activity) {

    }

    override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {

    }

    override fun onActivityDestroyed(activity: Activity) {
      activityList.remove(activity)
    }

  }

  private fun initHttp() {
//        val header = HashMap<String, String>()
//        header["Content-Type"] = "application/json"
//        header["charset"] = "UTF-8"
//        ServiceFactory.getInstance().addInterceptor(HttpInterceptor(header))
//        ServiceFactory.getInstance().init(true, URLs.BASE_URL, 10, 10)
    ServiceFactory.getInstance().init(true, BuildConfig.API_URL, 10, 10)
    ServiceFactory.getInstance().addInterceptor(ApiSaveCookiesInterceptor())
    ServiceFactory.getInstance().addInterceptor(ApiSetCookiesInterceptor())

  }

  private fun loadService() {
    Zap.addService(XPageService.SERVICE_NAME, XPageService())
    Zap.addService(XEventService.SERVICE_NAME, XEventService())
    Zap.addService(AppUpgradeService.SERVICE_NAME, AppUpgradeService())
    Zap.addService(MBHttpReqService.SERVICE_NAME, MBHttpReqService())
    Zap.addService(ImageService.SERVICE_NAME, ImageService())
  }

}
