package com.example.uitugasakhir.ui_manual

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
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.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.animation.animateColorAsState
import android.util.Log
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import com.example.app.ui.theme.Inria_Regular
import com.example.uitugasakhir.R


@Composable
fun PenyiramanSection(
    mode: String,
    pumpStatus: String,
    isLoading: Boolean = false,
    onPumpChange: (String) -> Unit
) {
    val isManual = mode == "Manual"

    val isOn = pumpStatus == "ON"

    val statusColor by animateColorAsState(
        targetValue = if (isOn) Color(0xFF2F6F4E) else Color(0xFFD81F23),
        label = ""
    )

    Column {

        // ================= TITLE =================
        Text(
            text = stringResource(R.string.watering),
            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.watering_is_performed_manually),
            fontSize = 12.sp,
            fontFamily = Inria_Regular,
            fontWeight = FontWeight.Normal,
            color = Color.Gray,
            modifier = Modifier.padding(horizontal = 28.dp)
        )

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

        // ================= STATUS =================
        Text(
            text = if (isOn) "🟢 Pompa Aktif" else "🔴 Pompa Mati",
            color = statusColor,
            fontSize = 14.sp,
            modifier = Modifier.padding(horizontal = 28.dp)
        )

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

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

            Column(
                modifier = Modifier.fillMaxSize(),
                verticalArrangement = Arrangement.Center
            ) {

                // ================= SEGMENTED =================
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(horizontal = 16.dp)
                        .height(50.dp)
                        .clip(RoundedCornerShape(50.dp))
                        .background(Color(0xFFE0E0E0))
                        .padding(4.dp),
                    verticalAlignment = Alignment.CenterVertically
                ) {

                    // ===== ON =====
                    val onColor by animateColorAsState(
                        targetValue = if (isOn) Color(0xFF2F6F4E) else Color.Transparent,
                        label = ""
                    )

                    Box(
                        modifier = Modifier
                            .weight(1f)
                            .fillMaxHeight()
                            .clip(RoundedCornerShape(50.dp))
                            .background(color = onColor)
                            .clickable(enabled = isManual && !isLoading) {

                                Log.d("DEBUG", "PUMP CHANGE DIPANGGIL")

                                onPumpChange("ON")
                            },
                        contentAlignment = Alignment.Center
                    ) {
                        Text(
                            text = if (isLoading && isOn) "Menyalakan..." else "🌱 ON",
                            color = if (isOn) Color.White else Color.Black
                        )
                    }

                    // ===== OFF =====
                    val offColor by animateColorAsState(
                        targetValue = if (!isOn) Color(0xFFD81F23) else Color.Transparent,
                        label = ""
                    )

                    Box(
                        modifier = Modifier
                            .weight(1f)
                            .fillMaxHeight()
                            .clip(RoundedCornerShape(50.dp))
                            .background(color = offColor)
                            .clickable(enabled = isManual && !isLoading) {

                                Log.d("BUTTON", "OFF DIKLIK")

                                onPumpChange("OFF")
                            },
                        contentAlignment = Alignment.Center
                    ) {
                        Text(
                            text = if (isLoading && !isOn) "Mematikan..." else "🛑 OFF",
                            color = if (!isOn) Color.White else Color.Black
                        )
                    }
                }

                // ================= LOADING =================
                if (isLoading) {
                    Spacer(modifier = Modifier.height(10.dp))
                    LinearProgressIndicator(
                        modifier = Modifier
                            .fillMaxWidth()
                            .padding(horizontal = 20.dp)
                    )
                }
            }
        }
    }
}