package com.za.service.jpush

import android.content.Context
import android.util.Log
import cn.jpush.android.api.CmdMessage
import cn.jpush.android.api.JPushMessage
import cn.jpush.android.service.JPushMessageReceiver
import com.za.common.log.LogUtil

/**
 * 自定义JPush message 接收器,包括操作tag/alias的结果返回(仅仅包含tag/alias新接口部分)
 */
class MyJPushMessageReceiver : JPushMessageReceiver() {
    override fun onRegister(context: Context, s: String) {
        super.onRegister(context, s)
        LogUtil.print("JPushConnected onRegister", "REGID：$s")
    }

    override fun onConnected(context: Context, b: Boolean) {
        super.onConnected(context, b)
        LogUtil.print("JPushConnected", "极光连接情况：$b")
        //        Global.isJpushConnected = b;
//        if (b && null != Global.VEHICLE_INFO && !Global.isAliasSuccess)
//            JPushInterface.setAlias(context, 5, AppUrlConfig.isRelease ? (Global.VEHICLE_INFO.vehicleId + "") : (Global.JPUSH_PREFIX + Global.VEHICLE_INFO.vehicleId));
    }

    override fun onTagOperatorResult(context: Context, jPushMessage: JPushMessage) {
//        TagAliasOperatorHelper.getInstance().onTagOperatorResult(context, jPushMessage);
        super.onTagOperatorResult(context, jPushMessage)
    }

    override fun onCheckTagOperatorResult(context: Context, jPushMessage: JPushMessage) {
//        TagAliasOperatorHelper.getInstance().onCheckTagOperatorResult(context, jPushMessage);
        super.onCheckTagOperatorResult(context, jPushMessage)
    }

    override fun onAliasOperatorResult(context: Context, jPushMessage: JPushMessage) {
//        LogUtil.getInstance().print("极光 alias", "0->" + JSON.toJSONString(jPushMessage));
//        TagAliasOperatorHelper.getInstance().onAliasOperatorResult(context, jPushMessage);
        super.onAliasOperatorResult(context, jPushMessage)
    }

    override fun onMobileNumberOperatorResult(context: Context, jPushMessage: JPushMessage) {
//        TagAliasOperatorHelper.getInstance().onMobileNumberOperatorResult(context, jPushMessage);
        super.onMobileNumberOperatorResult(context, jPushMessage)
    }

    override fun onCommandResult(context: Context, cmdMessage: CmdMessage) {
        //注册失败+三方厂商注册回调
        LogUtil.print("极光 厂商", "[onCommandResult] $cmdMessage");
        //cmd为10000时说明为厂商token回调
        if (cmdMessage.cmd == 10000 && cmdMessage.extra != null) {
            val token = cmdMessage.extra.getString("token")
            val platform = cmdMessage.extra.getInt("platform")
            var deviceName = "unkown"
            when (platform) {
                1 -> deviceName = "小米"
                2 -> deviceName = "华为"
                3 -> deviceName = "魅族"
                4 -> deviceName = "OPPO"
                5 -> {
                    deviceName = "VIVO"
                    Log.e("厂商", "[onCommandResult] vivo:$token")
                }

                8 -> deviceName = "FCM"
            }
            LogUtil.print("极光 厂商", "获取到 $deviceName 的token:$token");
        }
    }
}
