package eu.darken.bluemusic.bluetooth.ui.discover

import androidx.compose.foundation.clickable
import androidx.compose.material3.Icon
import androidx.compose.material3.ListItem
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextOverflow
import eu.darken.bluemusic.R
import eu.darken.bluemusic.bluetooth.core.MockDevice
import eu.darken.bluemusic.bluetooth.core.SourceDevice
import eu.darken.bluemusic.bluetooth.core.speaker.FakeSpeakerDevice
import eu.darken.bluemusic.bluetooth.core.toIcon
import eu.darken.bluemusic.common.compose.Preview2
import eu.darken.bluemusic.common.compose.PreviewWrapper

@Composable
fun DeviceItem(
    device: SourceDevice,
    onClick: () -> Unit,
    modifier: Modifier = Modifier
) {
    ListItem(
        headlineContent = {
            Text(
                text = device.label,
                maxLines = 1,
                overflow = TextOverflow.Companion.Ellipsis
            )
        },
        supportingContent = {
            val isSpeaker = device.deviceType == SourceDevice.Type.PHONE_SPEAKER
            Text(
                text = if (isSpeaker) stringResource(R.string.discover_device_speaker_desc) else device.address,
                maxLines = if (isSpeaker) Int.MAX_VALUE else 1,
                overflow = TextOverflow.Companion.Ellipsis
            )
        },
        leadingContent = {
            Icon(
                imageVector = device.deviceType.toIcon(),
                contentDescription = null
            )
        },
        modifier = modifier.clickable(onClick = onClick)
    )
}

@Preview2
@Composable
private fun DeviceItemPreview() {
    PreviewWrapper {
        DeviceItem(
            device = MockDevice(),
            onClick = {},
        )
    }
}

@Preview2
@Composable
private fun DeviceItemSpeakerPreview() {
    PreviewWrapper {
        DeviceItem(
            device = FakeSpeakerDevice(label = "Device speaker (Pixel 7)", isConnected = false),
            onClick = {},
        )
    }
}