// src/main/kotlin/ui/StockHeader.kt
package ui

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.*
import androidx.compose.runtime.Composable
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.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

@OptIn(ExperimentalMaterialApi::class)
@Composable
fun StockHeader(
    name: String,
    code: String,
    isDomestic: Boolean,
    previousClose: String, // 추가: 전일 종가
    openPrice: String,     // 추가: 금일 시가
    resultMessage: String,
    resultMessageClear : ()->Unit,
    isSuccess: Boolean
) {
    Column(modifier = Modifier.wrapContentWidth()) {
        // [1] 알림 메시지 영역 (기존 동일)
        if (resultMessage.isNotEmpty()) {
            Surface(
                color = if (isSuccess) Color(0xFF4CAF50) else Color(0xFFF44336),
                modifier = Modifier.padding(bottom = 8.dp),
                shape = RoundedCornerShape(4.dp),
                onClick = {
                    resultMessageClear.invoke()
                }
            ) {
                Text(text = resultMessage, color = Color.White, modifier = Modifier.padding(8.dp), fontWeight = FontWeight.Bold)
            }
        }

        // [2] 종목명 및 정보
        Row(verticalAlignment = Alignment.CenterVertically) {
            Surface(
                color = if (isDomestic) Color(0xFFE03E2D) else Color(0xFF0E62CF),
                shape = RoundedCornerShape(4.dp)
            ) {
                Text(text = if (isDomestic) "국내" else "해외", color = Color.White, fontSize = 10.sp, modifier = Modifier.padding(horizontal = 6.dp, vertical = 2.dp))
            }
            Spacer(modifier = Modifier.width(8.dp))
            Text(text = name, style = MaterialTheme.typography.h5, fontWeight = FontWeight.Bold)
            Spacer(modifier = Modifier.width(6.dp))
            Text(text = "($code)", color = Color.Gray)
        }

        // [3] 전일 종가 및 시가 정보 행 추가
        Row(modifier = Modifier.padding(top = 4.dp)) {
            PriceSummaryItem("전일 종가", previousClose)
            Spacer(modifier = Modifier.width(16.dp))
            PriceSummaryItem("금일 시가", openPrice)
        }
    }
}

@Composable
fun PriceSummaryItem(label: String, price: String) {
    Row(verticalAlignment = Alignment.CenterVertically) {
        Text(text = label, fontSize = 11.sp, color = Color.Gray)
        Spacer(modifier = Modifier.width(4.dp))
        val formattedPrice = price.toLongOrNull()?.let { String.format("%,d", it) } ?: price
        Text(text = "${formattedPrice}원", fontSize = 12.sp, fontWeight = FontWeight.Medium)
    }
}