package de.trainingstagebuch.app.ui.screens

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Button
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import de.trainingstagebuch.app.R
import de.trainingstagebuch.app.ui.ErrorBanner
import de.trainingstagebuch.app.ui.ErrorSnackbarHost
import de.trainingstagebuch.app.ui.TrainingstagebuchTopBar
import de.trainingstagebuch.app.viewmodel.PersonalSettingsViewModel

@Composable
fun PersonalSettingsScreen(
    viewModel: PersonalSettingsViewModel = viewModel()
) {
    val state by viewModel.state.collectAsState()
    val context = LocalContext.current
    val snackbarHostState = remember { SnackbarHostState() }

    LaunchedEffect(state.errorMessage, state.successMessage) {
        val msg = state.errorMessage ?: state.successMessage ?: return@LaunchedEffect
        snackbarHostState.showSnackbar(msg.resolve(context))
    }

    Scaffold(
        topBar = { TrainingstagebuchTopBar(title = stringResource(R.string.personal_settings_title)) },
        snackbarHost = { ErrorSnackbarHost(hostState = snackbarHostState) }
    ) { paddingValues ->
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(paddingValues)
                .padding(16.dp),
            verticalArrangement = Arrangement.spacedBy(10.dp)
        ) {
            Text(
                text = stringResource(R.string.personal_settings_title),
                style = MaterialTheme.typography.headlineSmall
            )
            Text(
                text = stringResource(R.string.personal_settings_language_description),
                style = MaterialTheme.typography.bodyMedium
            )

            state.errorMessage?.let {
                ErrorBanner(
                    message = it.resolve(context),
                    onDismiss = viewModel::clearMessages
                )
            }

            if (state.loading) {
                CircularProgressIndicator()
            } else {
                LazyColumn(
                    modifier = Modifier.fillMaxWidth(),
                    verticalArrangement = Arrangement.spacedBy(8.dp)
                ) {
                    items(state.languages, key = { it.code }) { language ->
                        val isSelected = language.code == state.selectedLanguage
                        Button(
                            onClick = { viewModel.onLanguageSelected(language.code) },
                            modifier = Modifier.fillMaxWidth()
                        ) {
                            val label = context.getString(language.labelResId)
                            if (isSelected) {
                                Text(text = stringResource(R.string.personal_settings_selected_language, label))
                            } else {
                                Text(text = label)
                            }
                        }
                    }
                }
            }

            Button(onClick = viewModel::load, modifier = Modifier.fillMaxWidth()) {
                Text(stringResource(R.string.common_retry))
            }
        }
    }
}
