package com.idea.gametime.session

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.app.Service
import android.content.Intent
import android.os.Build
import android.os.IBinder
import com.idea.gametime.MainActivity
import com.idea.gametime.R

class SessionStatusForegroundService : Service() {
    override fun onCreate() {
        super.onCreate()
        ensureNotificationChannel()
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        if (intent?.action != ACTION_SHOW) {
            stopSelf()
            return START_NOT_STICKY
        }
        val title = intent.getStringExtra(EXTRA_TITLE) ?: "GameTime"
        val primaryLine = intent.getStringExtra(EXTRA_PRIMARY_LINE) ?: "Séance en cours"
        val secondaryLine = intent.getStringExtra(EXTRA_SECONDARY_LINE)
        startForeground(
            NOTIFICATION_ID,
            notification(
                title = title,
                primaryLine = primaryLine,
                secondaryLine = secondaryLine,
            ),
        )
        return START_STICKY
    }

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

    private fun notification(
        title: String,
        primaryLine: String,
        secondaryLine: String?,
    ): Notification {
        val builder = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            Notification.Builder(this, CHANNEL_ID)
        } else {
            @Suppress("DEPRECATION")
            Notification.Builder(this)
        }
        val launchIntent = Intent(this, MainActivity::class.java).apply {
            flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP
        }
        val contentIntent = PendingIntent.getActivity(
            this,
            0,
            launchIntent,
            PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT,
        )
        val style = secondaryLine?.takeIf { it.isNotBlank() }?.let {
            Notification.BigTextStyle().bigText("$primaryLine\n$it")
        }
        return builder
            .setSmallIcon(R.drawable.ic_ongoing_gt)
            .setContentTitle(title)
            .setContentText(primaryLine)
            .setSubText(secondaryLine)
            .setStyle(style)
            .setContentIntent(contentIntent)
            .setOngoing(true)
            .setOnlyAlertOnce(true)
            .setShowWhen(false)
            .setCategory(Notification.CATEGORY_STATUS)
            .build()
    }

    private fun ensureNotificationChannel() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
            return
        }
        val manager = getSystemService(NotificationManager::class.java)
        val channel = NotificationChannel(
            CHANNEL_ID,
            "Séance en cours",
            NotificationManager.IMPORTANCE_LOW,
        )
        manager.createNotificationChannel(channel)
    }

    companion object {
        const val ACTION_SHOW = "com.idea.gametime.session.SHOW"
        const val EXTRA_TITLE = "title"
        const val EXTRA_PRIMARY_LINE = "primaryLine"
        const val EXTRA_SECONDARY_LINE = "secondaryLine"

        private const val CHANNEL_ID = "gametime_session_status"
        private const val NOTIFICATION_ID = 92
    }
}
