package solutions.tretter.githugandroid

import androidx.compose.foundation.horizontalScroll
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

@Composable
internal fun SpecialKeyBar(
    onTab: () -> Unit,
    onHelp: () -> Unit,
    onCursorLeft: () -> Unit,
    onCursorRight: () -> Unit,
    onHistoryUp: () -> Unit,
    onHistoryDown: () -> Unit,
    onInsertText: (String) -> Unit,
) {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .horizontalScroll(rememberScrollState()),
        horizontalArrangement = Arrangement.spacedBy(1.dp),
    ) {
        TerminalKeyButton(label = "?", onClick = onHelp, highlight = true, fontFamily = FontFamily.Default)
        TerminalKeyButton(label = "↹", onClick = onTab, fontFamily = FontFamily.Default)
        TerminalKeyButton(label = ".", onClick = { onInsertText(".") })
        TerminalKeyButton(label = "-", onClick = { onInsertText("-") })
        TerminalKeyButton(label = "/", onClick = { onInsertText("/") })
        TerminalKeyButton(label = "←", onClick = onCursorLeft)
        TerminalKeyButton(label = "→", onClick = onCursorRight)
        TerminalKeyButton(label = "↑", onClick = onHistoryUp)
        TerminalKeyButton(label = "↓", onClick = onHistoryDown)
    }
}

@Composable
private fun TerminalKeyButton(
    label: String,
    onClick: () -> Unit,
    highlight: Boolean = false,
    fontFamily: FontFamily = FontFamily.Monospace,
) {
    Button(
        onClick = onClick,
        modifier = Modifier
            .width(32.dp)
            .height(26.dp),
        shape = RoundedCornerShape(0.dp),
        contentPadding = PaddingValues(horizontal = 0.dp, vertical = 0.dp),
        colors = ButtonDefaults.buttonColors(
            containerColor = if (highlight) Color(0xFF1976D2) else PanelSecondary,
            contentColor = TextPrimary,
        ),
    ) {
        Text(
            text = label,
            fontFamily = fontFamily,
            fontWeight = FontWeight.Bold,
            fontSize = 11.sp,
        )
    }
}
