package com.jsjdesigns.poker

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.systemBarsPadding
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Checkbox
import androidx.compose.material3.CheckboxDefaults
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.OutlinedTextFieldDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
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 androidx.lifecycle.compose.collectAsStateWithLifecycle

private val SetupFelt = Color(0xFF0B3D26)
private val TableGold = Color(0xFFE3C179)

@Composable
fun PokerApp(vm: PokerViewModel) {
    val state by vm.state.collectAsStateWithLifecycle()
    if (state.gameConfig == null) {
        TakeASeatScreen(onStart = vm::startGame)
    } else {
        TableScreen(vm)
    }
}

@Composable
private fun TakeASeatScreen(onStart: (String, Boolean) -> Unit) {
    var playerName by rememberSaveable { mutableStateOf("") }
    var coachEnabled by rememberSaveable { mutableStateOf(false) }

    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(SetupFelt)
            .systemBarsPadding()
            .padding(24.dp),
        verticalArrangement = Arrangement.Center,
    ) {
        Text(
            "JSJ Hold’em",
            color = TableGold,
            fontWeight = FontWeight.Bold,
            fontSize = 34.sp,
        )
        Text(
            "Take a seat",
            color = Color.White,
            fontWeight = FontWeight.Medium,
            fontSize = 22.sp,
        )
        Spacer(Modifier.height(24.dp))

        OutlinedTextField(
            value = playerName,
            onValueChange = { playerName = it.take(MAX_PLAYER_NAME_LENGTH) },
            modifier = Modifier.fillMaxWidth(),
            label = { Text("Your name (optional)") },
            placeholder = { Text("You") },
            singleLine = true,
            colors = OutlinedTextFieldDefaults.colors(
                focusedTextColor = Color.White,
                unfocusedTextColor = Color.White,
                focusedBorderColor = TableGold,
                unfocusedBorderColor = Color.White.copy(alpha = 0.4f),
                focusedLabelColor = TableGold,
                unfocusedLabelColor = Color.White.copy(alpha = 0.6f),
                cursorColor = TableGold,
                focusedPlaceholderColor = Color.White.copy(alpha = 0.35f),
                unfocusedPlaceholderColor = Color.White.copy(alpha = 0.35f),
            ),
        )

        Spacer(Modifier.height(16.dp))

        Row(
            modifier = Modifier
                .fillMaxWidth()
                .clickable { coachEnabled = !coachEnabled }
                .padding(vertical = 8.dp),
            verticalAlignment = Alignment.CenterVertically,
        ) {
            Checkbox(
                checked = coachEnabled,
                onCheckedChange = null,
                colors = CheckboxDefaults.colors(
                    checkedColor = TableGold,
                    checkmarkColor = SetupFelt,
                    uncheckedColor = Color.White.copy(alpha = 0.55f),
                ),
            )
            Column {
                Text("Post-action coach", color = Color.White, fontWeight = FontWeight.Medium)
                Text(
                    "Reviews your choice after you act. No hints during decisions.",
                    color = Color.White.copy(alpha = 0.5f),
                    fontSize = 12.sp,
                )
            }
        }

        Spacer(Modifier.height(8.dp))

        Card(
            colors = CardDefaults.cardColors(containerColor = Color.White.copy(alpha = 0.08f)),
            modifier = Modifier.fillMaxWidth(),
        ) {
            Column(Modifier.padding(16.dp)) {
                Text("Six-max cash game", color = Color.White, fontWeight = FontWeight.Bold)
                Spacer(Modifier.height(6.dp))
                Text(
                    "Buy-in  $DEFAULT_BUY_IN     Blinds  $DEFAULT_SMALL_BLIND / $DEFAULT_BIG_BLIND",
                    color = TableGold,
                )
                Spacer(Modifier.height(8.dp))
                Text(
                    "Ada · Bruno · Cleo · Dex · Enzo",
                    color = Color.White.copy(alpha = 0.72f),
                    fontSize = 13.sp,
                )
                Text(
                    "A calibrated mix of skill levels and playing styles.",
                    color = Color.White.copy(alpha = 0.48f),
                    fontSize = 12.sp,
                )
            }
        }

        Spacer(Modifier.height(24.dp))

        Button(
            onClick = { onStart(playerName, coachEnabled) },
            modifier = Modifier.fillMaxWidth(),
            colors = ButtonDefaults.buttonColors(
                containerColor = Color(0xFF2C6E49),
                contentColor = Color.White,
            ),
        ) {
            Text("Buy in for $DEFAULT_BUY_IN")
        }
    }
}
