package eu.gaudian.translator.view.vocabulary

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.hilt.lifecycle.viewmodel.compose.hiltViewModel
import androidx.navigation.NavController
import androidx.navigation.NavHostController
import eu.gaudian.translator.R
import eu.gaudian.translator.model.VocabularyStage
import eu.gaudian.translator.utils.findActivity
import eu.gaudian.translator.view.composable.AppScaffold
import eu.gaudian.translator.view.composable.AppTopAppBar
import eu.gaudian.translator.view.vocabulary.widgets.DetailedStageProgressBar
import eu.gaudian.translator.viewmodel.VocabularyViewModel

@Composable
fun StageDetailScreen(
    navController: NavController,
    stage: VocabularyStage,
) {
    val activity = LocalContext.current.findActivity()
    val viewModel = hiltViewModel<VocabularyViewModel>(viewModelStoreOwner = activity)

    val stageMapping by viewModel.stageMapping.collectAsState()
    val dueTodayItems by viewModel.dueTodayItems.collectAsState()

    val itemsInStage = stageMapping.filter { it.value == stage }.keys
    val dueTodayInStage = dueTodayItems.filter { stageMapping[it.id] == stage }

    AppScaffold(
        topBar = {
            AppTopAppBar(
                title = stringResource(R.string.due_today_, stage.toString()),
                onNavigateBack = { navController.popBackStack() },
            )
        }
    ) { paddingValues ->
        Column(modifier = Modifier.padding(paddingValues)) {
            DetailedStageProgressBar(
                stage = stage,
                itemCount = dueTodayInStage.size,
                totalItems = itemsInStage.size,
                onStageTapped = {},
            )

            AllCardsListScreen(
                categoryId = null,
                showDueTodayOnly = true,
                stage = stage,
                onNavigateToItem = { item ->
                    @Suppress("HardCodedStringLiteral")
                    navController.navigate(
                        "vocabulary_card/${item.id}/false/false/false"
                    )
                },
                onNavigateBack = { navController.popBackStack() },
                navController = navController as NavHostController,
                enableNavigationButtons = true
            )
        }
    }
}