package com.tool.elite.battery.business.push

import android.app.Notification
import android.content.AsyncQueryHandler
import android.content.ContentResolver
import android.content.ContentValues
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle


/**
 *Create by SleepDog on 2024-11-08
 */
object BadgeUtils {

    fun setBadgeNum(context: Context, count: Int, notification: Notification? = null) {
        when (OEM.current) {
            Samsung -> setSamsungBadge(context, count)
            is Xiaomi -> setMiUiBadge(notification, count)
            Motorola, Android, is Huawei, is Meizu, is Oppo, is Vivo -> {}
        }
    }

    private fun setSamsungBadge(context: Context, count: Int) {
        val intent = Intent("android.intent.action.BADGE_COUNT_UPDATE")
        intent.putExtra("badge_count", count)
        intent.putExtra("badge_count_package_name", context.packageName)
        intent.putExtra(
            "badge_count_class_name",
            context.packageManager.getLaunchIntentForPackage(context.packageName)?.component?.className
        )
        context.sendBroadcast(intent)
    }

    private fun setMiUiBadge(notification: Notification?, count: Int) {
        if (notification == null) return
        //消息类型不能是进度条样式和常驻通知，注意需要 setOngoing(false)
        runCatching {
            val field = notification.javaClass.getDeclaredField("extraNotification")
            val extraNotification = field.get(notification)
            val method = extraNotification.javaClass.getDeclaredMethod(
                "setMessageCount", Int::class.java
            )
            method.invoke(extraNotification, count)
        }
    }

    private fun setHuaweiBadge(count: Int, context: Context) {
        runCatching {
            val bundle = Bundle()
            bundle.putString("package", context.packageName)
            bundle.putString(
                "class",
                context.packageManager.getLaunchIntentForPackage(context.packageName)?.component?.className
            )
            bundle.putInt("badgenumber", count)
            context.contentResolver.call(
                Uri.parse(
                    "content://com.huawei.android.launcher" +
                            ".settings/badge/"
                ), "change_badge", null, bundle
            )
        }
    }


    private fun setSonyBadge(count: Int, context: Context) {
        val launcherClassName =
            context.packageManager.getLaunchIntentForPackage(context.packageName)?.component?.className
        try {
            //官方给出方法
            val contentValues = ContentValues()
            contentValues.put("badge_count", count)
            contentValues.put("package_name", context.packageName)
            contentValues.put(
                "activity_name", launcherClassName
            )
            val asyncQueryHandler = SonyAsyncQueryHandler(context.contentResolver)
            asyncQueryHandler.startInsert(
                0, null, Uri.parse(
                    "content://com.sonymobile.home" +
                            ".resourceprovider/badge"
                ), contentValues
            )

        } catch (e: Exception) {
            try {
                //网上大部分使用方法
                val intent = Intent("com.sonyericsson.home.action.UPDATE_BADGE")
                intent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE", count > 0)
                intent.putExtra(
                    "com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME",
                    launcherClassName
                )
                intent.putExtra(
                    "com.sonyericsson.home.intent.extra.badge.MESSAGE",
                    count.toString()
                )
                intent.putExtra(
                    "com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME", context
                        .packageName
                )
                context.sendBroadcast(intent)
            } catch (e1: Exception) {
                e1.printStackTrace()
            }
        }
    }

    private class SonyAsyncQueryHandler internal constructor(cr: ContentResolver?) :
        AsyncQueryHandler(cr)
}