package eu.gaudian.translator.view.vocabulary.widgets

import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
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.foundation.layout.size
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedCard
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.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import eu.gaudian.translator.R
import eu.gaudian.translator.model.LanguageLevels
import eu.gaudian.translator.model.MyAppLanguageLevel

/**
 * Main widget that displays word statistics and the user's current language level.
 */
@Composable
fun LevelWidget(
    totalWords: Int,
    learnedWords: Int,
    onNavigateToProgress: () -> Unit // Add this parameter
) {

        Column(
            modifier = Modifier
                .fillMaxWidth()
                .clickable { onNavigateToProgress() }, // Make whole widget clickable
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            // Original row with word statistics
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(16.dp),
                horizontalArrangement = Arrangement.SpaceAround
            ) {
                StatItem(value = totalWords.toString(), label = stringResource(R.string.label_total_words))
                StatItem(value = learnedWords.toString(), label = stringResource(R.string.label_learned))
                StatItem(
                    value = (totalWords - learnedWords).toString(),
                    label = stringResource(R.string.remaining)
                )
            }

            // Determine the current language level based on the number of learned words.
            val currentLevel = LanguageLevels.getLevelForWords(learnedWords)

            // Display the detailed language level widget below the stats.
            LanguageLevelInfoWidget(level = currentLevel)
        }
}

/**
 * A composable that displays a single statistic (e.g., total words).
 */
@Composable
fun StatItem(value: String, label: String) {
    Column(horizontalAlignment = Alignment.CenterHorizontally) {
        Text(text = value, style = MaterialTheme.typography.headlineMedium)
        Spacer(modifier = Modifier.height(4.dp))
        Text(
            text = label,
            style = MaterialTheme.typography.bodyMedium,
            color = MaterialTheme.colorScheme.onSurfaceVariant
        )
    }
}

/**
 * A widget (previously LanguageLevelWidget) that displays detailed information
 * about a single language level in a styled card.
 *
 * @param level The [MyAppLanguageLevel] object to display.
 * @param modifier A [Modifier] for this composable.
 */
@Composable
fun LanguageLevelInfoWidget(level: MyAppLanguageLevel, modifier: Modifier = Modifier) {
    OutlinedCard(
        modifier = modifier
            .fillMaxWidth()
            .padding(horizontal = 16.dp, vertical = 8.dp),
    ) {
        Row(
            modifier = Modifier.padding(16.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            // Level Icon
            Image(
                painter = painterResource(id = level.iconResId),
                contentDescription = stringResource(id = level.nameResId),
                modifier = Modifier
                    .size(64.dp)
                    .padding(end = 16.dp)
            )

            // Level Details Column
            Column(modifier = Modifier.weight(1f)) {
                // Level Name
                Text(
                    text = stringResource(id = level.nameResId),
                    style = MaterialTheme.typography.titleLarge,
                    fontWeight = FontWeight.Bold
                )
                Spacer(modifier = Modifier.height(4.dp))
                // Words Known
                Text(
                    text = stringResource(R.string.words_known, level.wordsKnown),
                    style = MaterialTheme.typography.bodyMedium,
                    color = MaterialTheme.colorScheme.primary
                )
                Spacer(modifier = Modifier.height(8.dp))
                Text(
                    text = stringResource(id = level.descriptionResId),
                    style = MaterialTheme.typography.bodySmall,
                    color = MaterialTheme.colorScheme.onSurfaceVariant
                )
            }
        }
    }
}