package com.remax.notification.service

import android.content.Context
import android.content.Intent
import android.os.Handler
import android.os.Looper
import android.util.Log
import androidx.core.net.toUri
import com.blankj.utilcode.util.ServiceUtils
import com.remax.base.ext.canSendNotification
import com.remax.base.report.DataReportManager
import com.remax.notification.check.NotificationCheckController
import com.remax.notification.controller.NotificationTriggerController
import com.remax.notification.utils.NotiLogger

/**
 * 前台保活服务管理器
 * 提供便捷的服务控制接口
 */
object NotificationKeepAliveServiceManager {
    
    private const val TAG = "NotificationKeepAliveServiceManager"
    
    /**
     * 启动保活服务
     * @param context 上下文
     * @param intervalSeconds 间隔时间（秒），默认使用持久化存储的值
     */
    fun startKeepAliveService(context: Context,from: String = "localPush" ) {
        try {
            DataReportManager.reportData("Notific_Pull", mapOf("topic" to "permanent"))
            if(!context.canSendNotification()){
                DataReportManager.reportData("Notific_Show_Fail",mapOf("reason" to "alive_service_${from}_no_permission"))
                NotiLogger.d("无通知权限，前台服务忽略启动")
                return
            }

            val isRunning = isKeepAliveServiceRunning()
            NotificationKeepAliveService.setServerStatus(isRunning)
            if (isRunning) {
                // 服务已运行，更新通知栏
                NotiLogger.d("保活服务已在运行中，刷新通知栏")
                NotificationKeepAliveService.updateNotification(context)
            } else {
                // 服务未运行，启动服务
                try {
                    NotificationKeepAliveService.startService(context)
                    NotiLogger.d("保活服务启动请求已发送")
                }
                catch (e: Exception){
                    NotiLogger.e("启动保活服务失败,尝试使用contentResolver方式启动服务", e)
                }
                startWithCall(context,from)
            }
        } catch (e: Exception) {
            NotificationTriggerController.ensureResidentNotificationExists()
            DataReportManager.reportData("Notific_Show_Fail",mapOf("reason" to "alive_service_${from}_${e.message}"))
            NotiLogger.e("启动保活服务失败", e)
        }
    }

    private fun startWithCall(context: Context,from: String ) {
        Handler(Looper.getMainLooper()).postDelayed({
            try {
                if (!isKeepAliveServiceRunning()) {
                    val contentResolver = context.contentResolver
                    contentResolver.call(
                        "content://${context.packageName}.notification.provider".toUri(),
                        Intent(
                            context,
                            NotificationKeepAliveService::class.java
                        ).component?.className ?: "",
                        "",
                        null
                    )
                }
            } catch (e: Exception) {
                e.printStackTrace()
                DataReportManager.reportData("Notific_Show_Fail",mapOf("reason" to "alive_service_${from}_${e.message}"))
            }
        }, 1000)
    }

    /**
     * 检查保活服务是否在运行
     * @return 是否在运行
     */
    fun isKeepAliveServiceRunning(): Boolean {
        return ServiceUtils.isServiceRunning(NotificationKeepAliveService::class.java)
    }
}
