package com.isidroid.ui.print_job

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.ElevatedCard
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.constraintlayout.compose.ConstraintLayout
import androidx.constraintlayout.compose.Dimension
import com.isidroid.ui.print_job._components.CardInfoComponent
import com.isidroid.ui.print_job._components.ListCardImageComponent
import com.isidroid.utils.asCost
import java.util.Date

@Composable
fun PrintJobListItemComponent(
    cost: Float,
    accessCode: String,
    spotName: String,
    cover: String?,
    statusName: String,
    statusColor: Color,
    createdAt: Date,
    modifier: Modifier = Modifier,
    clickOnSpot: () -> Unit = {},
    clickOnCard: () -> Unit = {},
) {
    ElevatedCard(
        onClick = clickOnCard,
        modifier = modifier.padding(vertical = 4.dp),
        colors = CardDefaults.elevatedCardColors()
    ) {
        ConstraintLayout(
            modifier = Modifier
                .fillMaxWidth()
                .padding(horizontal = 12.dp, vertical = 8.dp)
        ) {
            val (image, titleView, statusView, costView, accessCodeView) = createRefs()

            ListCardImageComponent(cover = cover,
                modifier = Modifier.constrainAs(image) {
                    start.linkTo(parent.start)
                    top.linkTo(parent.top)
                })

            Text(
                text = spotName,
                fontSize = 16.sp,
                fontWeight = FontWeight.W600,
                modifier = Modifier
                    .clickable { clickOnSpot() }
                    .constrainAs(titleView) {
                        start.linkTo(image.end, 8.dp)
                        end.linkTo(costView.start, 8.dp)
                        top.linkTo(image.top, 4.dp)
                        width = Dimension.fillToConstraints
                    }
            )

            CardInfoComponent(
                code = accessCode,
                createdAt = createdAt,
                modifier = Modifier.constrainAs(accessCodeView) {
                    linkTo(start = titleView.start, end = titleView.end)
                    top.linkTo(titleView.bottom, 8.dp)
                    width = Dimension.fillToConstraints
                }
            )

            Text(
                text = cost.asCost(),
                fontWeight = FontWeight.Medium,
                fontSize = 20.sp,
                fontFamily = FontFamily.Monospace,
                modifier = Modifier.constrainAs(costView) {
                    top.linkTo(parent.top, 8.dp)
                    end.linkTo(parent.end)
                }
            )

            Text(
                text = statusName,
                fontSize = 11.sp,
                fontWeight = FontWeight.W200,
                letterSpacing = .76.sp,
                color = Color.White,
                modifier = Modifier
                    .clip(RoundedCornerShape(6.dp))
                    .background(statusColor)
                    .padding(horizontal = 6.dp, vertical = 2.dp)
                    .constrainAs(statusView) {
                        linkTo(start = costView.start, end = costView.end)
                        top.linkTo(costView.bottom, 6.dp)
                    }
            )
        }
    }
}

@Preview
@Composable
private fun ListCardTitleComponentPreview() {
    Surface {
        Column(modifier = Modifier.fillMaxWidth()) {
            PrintJobListItemComponent(
                cost = 12f,
                accessCode = "123412",
                spotName = "Office Print at Joey",
                cover = null,
                statusName = "Sending",
                statusColor = Color(0xFF1E88E5),
                createdAt = Date(),

                )

            PrintJobListItemComponent(
                cost = 12f,
                accessCode = "123412",
                spotName = "Office Print at Joey",
                cover = null,
                statusName = "Complete",
                statusColor = Color(0xFF1E88E5),
                createdAt = Date(),
            )
        }
    }
}