package com.isidroid.slider

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.viewinterop.AndroidView
import com.isidroid.slider.ext.ExtendedSliderView

@Composable
fun ComposeSlider(
    updateAnswer: (value: String?, isManual: Boolean) -> Unit,
    modifier: Modifier = Modifier,
    colors: SliderColor = SliderColor.getDefaults(),
    options: List<SliderOption> = emptyList(),
    currentValue: String? = null,
    isEnabled: Boolean = true,
    isAnswered: Boolean = false,
    allowSubmitDefaultAnswer: Boolean = false,
) {

    AndroidView(
        modifier = modifier,
        factory = { context ->
            ExtendedSliderView(context)
                .update(
                    colors = colors,
                    currentValue = currentValue,
                    updateAnswer = updateAnswer,
                    isAnswered = isAnswered,
                    isEnabled = isEnabled,
                    allowSubmitDefaultAnswer = allowSubmitDefaultAnswer,
                    options = options
                )
        }
    )
}


@Preview
@Composable
private fun ComposeSliderPreview(){
    val valuesList = (1..10).map { "$it" }
    val options = valuesList.map {
        val message = it
        SliderOption(
            text = message,
            tag = "",
            optionMessage = message,
            isActive = true // TODO
        )
    }

    var currentValue by remember { mutableStateOf<String?>("3") }
    var isManual by remember { mutableStateOf<Boolean?>(null) }
    val colors = SliderColor.getDefaults().copy(trackActive = MaterialTheme.colorScheme.primary, labelActive = MaterialTheme.colorScheme.primary)

    Column {
        Text(text = "value=$currentValue, isManual=$isManual")

        ComposeSlider(
            colors = colors,
            options = options,
            updateAnswer = { value, manual ->
                currentValue = value
                isManual = manual
            },
            modifier = Modifier.fillMaxWidth(),
            currentValue = currentValue,
            allowSubmitDefaultAnswer = true
        )


        ComposeSlider(
            colors = colors,
            options = options,
            updateAnswer = { value, manual ->
                currentValue = value
                isManual = manual
            },
            modifier = Modifier.fillMaxWidth(),
            currentValue = currentValue,
            allowSubmitDefaultAnswer = true,
            isEnabled = false,
            isAnswered = true
        )

    }
}