package com.pixelized.biblib.ui.screen.home.common.item

import android.annotation.SuppressLint
import android.content.res.Configuration
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.material.Card
import androidx.compose.material.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.tooling.preview.PreviewParameter
import androidx.compose.ui.unit.dp
import coil.compose.AsyncImage
import com.pixelized.biblib.R
import com.pixelized.biblib.ui.theme.BibLibTheme
import com.pixelized.biblib.utils.extention.bibLib
import com.pixelized.biblib.utils.extention.modifier.drawDiagonalLabel
import com.pixelized.biblib.utils.extention.placeholder

@Composable
fun LargeBookThumbnail(
    modifier: Modifier = Modifier,
    thumbnail: BookThumbnailUio?,
    onClick: (BookThumbnailUio) -> Unit,
) {
    Card(
        modifier = modifier
            .fillMaxWidth()
            .wrapContentHeight(),
        shape = MaterialTheme.bibLib.shapes.bookThumbnailCoverLarge,
    ) {
        if (thumbnail != null) {
            LargeBookThumbnailContent(
                modifier = modifier,
                thumbnail = thumbnail,
                onClick = onClick,
            )
        } else {
            LargeBookThumbnailPlaceHolder(
                modifier = modifier,
            )
        }
    }
}

@Composable
private fun LargeBookThumbnailContent(
    modifier: Modifier = Modifier,
    thumbnail: BookThumbnailUio,
    onClick: (BookThumbnailUio) -> Unit,
) {
    AsyncImage(
        modifier = Modifier
            .clip(shape = MaterialTheme.bibLib.shapes.bookThumbnailCoverLarge)
            .fillMaxWidth()
            .aspectRatio(64f / 102f)
            .isNew { thumbnail.isNew }
            .clickable { thumbnail.let(onClick) }
            .then(other = modifier),
        model = thumbnail.cover,
        contentScale = ContentScale.Crop,
        contentDescription = null,
    )
}

@Composable
private fun LargeBookThumbnailPlaceHolder(
    modifier: Modifier = Modifier,
) {
    Box(
        modifier = Modifier
            .fillMaxWidth()
            .aspectRatio(64f / 102f)
            .placeholder(
                shape = MaterialTheme.bibLib.shapes.bookThumbnailCoverLarge,
                visible = { true }
            )
            .then(other = modifier)
    )
}

@SuppressLint("ComposableModifierFactory")
@Composable
private fun Modifier.isNew(
    isNew: () -> Boolean,
) = if (isNew()) {
    val theme = MaterialTheme.bibLib
    val label: String = stringResource(id = R.string.list_is_new)
    val color: Color = Color.Red
    val style: TextStyle = remember {
        theme.typography.base.caption.copy(color = Color.White)
    }
    drawDiagonalLabel(
        text = label,
        color = color,
        style = style,
        labelTextRatio = 4f,
    )
} else {
    this
}

@Composable
@Preview(showBackground = true, uiMode = Configuration.UI_MODE_NIGHT_NO)
@Preview(showBackground = true, uiMode = Configuration.UI_MODE_NIGHT_YES)
private fun LargeBookThumbnailPreview(
    @PreviewParameter(BookThumbnailPreviewProvider::class) preview: BookThumbnailUio?,
) {
    BibLibTheme {
        LargeBookThumbnail(
            modifier = Modifier.width(168.dp),
            thumbnail = preview,
            onClick = { },
        )
    }
}