package eu.darken.sdmse.main.ui.dashboard.cards

import android.app.Activity
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.size
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
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.graphics.Color
import androidx.activity.compose.LocalActivity
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import eu.darken.sdmse.R
import eu.darken.sdmse.common.compose.icons.GooglePlay
import eu.darken.sdmse.common.compose.icons.SdmIcons
import eu.darken.sdmse.common.compose.preview.Preview2
import eu.darken.sdmse.common.compose.preview.PreviewWrapper
import eu.darken.sdmse.main.ui.dashboard.cards.common.DashboardActionIconSpacing
import eu.darken.sdmse.main.ui.dashboard.cards.common.DashboardCard
import eu.darken.sdmse.main.ui.dashboard.cards.common.DashboardFilledActionButton
import eu.darken.sdmse.main.ui.dashboard.cards.common.DashboardFlatActionButton


data class ReviewDashboardCardItem(
    val onReview: (Activity) -> Unit,
    val onDismiss: () -> Unit,
) : DashboardItem {
    override val stableId: Long = this.javaClass.hashCode().toLong()
}

@Composable
internal fun ReviewDashboardCard(item: ReviewDashboardCardItem) {
    val activity = LocalActivity.current
    // 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 dashboard's lazy list retains saveable state per
    // item key, so a latched card removed by a priority cycle would come back still latched. The
    // latch only has to cover the sub-second window until the list drops the card, and disposal on
    // removal is exactly the reset we want.
    var dismissLocked by remember { mutableStateOf(false) }
    var fullyLatched by remember { mutableStateOf(false) }
    val onReview = {
        if (!fullyLatched && activity != null) {
            dismissLocked = true
            item.onReview(activity)
        }
    }
    DashboardCard(
        containerColor = MaterialTheme.colorScheme.secondaryContainer,
        onClick = onReview,
    ) {
        Row(verticalAlignment = Alignment.CenterVertically) {
            Icon(
                painter = painterResource(R.drawable.sdm_happy),
                contentDescription = null,
                modifier = Modifier.size(56.dp),
                tint = Color.Unspecified,
            )
            Spacer(modifier = Modifier.width(16.dp))
            Text(
                text = stringResource(R.string.review_app_body),
                style = MaterialTheme.typography.bodyLarge,
            )
        }

        Spacer(modifier = Modifier.height(8.dp))

        Row(
            modifier = Modifier.fillMaxWidth(),
            verticalAlignment = Alignment.CenterVertically,
        ) {
            DashboardFlatActionButton(
                onClick = {
                    if (!fullyLatched && !dismissLocked) {
                        dismissLocked = true
                        fullyLatched = true
                        item.onDismiss()
                    }
                },
                enabled = !fullyLatched && !dismissLocked,
            ) {
                Text(text = stringResource(R.string.review_app_dismiss_action))
            }
            Spacer(modifier = Modifier.width(8.dp))
            DashboardFilledActionButton(
                modifier = Modifier.weight(1f),
                onClick = onReview,
                enabled = !fullyLatched && activity != null,
            ) {
                Icon(
                    imageVector = SdmIcons.GooglePlay,
                    contentDescription = null,
                )
                Spacer(modifier = Modifier.width(DashboardActionIconSpacing))
                Text(text = stringResource(R.string.review_app_review_action))
            }
        }
    }
}

@Preview2
@Composable
private fun ReviewDashboardCardPreview() {
    PreviewWrapper {
        ReviewDashboardCard(
            item = ReviewDashboardCardItem(
                onReview = {},
                onDismiss = {},
            ),
        )
    }
}
