package com.aiosman.riderpro.ui.index.tabs.profile.composable

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.Composable
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
import com.aiosman.riderpro.LocalNavController
import com.aiosman.riderpro.entity.MomentEntity
import com.aiosman.riderpro.ui.composables.CustomAsyncImage
import com.aiosman.riderpro.ui.modifiers.noRippleClickable
import com.aiosman.riderpro.ui.navigateToPost

@Composable
fun GalleryItem(
    moment: MomentEntity,
    idx: Int = 0
) {
    val navController = LocalNavController.current

    Box(
        modifier = Modifier
            .fillMaxWidth()
            .let {
                val firstImage = moment.images.firstOrNull()
                if (firstImage?.width != null &&
                    firstImage.height != null &&
                    firstImage.width!! > 0 &&
                    firstImage.height!! > 0
                ) {
                    val ratio = firstImage.width!!.toFloat() / firstImage.height!!.toFloat()
                    return@let it.aspectRatio(ratio.coerceIn(0.7f, 1.5f))
                } else {
                    return@let it.aspectRatio(if (idx % 3 == 0) 1.5f else 1f)
                }
            }
            .clip(RoundedCornerShape(8.dp))
            .noRippleClickable {
                navController.navigateToPost(
                    moment.id
                )
            }


    ) {
        CustomAsyncImage(
            LocalContext.current,
            moment.images[0].thumbnail,
            modifier = Modifier
                .fillMaxSize(),
            contentDescription = "",
            contentScale = ContentScale.Crop,
        )
    }
}