package com.omixlab.mosis

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.Service
import android.content.Intent
import android.content.res.AssetManager
import android.os.IBinder
import androidx.core.app.NotificationCompat

class NativeService : Service() {
    companion object {
        const val NOTIFICATION_ID = 1
        const val CHANNEL_ID = "MosisServiceChannel"
        init {
            System.loadLibrary("mosis-service")
        }
    }
    external fun getBinderNative(): IBinder
    external fun setAssetManager(assetManager: AssetManager)

    override fun onBind(intent: Intent): IBinder {
        return getBinderNative()
    }

    override fun onCreate() {
        super.onCreate()
        setAssetManager(assets)
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        createNotificationChannel()
        // Create the notification required for the foreground service
        val notification: Notification = NotificationCompat.Builder(this, CHANNEL_ID)
            .setContentTitle("Mosis Service")
            .setContentText("Service is running in the background")
            .setSmallIcon(android.R.drawable.ic_dialog_info) // Replace with your app's icon
            .setPriority(NotificationCompat.PRIORITY_LOW)
            .build()
        // Promotes the service to a foreground service
        startForeground(NOTIFICATION_ID, notification)
        // START_STICKY ensures the service restarts if the system kills it
        return START_STICKY
    }

    private fun createNotificationChannel() {
        val serviceChannel = NotificationChannel(
            CHANNEL_ID,
            "Mosis Service Channel",
            NotificationManager.IMPORTANCE_DEFAULT
        )
        val manager = getSystemService(NotificationManager::class.java)
        manager.createNotificationChannel(serviceChannel)
    }
}