package com.syaroful.agrilinkvocpro.control_feature.page

import androidx.compose.foundation.background
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.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material.icons.filled.Info
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Switch
import androidx.compose.material3.SwitchDefaults
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
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.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.syaroful.agrilinkvocpro.control_feature.R

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun ControlActuatorScreen() {
    Scaffold(
        topBar = {
            TopAppBar(
                title = { Text("Control Actuator") },
                navigationIcon = {
                    IconButton(onClick = { /* TODO: handle back */ }) {
                        Icon(Icons.AutoMirrored.Filled.ArrowBack, contentDescription = "Back")
                    }
                },
                actions = {
                    IconButton(onClick = { /* TODO: handle history */ }) {
                        Icon(Icons.Filled.Info, contentDescription = "History")
                    }
                }
            )
        }
    ) { innerPadding ->
        Column(
            modifier = Modifier
                .padding(innerPadding)
                .padding(16.dp)
                .verticalScroll(rememberScrollState())
        ) {
            Text("Penyiraman Air", style = MaterialTheme.typography.titleMedium)

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

            ControlGrid(
                iconRes = R.drawable.ic_valve,
                items = List(4) { index -> "Bet ${index + 1}" }
            )

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

            Text("Penyiraman Pupuk", style = MaterialTheme.typography.titleMedium)

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

            ControlGrid(
                iconRes = R.drawable.ic_leaf,
                items = List(4) { index -> "Bet ${index + 1}" }
            )
        }
    }
}

@Composable
fun ControlGrid(iconRes: Int, items: List<String>) {
    val states = remember { items.map { mutableStateOf(false) } }

    LazyVerticalGrid(
        columns = GridCells.Fixed(2),
        modifier = Modifier
            .fillMaxWidth()
            .height(300.dp),
        verticalArrangement = Arrangement.spacedBy(12.dp),
        horizontalArrangement = Arrangement.spacedBy(12.dp)
    ) {
        items(items.size) { index ->
            ControlCard(
                iconRes = iconRes,
                label = items[index],
                isOn = states[index].value,
                onToggle = { states[index].value = it }
            )
        }
    }
}

@Composable
fun ControlCard(
    iconRes: Int,
    label: String,
    isOn: Boolean,
    onToggle: (Boolean) -> Unit
) {
    val backgroundColor = if (isOn) Color(0xFF00332C) else Color.White
    val iconTint = if (isOn) Color(0xFFB2FF59) else Color(0xFF4CAF50)
    val textColor = if (isOn) Color(0xFF00E676) else Color(0xFF4CAF50)

    Column(
        modifier = Modifier
            .clip(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 = if (isOn) Color(0xFF00332C) else Color(0xFFE0F2F1), shape = CircleShape),
                contentAlignment = Alignment.Center
            ) {
                Icon(
                    painter = painterResource(id = iconRes),
                    contentDescription = null,
                    tint = iconTint,
                    modifier = Modifier.size(20.dp)
                )
            }
            Switch(
                checked = isOn,
                onCheckedChange = onToggle,
                colors = SwitchDefaults.colors(
                    checkedThumbColor = Color(0xFF00E676),
                    uncheckedThumbColor = Color.Gray
                )
            )
        }

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

        Text(label, color = Color.Black)

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

@Preview
@Composable
fun ControlActuatorScreenPreview() {
    ControlActuatorScreen()
}

