package club.clubk.ktag.apps.koth.ui

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import club.clubk.ktag.apps.koth.KothState
import club.clubk.ktag.apps.koth.ui.theme.FourteenSegmentFont
import club.clubk.ktag.apps.koth.ui.theme.KothBlack
import club.clubk.ktag.apps.koth.ui.theme.KothBlue
import club.clubk.ktag.apps.koth.ui.theme.KothGreen
import club.clubk.ktag.apps.koth.ui.theme.KothRed

@Composable
fun StatusDisplay(
    statusText: String,
    gameState: KothState,
    modifier: Modifier = Modifier
) {
    val displayText = when (gameState) {
        is KothState.Playing -> buildPlayingStatusText(statusText)
        else -> buildAnnotatedString {
            withStyle(SpanStyle(color = KothGreen)) {
                append(statusText)
            }
        }
    }

    Text(
        text = displayText,
        fontFamily = FourteenSegmentFont,
        fontSize = 36.sp,
        textAlign = TextAlign.Center,
        lineHeight = 44.sp,
        modifier = modifier
            .clip(RoundedCornerShape(8.dp))
            .background(KothBlack.copy(alpha = 0.9f))
            .padding(20.dp)
    )
}

private fun buildPlayingStatusText(statusText: String): AnnotatedString {
    return buildAnnotatedString {
        // Parse "Red: X\n\nBlue: Y" format
        val lines = statusText.split("\n\n")
        if (lines.size == 2) {
            withStyle(SpanStyle(color = KothRed)) {
                append(lines[0])
            }
            append("\n\n")
            withStyle(SpanStyle(color = KothBlue)) {
                append(lines[1])
            }
        } else {
            withStyle(SpanStyle(color = KothGreen)) {
                append(statusText)
            }
        }
    }
}

@Composable
fun EndGameStatusDisplay(
    resultText: String,
    redTimeText: String,
    blueTimeText: String,
    modifier: Modifier = Modifier
) {
    val displayText = buildAnnotatedString {
        withStyle(SpanStyle(color = KothGreen)) {
            append(resultText)
            append("\n")
        }
        withStyle(SpanStyle(color = KothRed)) {
            append(redTimeText)
            append("\n")
        }
        withStyle(SpanStyle(color = KothBlue)) {
            append(blueTimeText)
        }
    }

    Text(
        text = displayText,
        fontFamily = FourteenSegmentFont,
        fontSize = 36.sp,
        textAlign = TextAlign.Center,
        lineHeight = 44.sp,
        modifier = modifier
            .clip(RoundedCornerShape(8.dp))
            .background(KothBlack.copy(alpha = 0.9f))
            .padding(20.dp)
    )
}
