package ui

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import model.RankingStock
import model.RankingType
import network.KisTradeService


// src/main/kotlin/ui/MarketStockItemRow.kt
@Composable
fun MarketStockItemRow(
    rank: Int,
    stock: RankingStock,
    onClick: () -> Unit
) {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .clickable { onClick() }
            .padding(vertical = 10.dp, horizontal = 4.dp),
        verticalAlignment = Alignment.CenterVertically
    ) {
        // 순위 표시
        Text(
            text = rank.toString(),
            modifier = Modifier.width(24.dp),
            fontSize = 14.sp,
            fontWeight = FontWeight.Bold,
            color = if (rank <= 3) Color(0xFFE03E2D) else Color.Gray
        )

        Column(modifier = Modifier.weight(1f)) {
            Text(stock.name, fontSize = 13.sp, fontWeight = FontWeight.Medium, maxLines = 1)
            Text(stock.code, fontSize = 10.sp, color = Color.Gray)
        }

        Column(horizontalAlignment = Alignment.End) {
            Text(
                text = String.format("%,d", stock.stck_prpr.toLongOrNull() ?: 0L),
                fontSize = 13.sp,
                fontWeight = FontWeight.Bold
            )
            val rate = stock.prdy_ctrt.toDoubleOrNull() ?: 0.0
            Text(
                text = "${if (rate > 0) "+" else ""}${stock.prdy_ctrt}%",
                fontSize = 11.sp,
                color = if (rate > 0) Color(0xFFE03E2D) else if (rate < 0) Color(0xFF0E62CF) else Color.DarkGray
            )
        }
    }
}