package de.jadehs.strassenschadenpro2.pages

import android.content.Context
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Slider
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.dp

@Composable
fun SettingsPage(modifier: Modifier = Modifier) {

    val context = LocalContext.current
    val sharedPreferences = remember { context.getSharedPreferences("settings", Context.MODE_PRIVATE) }

    var radius by remember {
        mutableFloatStateOf(sharedPreferences.getInt("radius", 50).toFloat())
    }

    Column(modifier = modifier.padding(16.dp)) {
        Text("Einstellungen", style = MaterialTheme.typography.headlineMedium)
        Text("Suchradius (in Meter)",modifier = Modifier.padding(top=16.dp))

        OutlinedTextField(
            value = radius.toInt().toString(),
            modifier = Modifier.padding(top=8.dp),
            label = { Text("Radius") },
            keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
            onValueChange = { newValue ->
                var textValue = newValue
                textValue = textValue.replace(" ","")
                textValue = textValue.filter { it.isDigit() }
                val parsed = newValue.toIntOrNull()
                if (parsed != null) {
                    val clamped = parsed.coerceIn(20, 500)
                    radius = clamped.toFloat()
                    sharedPreferences.edit().putInt("radius", clamped).apply()
                }
            },
            singleLine = true
        )
        Slider(
            value = radius,
            onValueChange = { newValue ->
                radius = newValue
                sharedPreferences.edit().putInt("radius", newValue.toInt()).apply()
                            },
            valueRange = 20f..500f,
            modifier = Modifier.padding(top=8.dp)
        )
    }
}

























