package com.isidroid.ui.print_job._components

import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import coil.compose.AsyncImage
import coil.request.ImageRequest

@Composable
internal fun ListCardImageComponent(
    cover: String?,
    modifier: Modifier
) {
    Card(
        modifier = modifier.size(72.dp),
        shape = RoundedCornerShape(12.dp),
        border = BorderStroke((.5).dp, Color.Gray.copy(alpha = .35f)),
        elevation = CardDefaults.cardElevation(defaultElevation = 12.dp)
    ) {
        if (!cover.isNullOrEmpty())
            ImageComponent(cover)
        else
            NoImageComponent()
    }
}

@Composable
private fun ImageComponent(cover: String?) {
    AsyncImage(
        model = ImageRequest.Builder(LocalContext.current)
            .data(cover)
            .crossfade(true)
            .build(),
        contentDescription = null,
        contentScale = ContentScale.Crop,
        modifier = Modifier.fillMaxSize()
    )
}


@Composable
private fun NoImageComponent() {
    Card(
        modifier = Modifier.fillMaxSize(),
        shape = RoundedCornerShape(8.dp),
        border = BorderStroke((.5).dp, Color.Gray.copy(alpha = .35f)),
        elevation = CardDefaults.cardElevation(defaultElevation = 12.dp)
    ) {
        Box(
            modifier = Modifier.fillMaxSize(),
            contentAlignment = Alignment.Center
        ) {
            Text(
                text = "No Preview",
                style = MaterialTheme.typography.bodySmall,
                fontWeight = FontWeight.Medium,
                color = MaterialTheme.colorScheme.onSurface.copy(alpha = .75f)
            )
        }
    }
}


@Composable
@Preview
fun NoImageComponentPreview() {
    Surface(modifier = Modifier.padding(12.dp)) {
        Box(Modifier.padding(40.dp)) {
            ListCardImageComponent(cover = null, modifier = Modifier.size(96.dp))
        }
    }
}