package com.safecheckin.app.utils

import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build
import androidx.core.app.NotificationCompat
import com.safecheckin.app.MainActivity
import com.safecheckin.app.R
import com.safecheckin.app.SafeCheckInApp
import com.safecheckin.app.service.CheckInNotificationService

object NotificationHelper {

    /**
     * 创建所有通知渠道
     */
    fun createNotificationChannels(context: Context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val notificationManager = context.getSystemService(NotificationManager::class.java)

            // 状态通知渠道 - 常驻通知，不可清除
            val statusChannel = NotificationChannel(
                SafeCheckInApp.STATUS_CHANNEL_ID,
                context.getString(R.string.channel_status_name),
                NotificationManager.IMPORTANCE_LOW
            ).apply {
                description = context.getString(R.string.channel_status_description)
                setShowBadge(false)
                setSound(null, null)
                enableVibration(false)
            }

            // 提醒通知渠道 - 可以清除
            val reminderChannel = NotificationChannel(
                SafeCheckInApp.REMINDER_CHANNEL_ID,
                context.getString(R.string.channel_reminder_name),
                NotificationManager.IMPORTANCE_DEFAULT
            ).apply {
                description = context.getString(R.string.channel_reminder_description)
                setShowBadge(true)
                enableVibration(true)
            }

            // 预警通知渠道 - 高优先级
            val warningChannel = NotificationChannel(
                SafeCheckInApp.WARNING_CHANNEL_ID,
                context.getString(R.string.channel_warning_name),
                NotificationManager.IMPORTANCE_HIGH
            ).apply {
                description = context.getString(R.string.channel_warning_description)
                setShowBadge(true)
                enableVibration(true)
                setSound(android.media.RingtoneManager.getDefaultUri(android.media.RingtoneManager.TYPE_NOTIFICATION), null)
            }

            notificationManager.createNotificationChannels(
                listOf(statusChannel, reminderChannel, warningChannel)
            )
        }
    }

    /**
     * 显示打卡成功通知
     */
    fun showCheckInSuccessNotification(context: Context) {
        val notificationManager = context.getSystemService(NotificationManager::class.java)

        val intent = Intent(context, MainActivity::class.java).apply {
            flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
        }
        val pendingIntent = PendingIntent.getActivity(
            context, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
        )

        val notification = NotificationCompat.Builder(context, SafeCheckInApp.REMINDER_CHANNEL_ID)
            .setSmallIcon(android.R.drawable.ic_dialog_info)
            .setContentTitle(context.getString(R.string.check_in_success_title))
            .setContentText(context.getString(R.string.check_in_success_content))
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setContentIntent(pendingIntent)
            .setAutoCancel(true)
            .build()

        notificationManager.notify(101, notification)
    }

    /**
     * 显示打卡提醒通知
     */
    fun showReminderNotification(context: Context) {
        val notificationManager = context.getSystemService(NotificationManager::class.java)

        val intent = Intent(context, MainActivity::class.java).apply {
            flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
        }
        val pendingIntent = PendingIntent.getActivity(
            context, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
        )

        // Check-in action
        val checkInIntent = Intent(context, CheckInNotificationService::class.java).apply {
            action = CheckInNotificationService.ACTION_CHECK_IN
        }
        val checkInPendingIntent = PendingIntent.getService(
            context, 1, checkInIntent,
            PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
        )

        val notification = NotificationCompat.Builder(context, SafeCheckInApp.REMINDER_CHANNEL_ID)
            .setSmallIcon(android.R.drawable.ic_dialog_info)
            .setContentTitle(context.getString(R.string.reminder_notification_title))
            .setContentText(context.getString(R.string.reminder_notification_content))
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setContentIntent(pendingIntent)
            .addAction(
                android.R.drawable.ic_menu_add,
                context.getString(R.string.notification_check_in_action),
                checkInPendingIntent
            )
            .setAutoCancel(true)
            .build()

        notificationManager.notify(1, notification)
    }

    /**
     * 显示预警通知
     */
    fun showWarningNotification(context: Context) {
        val notificationManager = context.getSystemService(NotificationManager::class.java)

        val intent = Intent(context, MainActivity::class.java).apply {
            flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
        }
        val pendingIntent = PendingIntent.getActivity(
            context, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
        )

        val notification = NotificationCompat.Builder(context, SafeCheckInApp.WARNING_CHANNEL_ID)
            .setSmallIcon(android.R.drawable.ic_dialog_alert)
            .setContentTitle(context.getString(R.string.warning_notification_title))
            .setContentText(context.getString(R.string.warning_notification_content))
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setContentIntent(pendingIntent)
            .setAutoCancel(true)
            .build()

        notificationManager.notify(2, notification)
    }

    /**
     * 显示测试提醒通知
     */
    fun showTestReminderNotification(context: Context) {
        val notificationManager = context.getSystemService(NotificationManager::class.java)

        val intent = Intent(context, MainActivity::class.java).apply {
            flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
        }
        val pendingIntent = PendingIntent.getActivity(
            context, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
        )

        // Check-in action
        val checkInIntent = Intent(context, CheckInNotificationService::class.java).apply {
            action = CheckInNotificationService.ACTION_CHECK_IN
        }
        val checkInPendingIntent = PendingIntent.getService(
            context, 1, checkInIntent,
            PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
        )

        val notification = NotificationCompat.Builder(context, SafeCheckInApp.REMINDER_CHANNEL_ID)
            .setSmallIcon(android.R.drawable.ic_dialog_info)
            .setContentTitle(context.getString(R.string.test_notification_title))
            .setContentText(context.getString(R.string.test_notification_content))
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setContentIntent(pendingIntent)
            .addAction(
                android.R.drawable.ic_menu_add,
                context.getString(R.string.notification_check_in_action),
                checkInPendingIntent
            )
            .setAutoCancel(true)
            .build()

        notificationManager.notify(999, notification)
    }

    /**
     * 取消所有通知
     */
    fun cancelAllNotifications(context: Context) {
        val notificationManager = context.getSystemService(NotificationManager::class.java)
        notificationManager.cancelAll()
    }
}