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

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.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Alignment
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.model.VocabularyStage
import eu.gaudian.translator.view.composable.AppIcons
import eu.gaudian.translator.view.composable.SecondaryButton
import eu.gaudian.translator.viewmodel.VocabularyViewModel

@Composable
fun AllVocabularyWidget(
    vocabularyViewModel: VocabularyViewModel,
    onOpenAllVocabulary: () -> Unit,
    onStageClicked: (VocabularyStage) -> Unit
) {
    val stageStats = vocabularyViewModel.stageStats.collectAsState().value
    val totalItems = stageStats.sumOf { it.itemCount }


        Column(
            modifier = Modifier.padding(16.dp),
            verticalArrangement = Arrangement.spacedBy(16.dp)
        ) {
            Row(
                modifier = Modifier.fillMaxWidth(),
                verticalAlignment = Alignment.CenterVertically,
                horizontalArrangement = Arrangement.SpaceBetween
            ) {

            }

            if (totalItems > 0) {
                VocabularyStage.entries.forEach { stage ->
                    val stageCount = stageStats.find { it.stage == stage }?.itemCount ?: 0
                    if (stageCount > 0) {
                        StageProgressBar(
                            stage = stage,
                            itemCount = stageCount,
                            totalItems = totalItems,
                            onClick = onStageClicked
                        )
                    }
                }
            } else {
                Box(
                    contentAlignment = Alignment.Center,
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(vertical = 32.dp)
                ) {
                    Text(
                        text = stringResource(R.string.text_no_vocabulary_available),
                        style = MaterialTheme.typography.bodyMedium,
                        color = MaterialTheme.colorScheme.onSurfaceVariant
                    )
                }
            }
            Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.Center) {
            SecondaryButton(
                onClick = onOpenAllVocabulary,
                text = stringResource(R.string.text_view_all),
                icon = AppIcons.ArrowRight
            )
        }
        }
}