package com.dahe.gldriver.push

import android.app.Notification
import android.content.Context
import android.util.Log
import com.dahe.gldriver.utils.SPUtils
import com.dahe.glex.bean.UserBean
import com.umeng.message.PushAgent
import com.umeng.message.UmengMessageHandler
import com.umeng.message.UmengNotificationClickHandler
import com.umeng.message.entity.UMessage

/**
 * PushSDK集成帮助类
 */
object PushHelper {
    const val TAG = "UPushDemo"

    /**
     * 推送设置
     */
    fun setting(api: PushAgent) {
        //修改为您app/src/main/AndroidManifest.xml中package值
        api.resourcePackageName = "com.dahe.gldriver"

        //设置通知栏显示通知的最大个数（0～10），0：不限制个数
        api.displayNotificationNumber = 0

        //推送消息处理
        val msgHandler: UmengMessageHandler = object : UmengMessageHandler() {
            //处理通知栏消息
            override fun dealWithNotificationMessage(context: Context, msg: UMessage) {
                super.dealWithNotificationMessage(context, msg)
                Log.i(TAG, """notification receiver:${msg.raw}""".trimIndent())
            }

            //自定义通知样式，此方法可以修改通知样式等
            override fun getNotification(context: Context, msg: UMessage): Notification {
                return super.getNotification(context, msg)
            }

            //处理透传消息
            override fun dealWithCustomMessage(context: Context, msg: UMessage) {
                super.dealWithCustomMessage(context, msg)
                Log.i(
                    TAG, """custom receiver:${msg.raw}""".trimIndent()
                )
            }
        }
        api.messageHandler = msgHandler

        //推送消息点击处理
        val notificationClickHandler: UmengNotificationClickHandler =
            object : UmengNotificationClickHandler() {
                override fun openActivity(context: Context, msg: UMessage) {
                    super.openActivity(context, msg)
                    Log.i(TAG, """click open activity:${msg.raw}""".trimIndent())
                }

                override fun launchApp(context: Context, msg: UMessage) {
                    super.launchApp(context, msg)
                    Log.i(TAG, """click launch app:${msg.raw}""".trimIndent())
                }

                override fun openUrl(context: Context, msg: UMessage) {
                    super.openUrl(context, msg)
                    Log.i(TAG, """click open deeplink:${msg.raw}""".trimIndent())
                }

                override fun dismissNotification(context: Context, msg: UMessage) {
                    super.dismissNotification(context, msg)
                    Log.i(TAG, """dismissNotification:${msg.raw}""".trimIndent())
                }
            }
        api.notificationClickHandler = notificationClickHandler

        //通过Service自定义接收并处理消息
//        api.setPushIntentServiceClass(MyCustomMessageService.class);

        //设置厂商Token回调
        api.setThirdTokenCallback { type, token -> Log.i(TAG, "push type:$type token:$token") }
    }

    /**
     * 注册设备推送通道（小米、华为等设备的推送）
     */
    fun registerDevicePush(context: Context?) {
        //小米推送：填写您在小米后台APP对应的xiaomi id和key
//        MiPushRegistar.register(context, PushConstants.MI_ID, PushConstants.MI_KEY);
//        //华为推送：注意华为推送的初始化参数在AndroidManifest.xml中配置
//        HuaWeiRegister.register(context.getApplicationContext());
//        //魅族推送：填写您在魅族后台APP对应的app id和key
//        MeizuRegister.register(context, PushConstants.MEI_ZU_ID, PushConstants.MEI_ZU_KEY);
//        //OPPO推送：填写您在OPPO后台APP对应的app key和secret
//        OppoRegister.register(context, PushConstants.OPPO_KEY, PushConstants.OPPO_SECRET);
//        //vivo推送：注意vivo推送的初始化参数在AndroidManifest.xml中配置
//        VivoRegister.register(context);
//        //荣耀推送：注意荣耀推送的初始化参数在AndroidManifest.xml中配置
//        HonorRegister.register(context);
//        //谷歌fcm推送
//        FCMRegister.register(context);
    }

    /**
     * 绑定别名，绑定成功后可通过别名推送消息
     */
    fun setAlias(context: Context, userBean: UserBean) {
        val alias = userBean.userid
        val type = "uid"
        PushAgent.getInstance(context).setAlias(alias, type) { success, message ->
            val msg: String
            msg = if (success) {
                "set alias success! type:$type alias:$alias"
            } else {
                "set alias failure! msg:$message"
            }
        }
    }

    /**
     * 删除别名
     */
    fun delete(context: Context) {
        val alias = SPUtils.instance.getUserInfo(context)?.userid
        val type = "uid"
        PushAgent.getInstance(context).deleteAlias(alias, type) { success, message ->
            val msg: String
            msg = if (success) {
                "delete alias success! type:$type alias:$alias"
            } else {
                "delete alias failure! msg:$message"
            }
        }
    }

}
