AmountKeypad.kt

/*
 * This file is part of GNU Taler
 * (C) 2026 Taler Systems S.A.
 *
 * GNU Taler is free software; you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 3, or (at your option) any later version.
 *
 * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */

package net.taler.merchantpos.compose

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
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.platform.LocalConfiguration
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

import net.taler.merchantpos.R

@Composable
fun AmountKeypad(
    isTabletLayout: Boolean,
    chargeEnabled: Boolean,
    onDigitPressed: (Char) -> Unit,
    onClearPressed: () -> Unit,
    onBackspacePressed: () -> Unit,
    onChargePressed: () -> Unit,
    modifier: Modifier = Modifier,
    /** Label of the action button; defaults to creating the order. */
    chargeLabel: String? = null,
)
{
    val configuration = LocalConfiguration.current
    val isCompactPhone = !isTabletLayout && configuration.screenHeightDp <= 720
    val rowSpacing = if (isCompactPhone) 6.dp else 8.dp
    val digitFontSize = if (isCompactPhone) 24.sp else 28.sp

    Column(
        modifier = modifier,
        verticalArrangement = Arrangement.spacedBy(rowSpacing),
    ) {
        val keyContainerColor = MaterialTheme.colorScheme.primaryContainer
        val keyContentColor = MaterialTheme.colorScheme.onPrimaryContainer
        val clearLabel = stringResource(R.string.amount_entry_clear)
        val clearTextSize =
            when
            {
                clearLabel.length >= 14 -> if (isCompactPhone) 12.sp else 14.sp
                clearLabel.length >= 10 -> if (isCompactPhone) 14.sp else 16.sp
                else -> if (isCompactPhone) 16.sp else 20.sp
            }

        listOf(
            listOf("1", "2", "3"),
            listOf("4", "5", "6"),
            listOf("7", "8", "9"),
        ).forEach { row ->
            Row(
                modifier =
                    Modifier
                        .fillMaxWidth()
                        .weight(1f),
                horizontalArrangement = Arrangement.spacedBy(rowSpacing),
            ) {
                row.forEach { key ->
                    AmountKeyButton(
                        text = key,
                        modifier = Modifier.weight(1f),
                        containerColor = keyContainerColor,
                        contentColor = keyContentColor,
                        fontSize = digitFontSize,
                        onClick = { onDigitPressed(key.first()) },
                    )
                }
            }
        }

        Row(
            modifier =
                Modifier
                    .fillMaxWidth()
                    .weight(1f),
            horizontalArrangement = Arrangement.spacedBy(rowSpacing),
        ) {
            AmountKeyButton(
                text = clearLabel,
                modifier = Modifier.weight(1f),
                containerColor = keyContainerColor,
                contentColor = keyContentColor,
                fontSize = clearTextSize,
                onClick = onClearPressed,
            )
            AmountKeyButton(
                text = "0",
                modifier = Modifier.weight(1f),
                containerColor = keyContainerColor,
                contentColor = keyContentColor,
                onClick = { onDigitPressed('0') },
            )
            Button(
                modifier =
                    Modifier
                        .weight(1f)
                        .fillMaxSize(),
                onClick = onBackspacePressed,
                colors =
                    ButtonDefaults.buttonColors(
                        containerColor = keyContainerColor,
                        contentColor = keyContentColor,
                    ),
                contentPadding = PaddingValues(0.dp),
            ) {
                Icon(
                    painter = painterResource(R.drawable.ic_backspace),
                    contentDescription = stringResource(R.string.amount_entry_backspace),
                    tint = keyContentColor,
                )
            }
        }

        Button(
            modifier =
                Modifier
                    .fillMaxWidth()
                    .height(56.dp),
            onClick = onChargePressed,
            enabled = chargeEnabled,
            colors =
                ButtonDefaults.buttonColors(
                    containerColor = colorResource(R.color.colorPrimary),
                    contentColor = colorResource(R.color.colorOnPrimary),
                    disabledContainerColor = MaterialTheme.colorScheme.surfaceVariant,
                    disabledContentColor = MaterialTheme.colorScheme.onSurfaceVariant,
                ),
        ) {
            Text(
                text = chargeLabel ?: stringResource(R.string.amount_entry_create_order_charge),
                fontWeight = FontWeight.SemiBold,
            )
        }
    }
}

@Composable
private fun AmountKeyButton(
    text: String,
    modifier: Modifier,
    containerColor: Color,
    contentColor: Color,
    fontSize: TextUnit? = null,
    onClick: () -> Unit,
)
{
    Button(
        modifier = modifier.fillMaxSize(),
        onClick = onClick,
        colors =
            ButtonDefaults.buttonColors(
                containerColor = containerColor,
                contentColor = contentColor,
            ),
        contentPadding = PaddingValues(horizontal = 2.dp, vertical = 0.dp),
    ) {
        Text(
            text = text,
            textAlign = TextAlign.Center,
            maxLines = 1,
            softWrap = false,
            overflow = TextOverflow.Ellipsis,
            style =
                fontSize?.let {
                    MaterialTheme.typography.headlineMedium.copy(
                        fontSize = it,
                        fontWeight = FontWeight.SemiBold,
                    )
                } ?: MaterialTheme.typography.headlineMedium.copy(
                    fontWeight = FontWeight.SemiBold,
                ),
        )
    }
}