package com.example.livingai.pages.settings

import android.app.Activity
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Slider
import androidx.compose.material3.Switch
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringArrayResource
import androidx.compose.ui.res.stringResource
import androidx.navigation.NavController
import com.example.livingai.R
import com.example.livingai.pages.commons.Dimentions
import com.example.livingai.pages.components.CommonScaffold
import com.example.livingai.pages.components.LabeledDropdown
import org.koin.androidx.compose.koinViewModel
import kotlin.math.roundToInt

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun SettingsScreen(
    navController: NavController,
    viewModel: SettingsViewModel = koinViewModel()
) {
    val settings by viewModel.settings.collectAsState()
    val context = LocalContext.current

    val languageEntries = stringArrayResource(id = R.array.language_entries)
    val languageValues = stringArrayResource(id = R.array.language_values)
    val languageMap = languageEntries.zip(languageValues).toMap()

    // Find the display name for the currently selected language value
    val selectedLanguageEntry = languageMap.entries.find { it.value == settings.language }?.key ?: languageEntries.first()

    val distanceMethods = listOf("Jaccard", "Euclidean", "Hamming")

    CommonScaffold(
        navController = navController,
        title = stringResource(id = R.string.top_bar_settings)
    ) {
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(it)
                .padding(Dimentions.SMALL_PADDING_TEXT)
        ) {
            LabeledDropdown(
                labelRes = R.string.language,
                options = languageEntries.toList(),
                selected = selectedLanguageEntry,
                onSelected = { selectedEntry ->
                    val selectedValue = languageMap[selectedEntry] ?: languageValues.first()
                    viewModel.saveSettings(settings.copy(language = selectedValue))
                    
                    // Restart activity to apply language change
                    val activity = context as? Activity
                    activity?.recreate()
                }
            )

            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(vertical = Dimentions.SMALL_PADDING_TEXT),
                horizontalArrangement = Arrangement.SpaceBetween,
                verticalAlignment = Alignment.CenterVertically
            ) {
                Text(text = stringResource(id = R.string.auto_capture))
                Switch(
                    checked = settings.isAutoCaptureOn,
                    onCheckedChange = { isChecked ->
                        viewModel.saveSettings(settings.copy(isAutoCaptureOn = isChecked))
                    }
                )
            }

            Column(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(vertical = Dimentions.SMALL_PADDING_TEXT)
            ) {
                LabeledDropdown(
                    labelRes = R.string.distance_method,
                    options = distanceMethods,
                    selected = settings.distanceMethod,
                    onSelected = { selectedMethod ->
                        viewModel.saveSettings(settings.copy(distanceMethod = selectedMethod))
                    },
                    modifier = Modifier.padding(vertical = Dimentions.SMALL_PADDING_TEXT)
                )

                Row(
                    modifier = Modifier.fillMaxWidth(),
                    horizontalArrangement = Arrangement.SpaceBetween
                ) {
                    Text(text = "Match Threshold")
                    Text(text = "${settings.jaccardThreshold.roundToInt()}%")
                }
                Slider(
                    value = settings.jaccardThreshold,
                    onValueChange = { newValue ->
                        viewModel.saveSettings(settings.copy(jaccardThreshold = newValue))
                    },
                    valueRange = 1f..100f,
                    steps = 99
                )
            }
        }
    }
}
