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

import androidx.compose.foundation.Image
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.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
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.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import eu.gaudian.translator.R
import eu.gaudian.translator.model.LanguageLevels
import eu.gaudian.translator.model.MyAppLanguageLevel
import eu.gaudian.translator.view.composable.ComponentDefaults


/**
 * A composable widget that displays a single language level's information.
 * It shows the level's icon, name, word count, and description in a styled card.
 *
 * @param level The [MyAppLanguageLevel] object to display.
 * @param modifier A [Modifier] for this composable.
 */
@Composable
fun LanguageLevelWidget(level: MyAppLanguageLevel, modifier: Modifier = Modifier) {
    Card(
        modifier = modifier
            .fillMaxWidth()
            .padding(horizontal = 16.dp, vertical = 8.dp),
        elevation = CardDefaults.cardElevation(defaultElevation = ComponentDefaults.DefaultElevation),
        shape = MaterialTheme.shapes.medium
    ) {
        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
                // Note: For proper localization, it's best to use Plurals string resources.
                // For simplicity here, we are using a simple string.
                Text(
                    text = stringResource(R.string.words_known, level.wordsKnown),
                    style = MaterialTheme.typography.bodyMedium,
                    color = MaterialTheme.colorScheme.primary
                )

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

                // Description
                Text(
                    text = stringResource(id = level.descriptionResId),
                    style = MaterialTheme.typography.bodySmall,
                    color = MaterialTheme.colorScheme.onSurfaceVariant
                )
            }
        }
    }
}

/**
 * A preview function to display the LanguageLevelWidget in Android Studio.
 * This uses the "Newborn" tier as an example.
 */
@Suppress("HardCodedStringLiteral")
@Preview(showBackground = true, name = "Single Level Preview")
@Composable
fun LanguageLevelWidgetPreview() {
    // This preview uses the Newborn object from your MyAppLanguageLevel sealed class.
    // Ensure you have a drawable resource named 'ic_level_newborn' and the
    // corresponding string resources for the preview to render correctly.

        LanguageLevelWidget(level = MyAppLanguageLevel.Newborn)

}

/**
 * A preview function that displays all language levels in a scrollable list.
 */
@Suppress("HardCodedStringLiteral")
@Preview(showBackground = true, name = "All Levels List Preview", heightDp = 800)
@Composable
fun AllLanguageLevelsPreview() {

        LazyColumn {
            items(LanguageLevels.all) { level ->
                LanguageLevelWidget(level = level)
            }
        }

}