package com.syaroful.agrilinkvocpro.presentation.screen.detail.component

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import com.syaroful.agrilinkvocpro.R
import com.syaroful.agrilinkvocpro.data.model.Dht
import com.syaroful.agrilinkvocpro.data.model.Npk
import com.syaroful.agrilinkvocpro.data.model.SensorDataResponse

@Composable
fun DetailSensorData(
    currentData: SensorDataResponse?,
    sensorId: String
) {
    val dataSensor = when (sensorId) {
        "npk1" -> currentData?.data?.npk1
        "npk2" -> currentData?.data?.npk2
        "dht" -> currentData?.data?.dht
        else -> null
    }

    if (dataSensor == null) {
        Text("Data sensor kosong")
        return
    }

    when (dataSensor) {
        is Npk -> {
            Column(verticalArrangement = Arrangement.spacedBy(8.dp)) {
                SensorBar(
                    label = "Nitrogen",
                    value = dataSensor.soilNitrogen,
                    max = 50f,
                    icon = R.drawable.npk
                )
                SensorBar(
                    label = "Pospor",
                    value = dataSensor.soilPhosphorus,
                    max = 255f,
                    icon = R.drawable.npk
                )
                SensorBar(
                    label = "Kalium",
                    value = dataSensor.soilPotassium,
                    max = 255f,
                    icon = R.drawable.npk
                )
                SensorBar(
                    label = "Kelembaban",
                    value = dataSensor.soilHumidity,
                    max = 100f,
                    icon = R.drawable.soil_humidity
                )
                SensorBar(
                    label = "Suhu Tanah",
                    value = dataSensor.soilTemperature,
                    max = 50f,
                    icon = R.drawable.soil_temperature
                )
                SensorBar(
                    label = "PH Tanah",
                    value = dataSensor.soilPh,
                    max = 14f,
                    icon = R.drawable.meters
                )
                SensorBar(
                    label = "Konduktivitas",
                    value = dataSensor.soilConductivity,
                    max = 200f,
                    icon = R.drawable.electricity
                )
            }
        }

        is Dht -> {
            Column(verticalArrangement = Arrangement.spacedBy(8.dp)) {
                SensorBar(
                    label = "Kelembaban Udara",
                    value = dataSensor.viciHumidity,
                    max = 50f,
                    icon = R.drawable.humidity
                )
                SensorBar(
                    label = "Intensitas Cahaya",
                    value = dataSensor.viciLuminosity,
                    max = 255f,
                    icon = R.drawable.luminosity
                )
                SensorBar(
                    label = "Suhu Udara",
                    value = dataSensor.viciTemperature,
                    max = 255f,
                    icon = R.drawable.weather
                )
            }
        }

        else -> {}
    }
}

@Composable
fun SensorBar(
    label: String,
    value: Number?,
    max: Float,
    icon: Int
) {
    DataSensorBar(
        label = label,
        value = value ?: 0,
        percentage = (value?.toFloat() ?: 0f) / max,
        painter = painterResource(id = icon)
    )
}
