package com.yidian.bcommon.utils

import android.content.Context
import android.os.Bundle
import com.orhanobut.hawk.Hawk
import com.umeng.analytics.MobclickAgent
import com.umeng.commonsdk.UMConfigure
import com.umeng.message.entity.UMessage
import com.umeng.umcrash.UMCrash
import com.umeng.umcrash.UMCrashCallback
import com.yidian.bcommon.constant.AppConfig
import com.yidian.bcommon.constant.HawkConfig
import com.yidian.bcommon.constant.RunConfig
import com.yidian.bcommon.constant.XRouterPathConstants
import com.yidian.xarc.xbase.utils.ChannelUtil
import com.yidian.xpage.XPageManager


/**
 * 友盟错误统计、push辅助类
 * */
class UMConfigUtils {
  companion object {

    /**
     * 正式初始化
     */
    fun init(context: Context) {
      //release环境上报错误信息，参数有：上下文、APPKey、渠道号、设备类型、推送密钥
      if (RunConfig.IS_UMENG_RELEASE) {
        val channelValue = ChannelUtil.getChannelFromApk(context, "channel")
        UMConfigure.init(context, AppConfig.UMAppKey, channelValue, UMConfigure.DEVICE_TYPE_PHONE, AppConfig.UMPushSecret)
      } else {
        UMConfigure.init(
          context,
          AppConfig.UMAppTestKey,
          AppConfig.UMTestChannel,
          UMConfigure.DEVICE_TYPE_PHONE,
          AppConfig.UMTestPushSecret
        )
      }
      //选择AUTO页面采集模式，统计SDK基础指标无需手动埋点可自动采集。建议在宿主App的Application.onCreate函数中调用此函数。
      MobclickAgent.setPageCollectionMode(MobclickAgent.PageMode.AUTO)
      // 友盟崩溃的回调,上传手机号
      UMCrash.registerUMCrashCallback { Hawk.get(HawkConfig.Mobile, "") }
    }

    private fun goToJump(p1: UMessage?) {
      //点击跳转参数
      val paramsMap = HashMap<String, Any?>()
      //接收到的参数
      val extras: Map<String, String>? = p1?.extra
      val b = Bundle()
      if (extras != null) {
        for (key in extras.keys) {
          b.putString(key, extras[key])
        }
      }

      //配置数据
      if (b.get("rstype") != null && b.get("docid") != null) {
        paramsMap["jumpFrom"] = "PushJump"
        paramsMap["doc_id"] = b.get("docid")
        when (b.get("rstype")) {
          "normal" -> paramsMap["content_type"] = "text_detail"
          "video" -> paramsMap["content_type"] = "video_detail"
        }
        XPageManager.push(XRouterPathConstants.NEWS_MAIN, paramsMap)
      }
    }
//                true -> {
//                    //debug环境打开日志输出，默认关闭
//                    UMConfigure.setLogEnabled(true)
//                }
//            }
//        }
  }
}
