package com.syaroful.agrilinkvocpro.control_feature.core.components

import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Switch
import androidx.compose.material3.SwitchDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
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.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import com.syaroful.agrilinkvocpro.control_feature.ui.theme.DarkGreen
import com.syaroful.agrilinkvocpro.control_feature.ui.theme.DarkGrey
import com.syaroful.agrilinkvocpro.control_feature.ui.theme.DividerColor
import com.syaroful.agrilinkvocpro.control_feature.ui.theme.MainGreen


@Composable
fun ControlCard(
    iconRes: Int,
    label: String,
    isOn: Boolean,
    isLoading: Boolean,
    onToggle: (Boolean) -> Unit
) {
    val backgroundColor = if (isOn) DarkGreen else MaterialTheme.colorScheme.surfaceContainer
    val iconTint = if (isOn) Color(0xFFB2FF59) else Color(0xFF4CAF50)
    val textColor = if (isSystemInDarkTheme()) Color.White else Color.Black
    val trackColor = if (isSystemInDarkTheme()) DarkGrey else DividerColor

    Column(
        modifier = Modifier
            .padding(vertical = 8.dp)
            .clip(RoundedCornerShape(12.dp))
            .border(1.dp, MainGreen.copy(alpha = 0.4f), RoundedCornerShape(12.dp))
            .background(backgroundColor)
            .padding(16.dp)
            .fillMaxWidth()
    ) {
        Row(
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.SpaceBetween,
            modifier = Modifier.fillMaxWidth()
        ) {
            Box(
                modifier = Modifier
                    .size(32.dp)
                    .background(
                        color = iconTint.copy(alpha = 0.1f),
                        shape = CircleShape
                    ),
                contentAlignment = Alignment.Center
            ) {
                Icon(
                    painter = painterResource(id = iconRes),
                    contentDescription = null,
                    tint = iconTint,
                    modifier = Modifier.size(20.dp)
                )
            }
            if (isLoading) {
                CircularProgressIndicator(color = MainGreen)
            } else {
                Switch(
                    checked = isOn,
                    onCheckedChange = onToggle,
                    colors = SwitchDefaults.colors(
                        checkedThumbColor = DarkGreen,
                        uncheckedThumbColor = Color.White,
                        uncheckedBorderColor = trackColor,
                        checkedTrackColor = MainGreen,
                        uncheckedTrackColor = trackColor
                    )
                )
            }
        }

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

        Text(label, color = if (isOn) Color.White else textColor)

        Text(
            if (isOn) "On" else "Off",
            color = MainGreen,
            fontWeight = FontWeight.Bold
        )
    }
}