package eu.gaudian.translator.view.settings

import android.annotation.SuppressLint
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.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.stringResource
import androidx.compose.ui.unit.dp
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.AppCard
import eu.gaudian.translator.view.composable.AppScaffold
import eu.gaudian.translator.view.composable.AppTopAppBar
import eu.gaudian.translator.view.composable.OptionItemSwitch
import eu.gaudian.translator.viewmodel.ApiViewModel
import eu.gaudian.translator.viewmodel.SettingsViewModel

@SuppressLint("LocalContextResourcesRead")
@Composable
fun TranslationSettingsScreen(
    navController: NavController,
) {
    val activity = LocalContext.current.findActivity()
    val settingsViewModel: SettingsViewModel = hiltViewModel(viewModelStoreOwner = activity)
    val apiViewModel: ApiViewModel = hiltViewModel(viewModelStoreOwner = activity)
    val context = LocalContext.current
    val customPrompt by settingsViewModel.customPrompt.collectAsStateWithLifecycle()
    val availableModels by apiViewModel.allValidModels.collectAsStateWithLifecycle()
    val allProviders by apiViewModel.allValidProviders.collectAsStateWithLifecycle()
    val selectedModel by apiViewModel.translationModel.collectAsStateWithLifecycle()
    var tempPrompt by remember(customPrompt) { mutableStateOf(customPrompt) }
    val examplePrompts = remember {
        context.resources.getStringArray(R.array.example_prompts).toList()
    }

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

    val useLibre by settingsViewModel.useLibreTranslate.collectAsStateWithLifecycle()

    AppScaffold(
        topBar = {
            AppTopAppBar(
                title = stringResource(R.string.label_translation_settings),
                onNavigateBack = { navController.popBackStack() },
                hintContent = null //TODO add hint
            )
        }
    ) { paddingValues ->
        Column(
            modifier = Modifier
                .padding(paddingValues)
                .fillMaxWidth()
                .verticalScroll(rememberScrollState())
                .padding(8.dp)
        ) {
            AppCard{
            Row(modifier = Modifier.fillMaxWidth().padding(0.dp),
                    ) {

                    OptionItemSwitch(
                        title = stringResource(R.string.toggle_use_libretranslate),
                        description = stringResource(R.string.toggle_use_libretranslate_desc),
                        checked = useLibre,
                        onCheckedChange = { settingsViewModel.setUseLibreTranslate(it) },
                    )
                }}


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

                BasePromptSettingsScreen(
                    state = screenState,
                    providers = allProviders,
                    description = stringResource(R.string.text_translation_instructions),
                    onPromptChanged = { tempPrompt = it },
                    onSaveClicked = { settingsViewModel.saveCustomPrompt(tempPrompt) },
                    onModelSelected = { apiViewModel.setTranslationModel(it) },
                    hints = context.resources.getStringArray(R.array.example_prompts),
                    //enabled = !useLibre
                )
            }
        }
    }
