package eu.darken.sdmse.analyzer.ui.storage.apps

import android.text.format.Formatter
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
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.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import coil.compose.AsyncImage
import coil.request.ImageRequest
import eu.darken.sdmse.analyzer.ui.storage.apps.AppsViewModel.Row
import eu.darken.sdmse.analyzer.ui.storage.preview.previewInstalled
import eu.darken.sdmse.analyzer.ui.storage.preview.previewPkgStat
import eu.darken.sdmse.common.compose.preview.Preview2
import eu.darken.sdmse.common.compose.preview.PreviewWrapper

@Composable
internal fun AppsItemRow(
    modifier: Modifier = Modifier,
    row: Row,
    onClick: () -> Unit = {},
) {
    val context = LocalContext.current
    val pkg = row.pkgStat.pkg
    val sizeText = Formatter.formatShortFileSize(context, row.pkgStat.totalSize)

    Card(
        onClick = onClick,
        modifier = modifier.fillMaxWidth(),
        elevation = CardDefaults.cardElevation(defaultElevation = 1.dp),
    ) {
        Column {
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(12.dp),
                verticalAlignment = Alignment.CenterVertically,
            ) {
                AsyncImage(
                    model = ImageRequest.Builder(context).data(pkg).build(),
                    contentDescription = null,
                    modifier = Modifier.size(40.dp),
                )
                Spacer(Modifier.size(12.dp))
                Column(modifier = Modifier.weight(1f)) {
                    Text(
                        text = pkg.label?.get(context) ?: pkg.packageName,
                        style = MaterialTheme.typography.bodyLarge,
                        maxLines = 1,
                    )
                    Text(
                        text = pkg.packageName,
                        style = MaterialTheme.typography.bodySmall,
                        color = MaterialTheme.colorScheme.onSurfaceVariant,
                        maxLines = 1,
                    )
                }
                Spacer(Modifier.size(12.dp))
                Text(
                    text = sizeText,
                    style = MaterialTheme.typography.labelMedium,
                )
            }
            row.sizeRatio?.let { ratio ->
                Box(
                    modifier = Modifier
                        .fillMaxWidth(ratio.coerceIn(0f, 1f))
                        .height(2.dp)
                        .background(MaterialTheme.colorScheme.secondary),
                )
            }
        }
    }
}

@Preview2
@Composable
private fun AppsItemRowPreview() {
    PreviewWrapper {
        AppsItemRow(
            row = Row(
                pkgStat = previewPkgStat(
                    pkg = previewInstalled(pkgName = "com.example.gallery", label = "Gallery"),
                ),
                sizeRatio = 0.7f,
            ),
            onClick = {},
        )
    }
}
