package eu.gaudian.translator.view.vocabulary


import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.foundation.Canvas
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.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import eu.gaudian.translator.R
import eu.gaudian.translator.ui.theme.ThemePreviews
import eu.gaudian.translator.view.composable.AppIcons

@Composable
fun ExerciseProgressIndicator(
    correctAnswers: Int,
    wrongAnswers: Int,
    totalItems: Int,
    onClose: () -> Unit,
    modifier: Modifier = Modifier,
    correctColor: Color = MaterialTheme.colorScheme.primary,
    wrongColor: Color = MaterialTheme.colorScheme.error,
    trackColor: Color = MaterialTheme.colorScheme.outline
) {
    val targetCorrectFraction = if (totalItems > 0) correctAnswers.toFloat() / totalItems else 0f
    val targetWrongFraction = if (totalItems > 0) wrongAnswers.toFloat() / totalItems else 0f

    val animatedCorrectFraction by animateFloatAsState(targetValue = targetCorrectFraction, label = "correctAnimation")
    val animatedWrongFraction by animateFloatAsState(targetValue = targetWrongFraction, label = "wrongAnimation")
    val totalProgressFraction = animatedCorrectFraction + animatedWrongFraction

    Row(
        modifier = modifier
            .fillMaxWidth()
            .padding(horizontal = 8.dp, vertical = 8.dp),
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.spacedBy(8.dp)
    ) {
        IconButton(onClick = onClose, modifier = Modifier.size(28.dp)) {
            Icon(
                imageVector = AppIcons.Close,
                contentDescription = stringResource(R.string.label_close_exercise),
                modifier = Modifier.size(36.dp),
                tint = MaterialTheme.colorScheme.onSurface
            )
        }

        Box(
            modifier = Modifier
                .weight(1f)
                .height(32.dp),
            contentAlignment = Alignment.Center
        ) {
            Canvas(
                modifier = Modifier
                    .matchParentSize()
                    .clip(RoundedCornerShape(8.dp))
            ) {
                drawRect(color = trackColor)
                drawRect(
                    color = wrongColor,
                    size = Size(width = size.width * totalProgressFraction, height = size.height)
                )
                drawRect(
                    color = correctColor,
                    size = Size(width = size.width * animatedCorrectFraction, height = size.height)
                )
            }

            ProgressContent(
                correctAnswers = correctAnswers,
                wrongAnswers = wrongAnswers,
                color = MaterialTheme.colorScheme.onPrimary
            )


        }

        Text(
            text = "/ $totalItems",
            style = MaterialTheme.typography.bodyMedium,
            color = MaterialTheme.colorScheme.onSurfaceVariant,
            fontWeight = FontWeight.SemiBold
        )
    }
}

@Composable
private fun ProgressContent(correctAnswers: Int, wrongAnswers: Int, color: Color) {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .padding(horizontal = 10.dp),
        horizontalArrangement = Arrangement.SpaceBetween,
        verticalAlignment = Alignment.CenterVertically
    ) {
        Row(
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.spacedBy(4.dp)
        ) {
            Icon(
                imageVector = AppIcons.Check,
                contentDescription = stringResource(R.string.correct_answers),
                tint = color,
                modifier = Modifier.size(16.dp, 16.dp)
            )
            Text(
                text = "$correctAnswers",
                color = color,
                fontSize = 14.sp,
                fontWeight = FontWeight.Bold
            )
        }

        Row(
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.spacedBy(4.dp)
        ) {
            Text(
                text = "$wrongAnswers",
                color = color,
                fontSize = 14.sp,
                fontWeight = FontWeight.Bold
            )
            Icon(
                imageVector = AppIcons.Cancel,
                contentDescription = stringResource(R.string.label_wrong_answers),
                tint = color,
                modifier = Modifier.size(16.dp, 16.dp)
            )
        }
    }
}


@ThemePreviews
@Composable
private fun ExerciseProgressIndicatorPreview() {
    Column(verticalArrangement = Arrangement.spacedBy(16.dp), modifier = Modifier.padding(16.dp)) {
        ExerciseProgressIndicator(
            correctAnswers = 0,
            wrongAnswers = 0,
            totalItems = 20,
            onClose = {}
        )
        ExerciseProgressIndicator(
            correctAnswers = 5,
            wrongAnswers = 2,
            totalItems = 20,
            onClose = {}
        )
        ExerciseProgressIndicator(
            correctAnswers = 10,
            wrongAnswers = 8,
            totalItems = 20,
            onClose = {}
        )
    }
}