package solutions.tretter.githugandroid

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.selection.SelectionContainer
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.Alignment
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.layout.boundsInRoot
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp

@Composable
fun ExercisePane(
    level: Level,
    visibleHint: String?,
    suggestionsExpanded: Boolean,
    showDescriptionHint: Boolean,
    onBoundsChanged: (Rect) -> Unit = {},
    onToggleSuggestions: () -> Unit,
    onHint: () -> Unit,
    onReset: () -> Unit,
) {
    Column(
        modifier = Modifier
            .fillMaxWidth()
            .onGloballyPositioned { onBoundsChanged(it.boundsInRoot()) },
        verticalArrangement = Arrangement.spacedBy(12.dp),
    ) {
        if (showDescriptionHint) {
            ExerciseDescriptionHintBubble()
        }
        SelectionContainer {
            Column(verticalArrangement = Arrangement.spacedBy(12.dp)) {
                Text(level.title, style = MaterialTheme.typography.titleLarge, color = TextPrimary)
                Text(level.description, color = TextSecondary)
            }
        }
        Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
            Button(
                onClick = onHint,
                colors = ButtonDefaults.buttonColors(
                    containerColor = Accent,
                    contentColor = Color.Black,
                ),
            ) {
                Text("Hint")
            }
            TextButton(
                onClick = onToggleSuggestions,
                colors = ButtonDefaults.textButtonColors(containerColor = Accent,
                    contentColor = Color.Black,
                    ),
            ) {
                Text("Suggestions")
            }
            TextButton(
                onClick = onReset,
                colors = ButtonDefaults.textButtonColors(contentColor = Accent),
            ) {
                Text("Reset level")
            }
        }
        if (suggestionsExpanded && level.commandSuggestions.isNotEmpty()) {
            SelectionContainer {
                Column(verticalArrangement = Arrangement.spacedBy(4.dp)) {
                    Text("Suggestions", color = TextPrimary, fontWeight = FontWeight.Bold)
                    level.commandSuggestions.forEach { suggestion ->
                        Text(suggestion, color = TextSecondary, fontFamily = FontFamily.Monospace)
                    }
                }
            }
        }
        if (visibleHint != null) {
            SelectionContainer {
                Column(verticalArrangement = Arrangement.spacedBy(4.dp)) {
                    Text("Hint", color = TextPrimary, fontWeight = FontWeight.Bold)
                    Text(visibleHint, color = TextSecondary)
                }
            }
        }
    }
}

@Composable
private fun ExerciseDescriptionHintBubble() {
    val bubbleColor = Color(0xFFFFF1A8)
    Column(
        modifier = Modifier.fillMaxWidth(),
        horizontalAlignment = Alignment.Start,
    ) {
        Box(
            modifier = Modifier
                .background(bubbleColor, RoundedCornerShape(18.dp))
                .padding(horizontal = 14.dp, vertical = 10.dp),
        ) {
            Text(
                text = "Read the exercise description, then solve it by entering commands below.",
                color = Color(0xFF161000),
                fontWeight = FontWeight.Bold,
            )
        }
        Canvas(
            modifier = Modifier
                .padding(start = 28.dp)
                .width(26.dp)
                .height(13.dp),
        ) {
            drawPath(
                path = Path().apply {
                    moveTo(0f, 0f)
                    lineTo(size.width, 0f)
                    lineTo(size.width * 0.25f, size.height)
                    close()
                },
                color = bubbleColor,
            )
        }
    }
}
