package com.atridad.ascently.ui.components

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp

@Composable
fun ImageDisplay(
    imagePaths: List<String>,
    modifier: Modifier = Modifier,
    imageSize: Int = 120,
    onImageClick: ((Int) -> Unit)? = null,
) {
    LocalContext.current

    if (imagePaths.isNotEmpty()) {
        LazyRow(modifier = modifier, horizontalArrangement = Arrangement.spacedBy(8.dp)) {
            itemsIndexed(imagePaths) { index, imagePath ->
                OrientationAwareImage(
                    imagePath = imagePath,
                    contentDescription = "Problem photo",
                    modifier =
                    Modifier.size(imageSize.dp)
                        .clip(RoundedCornerShape(8.dp))
                        .clickable(enabled = onImageClick != null) {
                            onImageClick?.invoke(index)
                        },
                    contentScale = ContentScale.Crop,
                )
            }
        }
    }
}

@Composable
fun ImageDisplaySection(
    imagePaths: List<String>,
    modifier: Modifier = Modifier,
    title: String = "Photos",
    onImageClick: ((Int) -> Unit)? = null,
) {
    if (imagePaths.isNotEmpty()) {
        Column(modifier = modifier) {
            Text(
                text = title,
                style = MaterialTheme.typography.titleMedium,
                color = MaterialTheme.colorScheme.onSurface,
            )

            Spacer(modifier = Modifier.height(8.dp))

            ImageDisplay(imagePaths = imagePaths, imageSize = 120, onImageClick = onImageClick)
        }
    }
}
