package com.example.shiftalarm

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

object NotificationHelper {
    const val CHANNEL_UPCOMING = "UPCOMING_ALARM_CHANNEL_V1"
    const val NOTIFICATION_ID_UPCOMING = 3001

    fun createNotificationChannels(context: Context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val name = "알람 사전 알림"
            val descriptionText = "알람 발동 전 미리 알림 및 조기 해제"
            val importance = NotificationManager.IMPORTANCE_HIGH
            val channel = NotificationChannel(CHANNEL_UPCOMING, name, importance).apply {
                description = descriptionText
                enableVibration(true)
                vibrationPattern = longArrayOf(0, 250, 250, 250)
            }
            val notificationManager: NotificationManager =
                context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.createNotificationChannel(channel)
        }
    }

    fun showUpcomingAlarmNotification(
        context: Context,
        alarmId: Int,
        timeStr: String,
        shiftType: String,
        label: String?,
        minutesBefore: Int
    ) {
        createNotificationChannels(context)

        val notificationManager =
            context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

        // Intent to cancel today's alarm early
        val cancelIntent = Intent(context, CancelAlarmReceiver::class.java).apply {
            putExtra("EXTRA_ALARM_ID", alarmId)
            putExtra("EXTRA_TIME", timeStr)
        }
        val cancelPendingIntent = PendingIntent.getBroadcast(
            context,
            alarmId * 10 + 1,
            cancelIntent,
            PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
        )

        // Intent to open settings or activity
        val openIntent = Intent(context, SettingsActivity::class.java).apply {
            putExtra("TARGET_TAB", 1)
            flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
        }
        val openPendingIntent = PendingIntent.getActivity(
            context,
            alarmId * 10 + 2,
            openIntent,
            PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
        )

        val titleText = if (!label.isNull_or_Empty()) "🔔 $label ($shiftType)" else "🔔 $shiftType 근무 알람"
        val contentText = "$timeStr 알람이 ${minutesBefore}분 후 울립니다."

        val notification = NotificationCompat.Builder(context, CHANNEL_UPCOMING)
            .setSmallIcon(R.drawable.ic_alarm)
            .setContentTitle(titleText)
            .setContentText(contentText)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setCategory(NotificationCompat.CATEGORY_ALARM)
            .setAutoCancel(true)
            .setContentIntent(openPendingIntent)
            .addAction(R.drawable.ic_check, "해제", cancelPendingIntent)
            .build()

        notificationManager.notify(NOTIFICATION_ID_UPCOMING + alarmId, notification)
    }

    fun dismissUpcomingNotification(context: Context, alarmId: Int) {
        val notificationManager =
            context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.cancel(NOTIFICATION_ID_UPCOMING + alarmId)
    }
}

private fun String?.isNull_or_Empty(): Boolean {
    return this == null || this.trim().isEmpty()
}
