package com.optix.android.ui.components

import androidx.compose.animation.animateColorAsState
import androidx.compose.animation.core.RepeatMode
import androidx.compose.animation.core.animateFloat
import androidx.compose.animation.core.infiniteRepeatable
import androidx.compose.animation.core.rememberInfiniteTransition
import androidx.compose.animation.core.tween
import androidx.compose.foundation.background
import androidx.compose.foundation.border
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.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.BatteryAlert
import androidx.compose.material.icons.filled.Memory
import androidx.compose.material.icons.filled.Thermostat
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.optix.android.ui.theme.*
import com.optix.android.util.HardwareState

@Composable
fun HardwareWidget(
    state: HardwareState,
    modifier: Modifier = Modifier,
) {
    Row(
        modifier = modifier.fillMaxWidth(),
        horizontalArrangement = Arrangement.spacedBy(8.dp),
    ) {
        val batteryLabel = if (state.batteryPercent >= 0) "${state.batteryPercent}%" else "N/A"
        val batteryAlert = state.isBatteryLow
        HardwarePill(
            icon       = Icons.Default.BatteryAlert,
            label      = batteryLabel,
            subLabel   = "Bat",
            isWarning  = batteryAlert && !state.isHot,
            isDanger   = batteryAlert && state.isHot,
            modifier   = Modifier.weight(1f),
        )

        val tempLabel = if (state.tempCelsius >= 0f) "${"%.1f".format(state.tempCelsius)}°C" else "N/A"
        HardwarePill(
            icon       = Icons.Default.Thermostat,
            label      = tempLabel,
            subLabel   = "Temp",
            isWarning  = state.tempCelsius in 38f..40f,
            isDanger   = state.isHot,
            modifier   = Modifier.weight(1f),
        )

        val ramLabel = if (state.ramTotalMb > 0) "${state.ramUsedMb}/${state.ramTotalMb} MB" else "N/A"
        HardwarePill(
            icon       = Icons.Default.Memory,
            label      = ramLabel,
            subLabel   = "RAM",
            isWarning  = state.ramRatio in 0.70f..0.85f,
            isDanger   = state.isRamCritical,
            modifier   = Modifier.weight(1f),
        )
    }
}

@Composable
private fun HardwarePill(
    icon:      ImageVector,
    label:     String,
    subLabel:  String,
    isWarning: Boolean,
    isDanger:  Boolean,
    modifier:  Modifier = Modifier,
) {
    val targetAccent: Color = when {
        isDanger   -> T.warning
        isWarning  -> T.warning
        else       -> T.success
    }
    val accentColor by animateColorAsState(
        targetValue = targetAccent,
        animationSpec = tween(durationMillis = 600),
        label = "pill_accent",
    )

    val infiniteTransition = rememberInfiniteTransition(label = "pill_pulse")
    val pulseAlpha by infiniteTransition.animateFloat(
        initialValue = 1f,
        targetValue  = if (isDanger || isWarning) 0.55f else 1f,
        animationSpec = infiniteRepeatable(
            animation  = tween(durationMillis = 900),
            repeatMode = RepeatMode.Reverse,
        ),
        label = "pulse_alpha",
    )

    Box(
        modifier = modifier
            .alpha(if (isDanger || isWarning) pulseAlpha else 1f)
            .background(T.surfaceVar, RoundedCornerShape(8.dp))
            .border(1.dp, accentColor.copy(alpha = 0.45f), RoundedCornerShape(8.dp))
            .padding(horizontal = 10.dp, vertical = 7.dp),
        contentAlignment = Alignment.CenterStart,
    ) {
        Row(verticalAlignment = Alignment.CenterVertically) {
            Icon(
                imageVector        = icon,
                contentDescription = subLabel,
                tint               = accentColor,
                modifier           = Modifier.size(14.dp),
            )
            Spacer(Modifier.width(5.dp))
            Column {
                Text(
                    text       = label,
                    color      = T.onSurface,
                    fontSize   = 11.sp,
                    fontWeight = FontWeight.SemiBold,
                    maxLines   = 1,
                )
                Text(
                    text     = subLabel,
                    color    = T.onSurfaceMid,
                    fontSize = 9.sp,
                    maxLines = 1,
                )
            }
        }
    }
}
