package eu.darken.bluemusic.devices.ui.dashboard.rows

import androidx.compose.animation.animateContentSize
import androidx.compose.foundation.layout.Arrangement
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.width
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import eu.darken.bluemusic.R
import eu.darken.bluemusic.common.compose.BlueMusicIcon
import eu.darken.bluemusic.common.compose.Preview2
import eu.darken.bluemusic.common.compose.PreviewWrapper

/**
 * Asks the user to leave a review. [onReview] is null when no hosting Activity is available, Play's
 * in-app review flow can't be launched without one, so the action is shown disabled instead.
 */
@Composable
fun ReviewCard(
    onReview: (() -> Unit)?,
    onDismiss: () -> Unit
) {
    // The card only disappears with the next state emission, so the tap targets need a latch. It is
    // asymmetric on purpose: the harmful orderings are a dismiss after a review (which overwrites
    // the review bookkeeping with a snooze) and a review after a dismiss. A repeated review tap is
    // harmless, the tool's single-flight lock absorbs it, and blocking it here would leave a dead
    // card whenever a Play request fails and nothing gets persisted.
    // Plain remember, not rememberSaveable: the card lives in a lazy list, whose host retains
    // saveable state across item removal, so a latched card that left the list would come back
    // latched. Disposal on removal is the intended reset and the latch window is sub-second.
    var dismissLocked by remember { mutableStateOf(false) }
    var fullyLatched by remember { mutableStateOf(false) }
    Card(
        modifier = Modifier
            .fillMaxWidth()
            .padding(horizontal = 16.dp, vertical = 8.dp)
            .animateContentSize(),
        colors = CardDefaults.cardColors(
            containerColor = MaterialTheme.colorScheme.secondaryContainer
        )
    ) {
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .padding(16.dp)
        ) {
            Row(
                verticalAlignment = Alignment.CenterVertically
            ) {
                BlueMusicIcon(
                    size = 40.dp,
                    contentDescription = null,
                )
                Spacer(modifier = Modifier.width(12.dp))
                Text(
                    text = stringResource(R.string.review_app_body),
                    style = MaterialTheme.typography.bodyMedium,
                    color = MaterialTheme.colorScheme.onSecondaryContainer
                )
            }
            Spacer(modifier = Modifier.height(12.dp))
            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.End,
                verticalAlignment = Alignment.CenterVertically
            ) {
                TextButton(
                    onClick = {
                        if (!fullyLatched && !dismissLocked) {
                            dismissLocked = true
                            fullyLatched = true
                            onDismiss()
                        }
                    },
                    enabled = !fullyLatched && !dismissLocked,
                ) {
                    Text(stringResource(R.string.review_app_dismiss_action))
                }
                Spacer(modifier = Modifier.width(8.dp))
                Button(
                    onClick = {
                        if (!fullyLatched && onReview != null) {
                            dismissLocked = true
                            onReview.invoke()
                        }
                    },
                    enabled = onReview != null && !fullyLatched,
                    colors = ButtonDefaults.buttonColors(
                        containerColor = MaterialTheme.colorScheme.primary
                    )
                ) {
                    Text(stringResource(R.string.review_app_review_action))
                }
            }
        }
    }
}

@Preview2
@Composable
fun ReviewCardPreview() {
    PreviewWrapper {
        ReviewCard(
            onReview = {},
            onDismiss = {}
        )
    }
}
