package eu.gaudian.translator.view.settings

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
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.platform.LocalContext
import androidx.compose.ui.res.stringArrayResource
import androidx.compose.ui.res.stringResource
import androidx.hilt.lifecycle.viewmodel.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.navigation.NavController
import eu.gaudian.translator.R
import eu.gaudian.translator.utils.findActivity
import eu.gaudian.translator.view.composable.AppScaffold
import eu.gaudian.translator.view.composable.AppTopAppBar
import eu.gaudian.translator.viewmodel.ApiViewModel
import eu.gaudian.translator.viewmodel.SettingsViewModel


/**
 * A settings screen for configuring the custom prompt used for vocabulary generation.
 */
@Composable
fun CustomVocabularyPromptScreen(
    navController: NavController
) {

    val activity = LocalContext.current.findActivity()
    val settingsViewModel: SettingsViewModel = hiltViewModel(viewModelStoreOwner = activity)
    val apiViewModel: ApiViewModel = hiltViewModel(viewModelStoreOwner = activity)
    val customPrompt by settingsViewModel.customPromptVocabulary.collectAsStateWithLifecycle()
    val availableModels by apiViewModel.allValidModels.collectAsStateWithLifecycle()
    val allProviders by apiViewModel.allValidProviders.collectAsStateWithLifecycle()
    val selectedModel by apiViewModel.vocabularyModel.collectAsStateWithLifecycle()
    var tempPrompt by remember(customPrompt) { mutableStateOf(customPrompt) }

    val screenState = PromptSettingsState(
        availableModels = availableModels,
        selectedModel = selectedModel,
        customPrompt = tempPrompt
    )

    AppScaffold(
        topBar = {
            AppTopAppBar(
                title = stringResource(R.string.text_vocabulary_prompt),
                onNavigateBack = { navController.popBackStack() },
                hintContent = null //TODO: Add hint

            )
        }
    ) { paddingValues ->
        Column(
            modifier = Modifier
                .padding(paddingValues)
                .verticalScroll(rememberScrollState())
                .fillMaxWidth()
        ) {
                BasePromptSettingsScreen(
                    state = screenState,
                    providers = allProviders,
                    description = stringResource(R.string.text_here_you_can_set_a_custom_),
                    onPromptChanged = { tempPrompt = it },
                    onSaveClicked = { settingsViewModel.saveCustomVocabularyPrompt(tempPrompt) },
                    onModelSelected = { apiViewModel.setVocabularyModel(it) },
                    hints = stringArrayResource(R.array.vocabulary_example_prompts),
                )
        }
    }
}