package app.voltplan.cable.ui.overview

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.Add
import androidx.compose.material.icons.outlined.Remove
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import app.voltplan.cable.R
import kotlin.math.roundToInt

private const val MIN_HOURS = 0.5
private const val MAX_HOURS = 240.0

/** Day/hour/minute goal editor with stepper controls. Mirrors the iOS `GoalEditorSheet`. */
@Composable
fun GoalEditorDialog(
    title: String,
    initialHours: Double,
    hasGoal: Boolean,
    onSave: (Double) -> Unit,
    onRemove: () -> Unit,
    onDismiss: () -> Unit,
) {
    val clamped = initialHours.coerceIn(MIN_HOURS, MAX_HOURS)
    val totalMinutes = (clamped * 60).roundToInt()
    var days by remember { mutableIntStateOf(totalMinutes / (24 * 60)) }
    var hours by remember { mutableIntStateOf((totalMinutes % (24 * 60)) / 60) }
    var minutes by remember { mutableIntStateOf(((totalMinutes % 60) / 15) * 15) }

    fun currentHours(): Double = (days * 24 + hours + minutes / 60.0)

    AlertDialog(
        onDismissRequest = onDismiss,
        title = { Text(title) },
        text = {
            Column(verticalArrangement = Arrangement.spacedBy(8.dp)) {
                Stepper(stringResource(R.string.goal_days), days, 0, 10) { days = it }
                Stepper(stringResource(R.string.goal_hours), hours, 0, 23) { hours = it }
                Stepper(stringResource(R.string.goal_minutes), minutes, 0, 45, step = 15) { minutes = it }
            }
        },
        confirmButton = {
            TextButton(onClick = {
                val rounded = (currentHours() / 0.25).roundToInt() * 0.25
                onSave(rounded.coerceIn(MIN_HOURS, MAX_HOURS)); onDismiss()
            }) { Text(stringResource(R.string.overview_goal_save)) }
        },
        dismissButton = {
            Row {
                if (hasGoal) {
                    TextButton(onClick = { onRemove(); onDismiss() }) { Text(stringResource(R.string.overview_goal_clear)) }
                }
                TextButton(onClick = onDismiss) { Text(stringResource(R.string.overview_goal_cancel)) }
            }
        },
    )
}

@Composable
private fun Stepper(label: String, value: Int, min: Int, max: Int, step: Int = 1, onChange: (Int) -> Unit) {
    Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) {
        Text(label, modifier = Modifier.weight(1f))
        IconButton(onClick = { if (value - step >= min) onChange(value - step) }) {
            Icon(Icons.Outlined.Remove, contentDescription = null)
        }
        Text("$value", style = MaterialTheme.typography.titleMedium, fontWeight = FontWeight.SemiBold)
        IconButton(onClick = { if (value + step <= max) onChange(value + step) }) {
            Icon(Icons.Outlined.Add, contentDescription = null)
        }
    }
}
