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.fillMaxWidth
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.material.Card
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
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.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.tooling.preview.PreviewParameter
import androidx.compose.ui.unit.dp
import androidx.constraintlayout.compose.ConstraintLayout
import androidx.constraintlayout.compose.Dimension
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 SmallBookThumbnail(
    modifier: Modifier = Modifier,
    thumbnail: BookThumbnailUio?,
    onClick: (BookThumbnailUio) -> Unit,
) {
    if (thumbnail != null) {
        SmallBookThumbnailContent(
            modifier = modifier,
            thumbnail = thumbnail,
            onClick = onClick,
        )
    } else {
        SmallBookThumbnailPlaceHolder(
            modifier = modifier,
        )
    }
}

@Composable
private fun SmallBookThumbnailContent(
    modifier: Modifier = Modifier,
    thumbnail: BookThumbnailUio,
    onClick: (BookThumbnailUio) -> Unit,
) {
    Card(
        modifier = modifier
            .clickable { onClick(thumbnail) }
            .fillMaxWidth()
            .wrapContentHeight(),
        shape = MaterialTheme.bibLib.shapes.bookThumbnailCoverSmall,
    ) {
        ConstraintLayout(
            modifier = Modifier.fillMaxWidth(),
        ) {
            val (cover, title, author, series, genre, date) = createRefs()

            AsyncImage(
                modifier = Modifier
                    .constrainAs(cover) {
                        top.linkTo(parent.top)
                        bottom.linkTo(parent.bottom)
                        start.linkTo(parent.start)
                    }
                    .clip(shape = MaterialTheme.bibLib.shapes.bookThumbnailCoverSmall)
                    .size(size = MaterialTheme.bibLib.dimen.thumbnail.cover)
                    .isNew { thumbnail.isNew },
                model = thumbnail.thumbnail,
                contentScale = ContentScale.Crop,
                contentDescription = null,
            )

            Text(
                modifier = Modifier.constrainAs(title) {
                    top.linkTo(parent.top, margin = 8.dp)
                    start.linkTo(cover.end, margin = 8.dp)
                    end.linkTo(parent.end, margin = 8.dp)
                    width = Dimension.fillToConstraints
                },
                style = MaterialTheme.typography.h6,
                color = MaterialTheme.bibLib.colors.typography.medium,
                text = thumbnail.title,
                maxLines = 2,
                overflow = TextOverflow.Ellipsis,
            )

            Text(
                modifier = Modifier.constrainAs(author) {
                    top.linkTo(title.bottom, margin = 4.dp)
                    bottom.linkTo(series.top, margin = 4.dp)
                    start.linkTo(cover.end, margin = 8.dp)
                    end.linkTo(parent.end, margin = 8.dp)
                    width = Dimension.fillToConstraints
                    height = Dimension.fillToConstraints
                },
                style = MaterialTheme.typography.body1,
                color = MaterialTheme.bibLib.colors.typography.light,
                maxLines = 1,
                overflow = TextOverflow.Ellipsis,
                text = thumbnail.author ?: ""
            )

            Text(
                modifier = Modifier.constrainAs(series) {
                    top.linkTo(author.bottom, margin = 4.dp)
                    bottom.linkTo(genre.top, margin = 4.dp)
                    start.linkTo(cover.end, margin = 8.dp)
                    end.linkTo(parent.end, margin = 8.dp)
                    width = Dimension.fillToConstraints
                },
                style = MaterialTheme.typography.caption,
                color = MaterialTheme.bibLib.colors.typography.light,
                maxLines = 1,
                overflow = TextOverflow.Ellipsis,
                text = thumbnail.series ?: ""
            )

            Text(
                modifier = Modifier.constrainAs(genre) {
                    bottom.linkTo(parent.bottom, margin = 8.dp)
                    start.linkTo(cover.end, margin = 8.dp)
                    end.linkTo(date.start, margin = 8.dp)
                    width = Dimension.fillToConstraints
                },
                maxLines = 1,
                overflow = TextOverflow.Ellipsis,
                style = MaterialTheme.typography.caption,
                color = MaterialTheme.bibLib.colors.typography.light,
                text = thumbnail.genre ?: ""
            )

            Text(
                modifier = Modifier.constrainAs(date) {
                    bottom.linkTo(parent.bottom, margin = 8.dp)
                    end.linkTo(parent.end, margin = 8.dp)
                },
                style = MaterialTheme.typography.caption,
                color = MaterialTheme.bibLib.colors.typography.light,
                text = thumbnail.date ?: ""
            )
        }
    }
}

@Composable
private fun SmallBookThumbnailPlaceHolder(
    modifier: Modifier = Modifier,
) {
    Card(
        modifier = modifier
            .fillMaxWidth()
            .wrapContentHeight(),
        shape = MaterialTheme.bibLib.shapes.bookThumbnailCoverSmall,
    ) {
        ConstraintLayout(
            modifier = Modifier.fillMaxWidth(),
        ) {
            val (cover, title, author, genre, date) = createRefs()

            Box(
                modifier = Modifier
                    .constrainAs(cover) {
                        top.linkTo(parent.top)
                        bottom.linkTo(parent.bottom)
                        start.linkTo(parent.start)
                    }
                    .size(size = MaterialTheme.bibLib.dimen.thumbnail.cover)
                    .placeholder(
                        shape = MaterialTheme.bibLib.shapes.bookThumbnailCoverSmall,
                        visible = { true },
                    ),
            )

            Box(
                modifier = Modifier
                    .constrainAs(title) {
                        top.linkTo(parent.top, margin = 8.dp)
                        start.linkTo(cover.end, margin = 8.dp)
                    }
                    .size(width = 120.dp, height = 16.dp)
                    .placeholder { true },
            )

            Box(
                modifier = Modifier
                    .constrainAs(author) {
                        top.linkTo(title.bottom, margin = 4.dp)
                        start.linkTo(cover.end, margin = 8.dp)
                    }
                    .size(width = 100.dp, height = 16.dp)
                    .placeholder { true },
            )

            Box(
                modifier = Modifier
                    .constrainAs(genre) {
                        bottom.linkTo(parent.bottom, margin = 8.dp)
                        start.linkTo(cover.end, margin = 8.dp)
                    }
                    .size(width = 80.dp, height = 16.dp)
                    .placeholder { true },
            )

            Box(
                modifier = Modifier
                    .constrainAs(date) {
                        bottom.linkTo(parent.bottom, margin = 8.dp)
                        end.linkTo(parent.end, margin = 8.dp)
                    }
                    .size(width = 40.dp, height = 16.dp)
                    .placeholder { true },
            )
        }
    }
}

@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 = 2f,
    )
} else {
    this
}

@Composable
@Preview(showBackground = true, uiMode = Configuration.UI_MODE_NIGHT_NO)
@Preview(showBackground = true, uiMode = Configuration.UI_MODE_NIGHT_YES)
private fun SmallBookThumbnailPreview(
    @PreviewParameter(BookThumbnailPreviewProvider::class) preview: BookThumbnailUio?,
) {
    BibLibTheme {
        SmallBookThumbnail(
            thumbnail = preview,
            onClick = { },
        )
    }
}