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

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.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.hilt.lifecycle.viewmodel.compose.hiltViewModel
import eu.gaudian.translator.R
import eu.gaudian.translator.ui.theme.semanticColors
import eu.gaudian.translator.utils.findActivity
import eu.gaudian.translator.view.composable.AppIcons
import eu.gaudian.translator.viewmodel.VocabularyViewModel

@Composable
fun StatusWidget(
    onNavigateToNew: () -> Unit,
    onNavigateToDuplicates: () -> Unit,
    onNavigateToFaulty: () -> Unit,
    onNavigateToNoGrammar: () -> Unit,
    onNavigateToMissingLanguage: (Int) -> Unit
) {

    val activity = LocalContext.current.findActivity()
    val vocabularyViewModel: VocabularyViewModel = hiltViewModel(viewModelStoreOwner = activity)
    val newItemsCount by vocabularyViewModel.newItemsCount.collectAsState()
    val duplicateCount by vocabularyViewModel.duplicateItemsCount.collectAsState()
    val faultyItemsCount by vocabularyViewModel.faultyItemsCount.collectAsState()
    val itemsWithoutGrammarCount by vocabularyViewModel.itemsWithoutGrammarCount.collectAsState()
    val missingLanguageInfo by vocabularyViewModel.missingLanguageInfo.collectAsState()

    val hasIssues = newItemsCount > 0 || duplicateCount > 0 || faultyItemsCount > 0 || itemsWithoutGrammarCount > 0 || missingLanguageInfo.isNotEmpty()

    Column(
        modifier = Modifier.padding(horizontal = 16.dp, vertical = 8.dp),
        verticalArrangement = Arrangement.spacedBy(8.dp)
    ) {
        if (hasIssues) {
            if (newItemsCount > 0) {
                StatusItem(
                    icon = AppIcons.Sort,
                    text = stringResource(R.string.status_widget_new_items),
                    count = newItemsCount,
                    onClick = onNavigateToNew,
                    color = MaterialTheme.colorScheme.primary
                )
            }
            if (duplicateCount > 0) {
                StatusItem(
                    icon = AppIcons.Error,
                    text = stringResource(R.string.status_widget_duplicates),
                    count = duplicateCount,
                    onClick = onNavigateToDuplicates,
                    color = MaterialTheme.colorScheme.error
                )
            }
            if (faultyItemsCount > 0) {
                StatusItem(
                    icon = AppIcons.Error,
                    text = stringResource(R.string.status_widget_faulty_items),
                    count = faultyItemsCount,
                    onClick = onNavigateToFaulty,
                    color = MaterialTheme.colorScheme.error
                )
            }
            missingLanguageInfo.forEach { (languageId, count) ->
                StatusItem(
                    icon = AppIcons.Error,
                    text = stringResource(R.string.language_with_id_d_not_found, languageId),
                    count = count,
                    onClick = { onNavigateToMissingLanguage(languageId) },
                    color = MaterialTheme.colorScheme.error
                )
            }
            if (itemsWithoutGrammarCount > 0) {
                StatusItem(
                    icon = AppIcons.Error,
                    text = stringResource(R.string.items_without_grammar_infos),
                    count = itemsWithoutGrammarCount,
                    onClick = onNavigateToNoGrammar,
                    color = MaterialTheme.colorScheme.error
                )
            }
        } else {
            StatusItem(
                icon = AppIcons.CheckCircle,
                text = stringResource(R.string.status_widget_all_good),
                onClick = null,
                color = MaterialTheme.semanticColors.success
            )
        }
    }
}

@Composable
private fun StatusItem(
    icon: ImageVector,
    text: String,
    onClick: (() -> Unit)?,
    color: Color,
    count: Int? = null
) {
    var modifier = Modifier.fillMaxWidth()
    if (onClick != null) {
        modifier = modifier.clickable(onClick = onClick)
    }

    Row(
        modifier = modifier.padding(vertical = 8.dp),
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.spacedBy(16.dp)
    ) {
        Icon(
            imageVector = icon,
            contentDescription = null,
            tint = color
        )
        Text(
            modifier = Modifier.weight(1f),
            text = text,
            style = MaterialTheme.typography.bodyLarge,
            color = MaterialTheme.colorScheme.onSurface
        )
        if (count != null) {
            Text(
                text = count.toString(),
                style = MaterialTheme.typography.bodyLarge,
                color = color,
                fontWeight = FontWeight.Bold
            )
        }
    }
}