package eu.gaudian.translator.view.dictionary

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.hilt.lifecycle.viewmodel.compose.hiltViewModel
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import eu.gaudian.translator.R
import eu.gaudian.translator.utils.findActivity
import eu.gaudian.translator.view.composable.AppButton
import eu.gaudian.translator.view.composable.AppOutlinedCard
import eu.gaudian.translator.view.composable.AppOutlinedTextField
import eu.gaudian.translator.view.composable.DictionaryLanguageDropDown
import eu.gaudian.translator.viewmodel.DictionaryViewModel
import eu.gaudian.translator.viewmodel.LanguageViewModel

@Composable
fun EtymologyScreen(
    navController: NavController,
    dictionaryViewModel: DictionaryViewModel,
    languageViewModel: LanguageViewModel,
) {
    var searchQuery by remember { mutableStateOf("") }
    val uiState by dictionaryViewModel.uiState.collectAsState()

    Column(modifier = Modifier.padding(16.dp)) {
        AppOutlinedCard {
            Column(
                modifier = Modifier.padding(16.dp),
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                AppOutlinedTextField(
                    value = searchQuery,
                    onValueChange = { searchQuery = it },
                    label = { Text(stringResource(R.string.search_for_a_word_s_origin)) },
                    modifier = Modifier.fillMaxWidth()
                )
                Spacer(modifier = Modifier.height(8.dp))
                Row(
                    modifier = Modifier.fillMaxWidth(),
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    DictionaryLanguageDropDown(
                        languageViewModel = languageViewModel,
                        modifier = Modifier.weight(1f)
                    )
                    AppButton(
                        onClick = {
                            val language = languageViewModel.selectedDictionaryLanguage.value
                            if (searchQuery.isNotBlank() && language != null) {
                                @Suppress("HardCodedStringLiteral")
                                navController.navigate("etymology_result/${searchQuery}/${language.code}")
                            }
                        },
                        enabled = searchQuery.isNotBlank() && !uiState.isLoading,
                        modifier = Modifier.padding(start = 8.dp)
                    ) {
                        if (uiState.isLoading) {
                            CircularProgressIndicator(
                                modifier = Modifier.size(24.dp),
                                color = MaterialTheme.colorScheme.onPrimary,
                                strokeWidth = 2.dp
                            )
                        } else {
                            Text(stringResource(R.string.cd_search))
                        }
                    }
                }
            }
        }


    }
}

@Preview
@Composable
fun EtymologyScreenPreview() {
    val activity = LocalContext.current.findActivity()
    val dictionaryViewModel: DictionaryViewModel = hiltViewModel(viewModelStoreOwner = activity)
    val languageViewModel: LanguageViewModel = hiltViewModel(viewModelStoreOwner = activity)
    EtymologyScreen(
        navController = rememberNavController(),
        dictionaryViewModel = dictionaryViewModel,
        languageViewModel = languageViewModel
    )
}