package eu.gaudian.translator.view.exercises

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.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
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.unit.dp
import androidx.hilt.lifecycle.viewmodel.compose.hiltViewModel
import eu.gaudian.translator.R
import eu.gaudian.translator.model.Language
import eu.gaudian.translator.utils.findActivity
import eu.gaudian.translator.view.composable.AppDialog
import eu.gaudian.translator.view.composable.AppOutlinedTextField
import eu.gaudian.translator.view.composable.DialogButton
import eu.gaudian.translator.view.composable.SourceLanguageDropdown
import eu.gaudian.translator.view.composable.TargetLanguageDropdown
import eu.gaudian.translator.viewmodel.LanguageViewModel

@Composable
fun YouTubeExerciseDialog(
    onDismiss: () -> Unit,
    onCreate: (youtubeUrl: String, sourceLanguage: Language, targetLanguage: Language) -> Unit,
    presetUrl: String? = null,
    languageOnly: Boolean = false
) {
    val activity = LocalContext.current.findActivity()
    var youtubeUrl by remember { mutableStateOf(presetUrl ?: "") }

    val languageViewModel: LanguageViewModel = hiltViewModel(viewModelStoreOwner = activity)
    val sourceLang = languageViewModel.selectedSourceLanguage.collectAsState().value
    val targetLang = languageViewModel.selectedTargetLanguage.collectAsState().value

    val isCreateEnabled = (languageOnly || youtubeUrl.isNotBlank()) && sourceLang != null

    AppDialog(onDismissRequest = onDismiss) {
        Card(
            modifier = Modifier.fillMaxWidth(),
            colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.surface)
        ) {
            Column(modifier = Modifier.padding(8.dp), verticalArrangement = Arrangement.spacedBy(16.dp)) {
                Text(
                    text = stringResource(R.string.menu_create_youtube_exercise),
                    style = MaterialTheme.typography.titleLarge,
                    color = MaterialTheme.colorScheme.onSurface
                )

                if (!languageOnly) {
                    AppOutlinedTextField(
                        value = youtubeUrl,
                        onValueChange = { youtubeUrl = it },
                        label = { Text(stringResource(R.string.text_youtube_link)) },
                        placeholder = { Text("https://www.youtube.com/watch?v=...") },
                        modifier = Modifier.fillMaxWidth(),
                        singleLine = true,
                        paste = true,
                    )
                }

                Text(stringResource(R.string.text_select_languages), style = MaterialTheme.typography.labelLarge)
                Row(modifier = Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) {
                    Box(modifier = Modifier.weight(1f)) {
                        SourceLanguageDropdown(languageViewModel = languageViewModel, autoEnabled = true, iconEnabled = true)
                    }
                    Spacer(modifier = Modifier.width(8.dp))
                    Box(modifier = Modifier.weight(1f)) {
                        TargetLanguageDropdown(languageViewModel = languageViewModel, iconEnabled = true)
                    }
                }

                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(top = 8.dp),
                    horizontalArrangement = Arrangement.End,
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    TextButton(onClick = onDismiss) { Text(stringResource(R.string.label_cancel)) }
                    DialogButton(onClick = {
                        if (sourceLang != null && targetLang != null) {
                            val finalUrl = (presetUrl ?: youtubeUrl).trim()
                            onCreate(finalUrl, sourceLang, targetLang)
                        }
                        onDismiss()
                    }, enabled = isCreateEnabled) {
                        Text(stringResource(R.string.label_confirm))
                    }
                }
            }
        }
    }
}