package eu.gaudian.translator.view.vocabulary
import androidx.compose.foundation.layout.Arrangement
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.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
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.res.stringResource
import androidx.compose.ui.unit.dp
import eu.gaudian.translator.R
import eu.gaudian.translator.view.composable.AppIcons
import eu.gaudian.translator.view.composable.AppOutlinedButton
import eu.gaudian.translator.view.composable.AppOutlinedTextField
import eu.gaudian.translator.view.composable.CorrectButton
import eu.gaudian.translator.view.composable.WrongButton

@Composable
fun ExerciseControls(
    state: VocabularyExerciseState?,
    onAction: (VocabularyExerciseAction) -> Unit
) {
    if (state == null) return // Don't show controls if exercise is finished

    var spellingAnswer by remember { mutableStateOf("") }

    LaunchedEffect(state.item.id) {
        spellingAnswer = ""
    }

    val isRevealed = state.isRevealed
    val isAnswered = state.isCorrect != null

    Column(modifier = Modifier.padding(horizontal = 16.dp, vertical = 8.dp)) {
        // Input field for spelling, only shown before the answer is revealed.
        if (!isRevealed && state is VocabularyExerciseState.Spelling) {
            AppOutlinedTextField(
                value = spellingAnswer,
                onValueChange = { spellingAnswer = it },
                label = { Text(stringResource(R.string.type_the_translation)) },
                modifier = Modifier.fillMaxWidth()
            )
            Spacer(modifier = Modifier.height(8.dp))
        }

        Row(
            modifier = Modifier.fillMaxWidth(),
            horizontalArrangement = Arrangement.SpaceEvenly
        ) {
            if (!isRevealed) {
                when (state) {
                    is VocabularyExerciseState.Spelling -> {
                        AppOutlinedButton(
                            onClick = { onAction(VocabularyExerciseAction.Submit(spellingAnswer)) },
                            modifier = Modifier.fillMaxWidth()
                        ) {
                            Icon(AppIcons.Check, contentDescription = stringResource(R.string.text_check))
                            Spacer(modifier = Modifier.padding(4.dp))
                            Text(stringResource(R.string.text_check))
                        }
                    }
                    is VocabularyExerciseState.Guessing -> {
                        AppOutlinedButton(
                            onClick = { onAction(VocabularyExerciseAction.Reveal) },
                            modifier = Modifier.fillMaxWidth()
                        ) {
                            Icon(AppIcons.Sync, contentDescription = stringResource(R.string.flip_card))
                            Spacer(modifier = Modifier.padding(4.dp))
                            Text(stringResource(R.string.flip_card))
                        }
                    }

                    else -> {
                    }
                }
            }
            else {
                if (isAnswered) {

                    AppOutlinedButton(
                        onClick = { onAction(VocabularyExerciseAction.Next) },
                        modifier = Modifier.fillMaxWidth()
                    ) {
                        Text(stringResource(R.string.next_card))
                    }
                } else {

                    if (state is VocabularyExerciseState.Guessing) {
                        Row(horizontalArrangement = Arrangement.spacedBy(16.dp)) {
                            WrongButton(
                                onClick = { onAction(VocabularyExerciseAction.Submit(false)) },
                                modifier = Modifier.weight(1f)
                            ) {
                                Icon(AppIcons.Close, contentDescription = stringResource(R.string.incorrect))
                                Text(stringResource(R.string.label_wrong))
                            }
                            CorrectButton(
                                onClick = { onAction(VocabularyExerciseAction.Submit(true)) },
                                modifier = Modifier.weight(1f)
                            ) {
                                Icon(AppIcons.Check, contentDescription = stringResource(R.string.label_correct))
                                Text(stringResource(R.string.right))
                            }
                        }
                    }
                }
            }
        }
    }
}