package cloud.molberg.picar

import android.app.*
import android.content.Intent
import android.os.IBinder

class CompanionService : Service() {
    private val repository get() = (application as CompanionApplication).repository

    override fun onCreate() {
        super.onCreate()
        val channel = NotificationChannel("companion_connection", "Car companion connection", NotificationManager.IMPORTANCE_LOW)
        getSystemService(NotificationManager::class.java).createNotificationChannel(channel)
        val launch = PendingIntent.getActivity(this, 0, Intent(this, MainActivity::class.java), PendingIntent.FLAG_IMMUTABLE)
        val notification = Notification.Builder(this, channel.id)
            .setSmallIcon(android.R.drawable.stat_sys_data_bluetooth)
            .setContentTitle("Pi Car Companion")
            .setContentText("Bluetooth and phone GPS are active")
            .setContentIntent(launch)
            .setOngoing(true)
            .build()
        startForeground(42, notification)
        repository.connect()
        repository.startLocation()
    }

    override fun onDestroy() { repository.stopLocation(); repository.stop(); super.onDestroy() }
    override fun onBind(intent: Intent?): IBinder? = null
}
