package com.example.uitugasakhir.ui_manual

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.example.app.ui.theme.Inria_Regular
import com.example.uitugasakhir.ViewModel.SensorViewModel
import androidx.compose.ui.platform.LocalContext
import com.example.uitugasakhir.R
import androidx.compose.ui.res.stringResource


// ================= STATUS =================
fun statusKelembaban(value: Float): String {
    return when {
        value < 40 -> "Kering"
        value < 70 -> "Normal"
        else -> "Lembab"
    }
}

fun statusSuhu(value: Float): String {
    return when {
        value < 25 -> "Dingin"
        value < 32 -> "Normal"
        else -> "Panas"
    }
}

// ================= UI =================
@Composable
fun SensorSection(
) {

    val context = LocalContext.current

    val sensorVm: SensorViewModel = viewModel()
    val data = sensorVm.data

    // 🔥 load sekali saat pertama buka
    LaunchedEffect(Unit) {
        sensorVm.getData(context)
    }

    val ku = data?.kelembaban_udara
    val su = data?.suhu_udara
    val kt = data?.kelembaban_tanah
    val ih = data?.intensitas_hujan

    val progressKU = ku?.div(100f)
    val progressSU = su?.div(50f)
    val progressKT = kt?.div(100f)
    val progressIH = ih?.div(100f)

    Column {

        Text(
            text = stringResource(R.string.sensor_monitoring),
            fontSize = 15.sp,
            fontFamily = Inria_Regular,
            fontWeight = FontWeight.Medium,
            color = Color.Black,
            modifier = Modifier.padding(horizontal = 28.dp)
        )

        Spacer(modifier = Modifier.height(5.dp))

        Text(
            text = stringResource(R.string.monitor_environmental_conditions_in_real_time),
            fontSize = 12.sp,
            fontFamily = Inria_Regular,
            fontWeight = FontWeight.Normal,
            color = Color.Gray,
            modifier = Modifier.padding(horizontal = 28.dp)
        )

        Spacer(modifier = Modifier.height(15.dp))

        Card(
            modifier = Modifier
                .padding(horizontal = 25.dp)
                .fillMaxWidth(),
            shape = RoundedCornerShape(24.dp),
            colors = CardDefaults.cardColors(
                containerColor = Color(0xFFEDEDED)
            ),
            elevation = CardDefaults.cardElevation(4.dp)
        ) {

            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(12.dp),
                horizontalArrangement = Arrangement.SpaceEvenly
            ) {

                com.example.uitugasakhir.ui_otomatis.SensorCard(
                    title = "Kel. Udara",
                    value = if (ku != null) "${ku.toInt()}%" else "--",
                    status = if (ku != null) com.example.uitugasakhir.ui_otomatis.statusKelembaban(
                        ku
                    ) else "--",
                    progress = progressKU ?: 0f,
                    color = Color(0xFF4FC3F7)
                )

                com.example.uitugasakhir.ui_otomatis.SensorCard(
                    title = "Suhu Udara",
                    value = if (su != null) "${su.toInt()}°C" else "--",
                    status = if (su != null) com.example.uitugasakhir.ui_otomatis.statusSuhu(su) else "--",
                    progress = progressSU ?: 0f,
                    color = Color(0xFF2E8B57)
                )

                com.example.uitugasakhir.ui_otomatis.SensorCard(
                    title = "Kel. Tanah",
                    value = if (kt != null) "${kt.toInt()}%" else "--",
                    status = if (kt != null) com.example.uitugasakhir.ui_otomatis.statusKelembaban(
                        kt
                    ) else "--",
                    progress = progressKT ?: 0f,
                    color = Color(0xFF8B5A2B)
                )

                com.example.uitugasakhir.ui_otomatis.SensorCard(
                    title = "Int. Hujan",
                    value = if (ih != null) "${ih.toInt()}%" else "--",
                    status = if (ih != null) {
                        when {
                            ih < 40 -> "Ringan"
                            ih < 70 -> "Sedang"
                            else -> "Deras"
                        }
                    } else "--",
                    progress = progressIH ?: 0f,
                    color = Color(0xFF1E3A5F)
                )
            }
        }
    }
}


@Composable
fun SensorCard(
    title: String,
    value: String,
    status: String,
    progress: Float,
    color: Color
) {
    Card(
        modifier = Modifier
            .width(75.dp)
            .height(120.dp),
        shape = RoundedCornerShape(16.dp),
        colors = CardDefaults.cardColors(
            containerColor = Color.White
        ),
        elevation = CardDefaults.cardElevation(3.dp)
    ) {

        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(8.dp),
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.SpaceEvenly
        ) {

            Box(
                contentAlignment = Alignment.Center,
                modifier = Modifier.size(50.dp)
            ) {

                CircularProgressIndicator(
                    progress = { 1f },
                    color = Color.LightGray,
                    strokeWidth = 3.dp,
                    modifier = Modifier.fillMaxSize()
                )

                CircularProgressIndicator(
                    progress = { progress },
                    color = color,
                    strokeWidth = 3.dp,
                    modifier = Modifier.fillMaxSize()
                )

                Text(
                    text = value,
                    fontSize = 10.sp,
                    fontWeight = FontWeight.Bold
                )
            }

            Text(
                text = title,
                fontSize = 9.sp,
                fontWeight = FontWeight.Bold
            )

            Text(
                text = status,
                fontSize = 8.sp,
                color = Color.Gray
            )
        }
    }
}