package club.clubk.ktag.apps.mine.ui

import android.view.HapticFeedbackConstants
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalView
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import club.clubk.ktag.apps.core.DeviceModel
import club.clubk.ktag.apps.mine.MineState
import club.clubk.ktag.apps.core.ui.theme.KTagDarkGray
import club.clubk.ktag.apps.core.ui.theme.KTagGreen
import club.clubk.ktag.apps.core.ui.theme.KTagRed
import club.clubk.ktag.apps.core.ui.theme.KTagYellow

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun DeviceCard(
    device: DeviceModel,
    mineState: MineState,
    onClick: () -> Unit,
    onLongClick: () -> Unit,
    modifier: Modifier = Modifier
) {
    val view = LocalView.current

    // Only show tagged status when mine is armed
    val showTaggedStatus = mineState is MineState.Armed && device.hasBeenTagged

    val cardColor = KTagDarkGray
    val borderColor = when {
        device.isTaggedOut -> KTagRed
        showTaggedStatus -> KTagRed
        device.inRange -> KTagGreen
        else -> Color.Gray
    }
    val statusText = when {
        device.isTaggedOut -> "TAGGED OUT!"
        showTaggedStatus -> "TAGGED"
        device.inRange -> "IN RANGE"
        else -> "OUT OF RANGE"
    }
    val statusColor = when {
        device.isTaggedOut -> KTagRed
        showTaggedStatus -> KTagRed
        device.inRange -> KTagGreen
        else -> Color.Gray
    }

    Card(
        modifier = modifier
            .fillMaxWidth()
            .padding(horizontal = 8.dp, vertical = 4.dp)
            .border(2.dp, borderColor, RoundedCornerShape(10.dp))
            .combinedClickable(
                onClick = onClick,
                onLongClick = {
                    view.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS)
                    onLongClick()
                }
            ),
        shape = RoundedCornerShape(10.dp),
        elevation = CardDefaults.cardElevation(defaultElevation = 5.dp),
        colors = CardDefaults.cardColors(containerColor = cardColor)
    ) {
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(8.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            // Device icon with color background
            Image(
                painter = painterResource(id = device.image),
                contentDescription = "Device icon",
                modifier = Modifier
                    .size(75.dp)
                    .clip(RoundedCornerShape(4.dp))
                    .background(device.color)
            )

            // Device info
            Column(
                modifier = Modifier
                    .weight(1f)
                    .padding(start = 16.dp),
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                Text(
                    text = device.name,
                    style = MaterialTheme.typography.titleMedium,
                    color = KTagYellow,
                    textAlign = TextAlign.Center
                )
                if (device.deviceType.isNotEmpty()) {
                    Text(
                        text = device.deviceType,
                        style = MaterialTheme.typography.bodySmall,
                        color = KTagYellow,
                        textAlign = TextAlign.Center,
                        modifier = Modifier.padding(top = 4.dp)
                    )
                }
                Text(
                    text = device.bleAddress,
                    style = MaterialTheme.typography.bodySmall,
                    color = KTagYellow,
                    textAlign = TextAlign.Center,
                    modifier = Modifier.padding(top = 4.dp)
                )
                Text(
                    text = "RSSI: ${device.rssi} dBm",
                    style = MaterialTheme.typography.bodySmall,
                    color = KTagYellow,
                    textAlign = TextAlign.Center,
                    modifier = Modifier.padding(top = 4.dp)
                )
                Text(
                    text = statusText,
                    style = MaterialTheme.typography.labelMedium,
                    fontWeight = FontWeight.Bold,
                    color = statusColor,
                    textAlign = TextAlign.Center,
                    modifier = Modifier.padding(top = 4.dp)
                )
            }
        }
    }
}
