package com.joe.camera.webcam.ui

import androidx.compose.foundation.Canvas
import androidx.compose.foundation.gestures.detectDragGestures
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.CornerRadius
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.layout.onSizeChanged
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.dp
import kotlin.math.max

@Composable
fun IndustrialSlider(
    value: Float,
    onValueChange: (Float) -> Unit,
    modifier: Modifier = Modifier,
    valueRange: ClosedFloatingPointRange<Float> = 0f..1f,
    activeColor: Color = Color(0xFF03A9F4),
) {
    var widthPx by remember { mutableFloatStateOf(1f) }
    val density = LocalDensity.current
    val rangeLength = max(valueRange.endInclusive - valueRange.start, 0.0001f)
    val progress = ((value - valueRange.start) / rangeLength).coerceIn(0f, 1f)
    val updateFromX: (Float) -> Unit = { x ->
        val fraction = (x / widthPx).coerceIn(0f, 1f)
        onValueChange(valueRange.start + fraction * rangeLength)
    }

    Canvas(
        modifier = modifier
            .fillMaxWidth()
            .height(30.dp)
            .onSizeChanged { widthPx = it.width.toFloat().coerceAtLeast(1f) }
            .pointerInput(widthPx, valueRange) {
                detectTapGestures { updateFromX(it.x) }
            }
            .pointerInput(widthPx, valueRange) {
                detectDragGestures(
                    onDragStart = { updateFromX(it.x) },
                    onDrag = { change, _ ->
                        change.consume()
                        updateFromX(change.position.x)
                    },
                )
            },
    ) {
        val trackHeight = with(density) { 3.dp.toPx() }
        val outerRadius = with(density) { 8.dp.toPx() }
        val innerRadius = with(density) { 3.dp.toPx() }
        val centerY = size.height / 2f
        val thumbX = size.width * progress
        val trackTop = centerY - trackHeight / 2f

        drawRoundRect(
            color = Color.White.copy(alpha = 0.22f),
            topLeft = Offset(0f, trackTop),
            size = Size(size.width, trackHeight),
            cornerRadius = CornerRadius(trackHeight),
        )
        drawRoundRect(
            color = activeColor,
            topLeft = Offset(0f, trackTop),
            size = Size(thumbX, trackHeight),
            cornerRadius = CornerRadius(trackHeight),
        )
        drawCircle(color = activeColor, radius = outerRadius, center = Offset(thumbX, centerY))
        drawCircle(color = Color(0xFF17171A), radius = innerRadius, center = Offset(thumbX, centerY))
    }
}
