package eu.gaudian.translator.view.exercises

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
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.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.hilt.lifecycle.viewmodel.compose.hiltViewModel
import eu.gaudian.translator.R
import eu.gaudian.translator.model.Exercise
import eu.gaudian.translator.utils.findActivity
import eu.gaudian.translator.view.composable.AppButton
import eu.gaudian.translator.view.composable.AppCard
import eu.gaudian.translator.view.composable.AppIcons
import eu.gaudian.translator.view.composable.AppOutlinedButton
import eu.gaudian.translator.view.composable.AppOutlinedCard
import eu.gaudian.translator.viewmodel.ExerciseViewModel

@Composable
fun ExerciseListScreen(
    onExerciseClicked: (Exercise) -> Unit,
    onLongExerciseClicked: (Exercise) -> Unit,
    onDeleteClicked: (Exercise) -> Unit,
) {
    val activity = LocalContext.current.findActivity()
    val exerciseViewModel: ExerciseViewModel = hiltViewModel(activity)
    val exercises by exerciseViewModel.exercises.collectAsState()

    AppOutlinedCard{

    LazyColumn(
        modifier = Modifier.fillMaxSize(),
        contentPadding = PaddingValues(8.dp)
    ) {
        items(exercises) { exercise ->
            ExerciseListItem(
                exercise = exercise,
                onClick = { onExerciseClicked(exercise) },
                onLongClick = { onLongExerciseClicked(exercise) },
                onDelete = { onDeleteClicked(exercise) }
            )
        }
    }
}
}

@Composable
private fun ExerciseListItem(
    exercise: Exercise,
    onClick: () -> Unit,
    onLongClick: () -> Unit,
    onDelete: () -> Unit
) {
    AppCard(
        modifier = Modifier
            .fillMaxWidth()
            .padding(vertical = 4.dp),
    ) {
        Row(
            modifier = Modifier.padding(start = 16.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            Icon(
                imageVector = AppIcons.Quiz,
                contentDescription = stringResource(R.string.label_exercise),
                modifier = Modifier.size(40.dp)
            )
            Spacer(modifier = Modifier.width(16.dp))
            Column(
                modifier = Modifier
                    .weight(1f)
                    .padding(vertical = 8.dp),
                verticalArrangement = Arrangement.spacedBy(4.dp)
            ) {
                Text(text = exercise.title, style = MaterialTheme.typography.titleMedium)
                Text(
                    text = stringResource(R.string.questions, exercise.questions.size),
                    style = MaterialTheme.typography.bodyMedium
                )
                // Add the new button
                Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
                    AppButton(onClick = onClick,             shape = RoundedCornerShape(8.dp),
                    ) {
                        Text(stringResource(R.string.label_start))
                    }
                    AppOutlinedButton(onClick = onLongClick,             shape = RoundedCornerShape(8.dp)) {
                        Text(stringResource(R.string.start_long))
                    }
                }
            }
            IconButton(onClick = onDelete) {
                Icon(AppIcons.Delete, contentDescription = stringResource(R.string.delete_exercise))
            }
        }
    }
}