package cl.homelogic.platform.designsystem.components.zExperiments

import androidx.compose.foundation.Canvas
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.graphics.StrokeCap
import androidx.compose.ui.graphics.StrokeJoin
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.unit.dp

@Composable
fun TemperatureGraph(
    data: List<Float>,
    modifier: Modifier = Modifier,
    lineColor: Color = Color(0xFF6B7280),
) {
    Canvas(modifier = modifier) {
        if (data.isEmpty()) return@Canvas

        val path = Path()
        val points = Path()

        val xStep = size.width / (data.size - 1)
        val yStep = size.height / (data.maxOrNull() ?: 1f)

        // Create smooth curve
        path.moveTo(0f, size.height - (data.first() * yStep))
        points.moveTo(0f, size.height - (data.first() * yStep))

        data.forEachIndexed { index, value ->
            if (index == 0) return@forEachIndexed
            val x = index * xStep
            val y = size.height - (value * yStep)

            // Create smooth curve
            val controlX1 = ((index - 1) * xStep + x) / 2f
            val controlX2 = controlX1
            val controlY1 = size.height - (data[index - 1] * yStep)
            val controlY2 = y

            path.cubicTo(controlX1, controlY1, controlX2, controlY2, x, y)
            points.cubicTo(controlX1, controlY1, controlX2, controlY2, x, y)
        }

        // Draw the line
        drawPath(
            path = path,
            color = lineColor,
            style = Stroke(
                width = 2.dp.toPx(),
                cap = StrokeCap.Round,
                join = StrokeJoin.Round,
            ),
        )
    }
}
