package com.example.uitugasakhir

import android.app.Service
import android.content.Intent
import android.os.IBinder
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class SensorMonitoringService : Service() {

    private val scope = CoroutineScope(Dispatchers.IO)
    private var job: Job? = null

    override fun onStartCommand(
        intent: Intent?,
        flags: Int,
        startId: Int
    ): Int {

        SensorNotification.createChannel(this)

        job = scope.launch {

            while (true) {

                // Ambil data sensor dari Firebase/API
                // lalu cek ambang batas

                val suhu = 35

                if (suhu > 30) {

                    SensorNotification.showNotification(
                        this@SensorMonitoringService,
                        "Peringatan Suhu",
                        "Suhu terlalu tinggi"
                    )
                }

                delay(15000)
            }
        }

        return START_STICKY
    }

    override fun onDestroy() {
        super.onDestroy()
        job?.cancel()
    }

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