package com.remax.notification.service

import android.app.Notification
import android.app.Service
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Handler
import android.os.IBinder
import android.os.Looper
import com.remax.base.ext.KvLongDelegate
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.controller.NotificationTriggerController.triggerFixTimeNotification
import com.remax.notification.timing.NotificationTimingController
import com.remax.notification.utils.NotiLogger
import com.remax.notification.utils.Topic

/**
 * 前台保活服务
 * 用于定期触发通知保活机制
 */
class NotificationKeepAliveService : Service() {

    companion object {
        private val NOTIFICATION_ID = NotificationTriggerController.getResidentNotificationId()

        // 默认15分钟 = 900秒
        private const val DEFAULT_INTERVAL_SECONDS = 900L
        
        // 持久化存储默认间隔时间
        var defaultIntervalSeconds by KvLongDelegate("notification_keep_alive_default_interval", DEFAULT_INTERVAL_SECONDS)

        // 服务控制参数
        private const val ACTION_START_SERVICE = "com.remax.notification.START_KEEP_ALIVE_SERVICE"
        private const val ACTION_STOP_SERVICE = "com.remax.notification.STOP_KEEP_ALIVE_SERVICE"
        private const val ACTION_UPDATE_NOTIFICATION = "com.remax.notification.UPDATE_FOREGROUND_NOTIFICATION"
        private const val EXTRA_INTERVAL_SECONDS = "interval_seconds"

        private var isServiceRunning = false
        
        /**
         * 设置默认间隔时间
         * @param seconds 间隔时间（秒）
         */
        fun setDefaultIntervalSeconds1(seconds: Long) {
            defaultIntervalSeconds = seconds
            NotiLogger.d("设置通知保活默认轮训间隔时间: ${seconds}秒")
        }

        fun setServerStatus(running: Boolean) {
            isServiceRunning = running
        }

        /**
         * 启动保活服务
         * @param context 上下文
         * @param intervalSeconds 间隔时间（秒），默认使用持久化存储的值
         */
        fun startService(context: Context, intervalSeconds: Long = defaultIntervalSeconds) {
            val intent = Intent(context, NotificationKeepAliveService::class.java).apply {
                action = ACTION_START_SERVICE
                putExtra(EXTRA_INTERVAL_SECONDS, intervalSeconds)
            }
            
            try {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    context.startForegroundService(intent)
                } else {
                    context.startService(intent)
                }
                NotiLogger.d("启动保活服务，间隔: ${intervalSeconds}秒")
            } catch (e: Exception) {
                DataReportManager.reportData("Notific_Show_Fail",mapOf("reason" to "alive_service_${e.message}"))
                NotiLogger.e("启动保活服务失败", e)
            }
        }

        /**
         * 停止保活服务
         * @param context 上下文
         */
        fun stopService(context: Context) {
            val intent = Intent(context, NotificationKeepAliveService::class.java).apply {
                action = ACTION_STOP_SERVICE
            }

            try {
                context.startService(intent)
                NotiLogger.d("停止保活服务")
            } catch (e: Exception) {
                NotiLogger.e("停止保活服务失败", e)
            }
        }


        /**
         * 更新前台服务通知
         * @param context 上下文
         */
        fun updateNotification(context: Context) {
            val intent = Intent(context, NotificationKeepAliveService::class.java).apply {
                action = ACTION_UPDATE_NOTIFICATION
            }
            
            try {
                context.startService(intent)
                NotiLogger.d("前台服务通知更新请求已发送")
            } catch (e: Exception) {
                NotiLogger.e("更新前台服务通知失败", e)
            }
        }
    }

    private var handler: Handler? = null
    private var keepAliveRunnable: Runnable? = null
    private var intervalSeconds: Long = defaultIntervalSeconds

    override fun onCreate() {
        super.onCreate()
        NotiLogger.d("保活服务创建")
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        when (intent?.action) {
            ACTION_START_SERVICE -> {
                intervalSeconds =
                    intent.getLongExtra(EXTRA_INTERVAL_SECONDS, defaultIntervalSeconds)
                startForegroundService()
            }

            ACTION_STOP_SERVICE -> {
                stopForegroundService()
            }
            
            ACTION_UPDATE_NOTIFICATION -> {
                updateForegroundNotification()
            }
        }
        return START_STICKY // 服务被杀死后自动重启
    }

    override fun onBind(intent: Intent?): IBinder? = null

    override fun onDestroy() {
        super.onDestroy()
        stopForegroundService()
        NotiLogger.d("保活服务销毁")
    }

    /**
     * 启动前台服务
     */
    private fun startForegroundService() {
        if (isServiceRunning) {
            NotiLogger.d("保活服务已在运行中，刷新通知，间隔: ${intervalSeconds}秒")
            // 服务已运行，只刷新通知
            updateForegroundNotification()
            triggerFixTimeNotification()
            return
        }
        
        // 检查通知权限
        val hasNotificationPermission = canSendNotification()
        NotiLogger.d("通知权限状态: $hasNotificationPermission")
        
        if (!hasNotificationPermission) {
            NotiLogger.w("没有通知权限，前台服务通知可能不会显示")
        }
        
        isServiceRunning = true
        
        // 创建前台通知
        val notification = createForegroundNotification()
        
        // 启动前台服务
        startForeground(NOTIFICATION_ID, notification)
        
        // 启动定时任务
        startKeepAliveTask()
        
        NotiLogger.d("保活服务启动成功，间隔: ${intervalSeconds}秒")
    }

    /**
     * 停止前台服务
     */
    private fun stopForegroundService() {
        if (!isServiceRunning) {
            return
        }

        isServiceRunning = false

        // 停止定时任务
        stopKeepAliveTask()

        // 停止前台服务
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            stopForeground(STOP_FOREGROUND_REMOVE)
        } else {
            @Suppress("DEPRECATION")
            stopForeground(true)
        }
        stopSelf()

        NotiLogger.d("保活服务停止")
    }

    /**
     * 启动保活任务
     */
    private fun startKeepAliveTask() {
        handler = Handler(Looper.getMainLooper())

        keepAliveRunnable = object : Runnable {
            override fun run() {
                if (!isServiceRunning) {
                    return
                }

                try {
                    NotiLogger.d("执行保活任务")
                    DataReportManager.reportData("Notific_Pull", mapOf("topic" to "timer"))
                    updateForegroundNotification()
                    triggerFixTimeNotification()
                    // 尝试触发保活通知
                    NotificationTimingController.getInstance().triggerNotificationIfAllowed(
                        NotificationCheckController.NotificationType.KEEPALIVE
                    )

                    NotiLogger.d("保活任务执行完成，下次间隔: ${intervalSeconds}秒")

                } catch (e: Exception) {
                    NotiLogger.e("保活任务执行失败", e)
                }

                // 调度下次执行
                handler?.postDelayed(this, intervalSeconds * 1000)
            }
        }

        // 延迟执行首次任务
        handler?.postDelayed(keepAliveRunnable!!, intervalSeconds * 1000)
        triggerFixTimeNotification()
    }

    /**
     * 停止保活任务
     */
    private fun stopKeepAliveTask() {
        keepAliveRunnable?.let { runnable ->
            handler?.removeCallbacks(runnable)
        }
        keepAliveRunnable = null
        handler = null
    }

    /**
     * 更新前台服务通知
     */
    private fun updateForegroundNotification() {
        if (!isServiceRunning) {
            NotiLogger.d("前台服务未运行，无法更新通知")
            return
        }

        if (!canSendNotification()) {
            NotiLogger.d("无通知权限，忽略本次更新通知")
            return
        }
        
        try {
            val newNotification = createForegroundNotification()
            startForeground(NOTIFICATION_ID, newNotification)
            NotiLogger.d("前台服务通知已更新")
        } catch (e: Exception) {
            NotiLogger.e("更新前台服务通知失败", e)
        }
    }

    /**
     * 创建前台通知
     */
    private fun createForegroundNotification(): Notification {
        // 使用NotificationTriggerController提供的构建函数
        return NotificationTriggerController.buildResidentNotification(this)
    }
    
}