package com.fabledsword.minstrel.player.output

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import com.composables.icons.lucide.Bluetooth
import com.composables.icons.lucide.Cast
import com.composables.icons.lucide.ChevronDown
import com.composables.icons.lucide.Headphones
import com.composables.icons.lucide.Lucide
import com.composables.icons.lucide.Smartphone
import com.composables.icons.lucide.Speaker

/**
 * Spotify-style chip showing the current output route. Sits between
 * BottomActionsRow and ScrubberRow in NowPlayingScreen. Tap to open
 * the picker sheet.
 *
 * Visibility rule per the spec: hidden when the route list has
 * exactly one entry AND that entry is BuiltIn — no reason to surface
 * a picker for "the only thing available." Visibility logic owned
 * by the caller (NowPlayingScreen).
 */
@Composable
fun DeviceChip(
    route: OutputRoute,
    onClick: () -> Unit,
    modifier: Modifier = Modifier,
) {
    Surface(
        modifier = modifier,
        onClick = onClick,
        shape = MaterialTheme.shapes.medium,
        color = MaterialTheme.colorScheme.surfaceVariant,
    ) {
        Row(
            modifier = Modifier.padding(horizontal = 12.dp, vertical = 8.dp),
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.spacedBy(8.dp),
        ) {
            Icon(
                imageVector = iconFor(route.kind),
                contentDescription = null,
                tint = MaterialTheme.colorScheme.onSurfaceVariant,
                modifier = Modifier.size(CHIP_ICON_DP.dp),
            )
            Text(
                text = route.name,
                style = MaterialTheme.typography.labelLarge,
                color = MaterialTheme.colorScheme.onSurfaceVariant,
                maxLines = 1,
                overflow = TextOverflow.Ellipsis,
            )
            Icon(
                imageVector = Lucide.ChevronDown,
                contentDescription = null,
                tint = MaterialTheme.colorScheme.onSurfaceVariant,
                modifier = Modifier.size(CHIP_CHEVRON_DP.dp),
            )
        }
    }
}

internal fun iconFor(kind: OutputRoute.Kind): ImageVector = when (kind) {
    OutputRoute.Kind.BuiltIn -> Lucide.Smartphone
    OutputRoute.Kind.Wired -> Lucide.Headphones
    OutputRoute.Kind.Bluetooth -> Lucide.Bluetooth
    OutputRoute.Kind.Cast -> Lucide.Cast
    OutputRoute.Kind.Other -> Lucide.Speaker
}

private const val CHIP_ICON_DP = 16
private const val CHIP_CHEVRON_DP = 14
