package fuck.andes.ui.components
import fuck.andes.R
import androidx.compose.ui.res.stringResource

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.size
import androidx.compose.foundation.layout.width
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import fuck.andes.ui.model.ActiveRunSummaryUi
import fuck.andes.ui.model.RunStatusUi
import top.yukonga.miuix.kmp.basic.ButtonDefaults
import top.yukonga.miuix.kmp.basic.Card
import top.yukonga.miuix.kmp.basic.InfiniteProgressIndicator
import top.yukonga.miuix.kmp.basic.Text
import top.yukonga.miuix.kmp.basic.TextButton
import androidx.compose.ui.res.painterResource
import com.composables.icons.lucide.R as LucideR
import top.yukonga.miuix.kmp.basic.Icon
import top.yukonga.miuix.kmp.theme.MiuixTheme

@Composable
fun AgentStatusCard(
    activeRun: ActiveRunSummaryUi,
    onOpenRun: () -> Unit,
    onStopRun: () -> Unit,
    modifier: Modifier = Modifier,
) {
    Card(
        modifier = modifier
            .fillMaxWidth()
            .padding(horizontal = 12.dp, vertical = 6.dp),
        onClick = onOpenRun,
    ) {
        Column(modifier = Modifier.padding(16.dp)) {
            Row(verticalAlignment = Alignment.CenterVertically) {
                StatusIndicator(status = activeRun.status)
                Spacer(modifier = Modifier.width(10.dp))
                Text(
                    text = activeRun.title,
                    modifier = Modifier.weight(1f),
                    style = MiuixTheme.textStyles.headline1,
                    color = MiuixTheme.colorScheme.onSurfaceContainer,
                    maxLines = 1,
                    overflow = TextOverflow.Ellipsis,
                )
            }
            Spacer(modifier = Modifier.height(8.dp))
            Text(
                text = activeRun.currentStep,
                style = MiuixTheme.textStyles.body2,
                color = MiuixTheme.colorScheme.onSurfaceVariantSummary,
            )
            Spacer(modifier = Modifier.height(12.dp))
            Row(verticalAlignment = Alignment.CenterVertically) {
                Text(
                    text = activeRun.elapsedLabel,
                    modifier = Modifier.weight(1f),
                    style = MiuixTheme.textStyles.footnote1,
                    color = MiuixTheme.colorScheme.onSurfaceVariantActions,
                )
                if (activeRun.status == RunStatusUi.Running) {
                    TextButton(
                        text = stringResource(R.string.ui_stop_a17f70),
                        onClick = onStopRun,
                        colors = ButtonDefaults.textButtonColors(),
                    )
                }
            }
        }
    }
}

@Composable
private fun StatusIndicator(status: RunStatusUi) {
    when (status) {
        RunStatusUi.Running -> InfiniteProgressIndicator(
            modifier = Modifier.size(18.dp),
            color = MiuixTheme.colorScheme.primary,
        )
        RunStatusUi.Success -> Icon(
            painter = painterResource(LucideR.drawable.lucide_ic_check),
            contentDescription = null,
            modifier = Modifier.size(18.dp),
            tint = MiuixTheme.colorScheme.primary,
        )
        RunStatusUi.Failed -> Icon(
            painter = painterResource(LucideR.drawable.lucide_ic_x),
            contentDescription = null,
            modifier = Modifier.size(18.dp),
            tint = MiuixTheme.colorScheme.primary,
        )
        RunStatusUi.Cancelled -> Icon(
            painter = painterResource(LucideR.drawable.lucide_ic_ellipsis),
            contentDescription = null,
            modifier = Modifier.size(18.dp),
            tint = MiuixTheme.colorScheme.onSurfaceVariantActions,
        )
    }
}
