package eu.gaudian.translator.view.settings

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
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.padding
import androidx.compose.foundation.layout.width
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import eu.gaudian.translator.R
import eu.gaudian.translator.model.LanguageModel
import eu.gaudian.translator.model.communication.ApiProvider
import eu.gaudian.translator.ui.theme.ThemePreviews
import eu.gaudian.translator.view.composable.ApiModelDropDown
import eu.gaudian.translator.view.composable.AppButton
import eu.gaudian.translator.view.composable.AppCard
import eu.gaudian.translator.view.composable.InspiringSearchField

data class PromptSettingsState(
    val availableModels: List<LanguageModel> = emptyList(),
    val selectedModel: LanguageModel? = null,
    val customPrompt: String = "",
    val examplePrompts: List<String> = emptyList()
)

@Composable
fun BasePromptSettingsScreen(
    state: PromptSettingsState,
    providers: List<ApiProvider>,
    description: String,
    onPromptChanged: (String) -> Unit,
    onSaveClicked: () -> Unit,
    onModelSelected: (LanguageModel?) -> Unit,
    hints: Array<String>,
    enabled: Boolean = true
) {
    AppCard {
    Column(
        modifier = Modifier
            .fillMaxWidth()
            .padding(horizontal = 0.dp)
    ) {
        Text(
            text = description,
            style = MaterialTheme.typography.bodyLarge,
            modifier = Modifier.padding(vertical = 16.dp),
            color = if (!enabled) MaterialTheme.colorScheme.onSurface.copy(alpha = 0.6f) else MaterialTheme.colorScheme.onSurface
        )


            Column(Modifier.padding(16.dp)) {
                InspiringSearchField(
                    value = state.customPrompt,
                    onValueChange = onPromptChanged,
                    //label = { Text(stringResource(id = R.string.text_enter_your_custom_prompt)) },
                    minLines = 3,
                    maxLines = 5,
                    hints = hints,
                    enabled = enabled
                )

                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(top = 16.dp),
                    verticalAlignment = Alignment.CenterVertically,
                    horizontalArrangement = Arrangement.SpaceBetween
                ) {

                    Box(modifier = Modifier.weight(1f)) {
                        ApiModelDropDown(
                            models = state.availableModels,
                            providers = providers, // Pass providers down
                            selectedModel = state.selectedModel,
                            onModelSelected = onModelSelected,
                            enabled = enabled
                        )
                    }

                    Spacer(modifier = Modifier.width(16.dp))

                    AppButton(onClick = onSaveClicked, enabled = enabled) {
                        Text(stringResource(id = R.string.label_save))
                    }
                }
            }


    }
    }
}





@Suppress("HardCodedStringLiteral")
@ThemePreviews
@Composable
fun BasePromptSettingsScreenPreview() {
    val previewProviders = listOf(
        ApiProvider("openai", "OpenAI", "", "", "", emptyList()),
        ApiProvider("anthropic", "Anthropic", "", "", "", emptyList())
    )
    val state = PromptSettingsState(
        availableModels = listOf(
            LanguageModel("gpt-4", "GPT-4", "openai", "OpenAI's most capable model."),
            LanguageModel("claude-2", "Claude 2", "anthropic", "Anthropic's largest model.")
        ),
        selectedModel = LanguageModel("gpt-4", "GPT-4", "openai", "OpenAI's most capable model."),
        customPrompt = "Translate the following English text to French:",
        examplePrompts = listOf(
            "Summarize this article for me.",
            "Explain this concept in simple terms.",
            "Write a poem about nature."
        )
    )
    BasePromptSettingsScreen(
        state = state,
        providers = previewProviders,
        description = "Configure your translation prompt settings here. You can choose a language model and set a custom prompt.",
        onPromptChanged = {},
        onSaveClicked = {},
        onModelSelected = {},
        hints = listOf("test1", "test2").toTypedArray(),
    )
}
