TransactionDepositComposable.kt

/*
 * This file is part of GNU Taler
 * (C) 2022 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.wallet.deposit

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import net.taler.common.Amount
import net.taler.common.Timestamp
import net.taler.lib.android.toAbsoluteTime
import net.taler.wallet.BottomInsetsSpacer
import net.taler.wallet.R
import net.taler.wallet.backend.TalerErrorCode.EXCHANGE_GENERIC_KYC_REQUIRED
import net.taler.wallet.backend.TalerErrorInfo
import net.taler.wallet.balances.ScopeInfo
import net.taler.wallet.transactions.AmountType
import net.taler.wallet.transactions.DepositActions
import net.taler.wallet.transactions.ErrorTransactionButton
import net.taler.wallet.transactions.TransactionAction
import net.taler.wallet.transactions.TransactionAction.Abort
import net.taler.wallet.transactions.TransactionAction.Retry
import net.taler.wallet.transactions.TransactionAction.Suspend
import net.taler.wallet.transactions.TransactionAmountComposable
import net.taler.wallet.transactions.TransactionDeposit
import net.taler.wallet.transactions.TransactionMajorState.Pending
import net.taler.wallet.transactions.TransactionMinorState
import net.taler.wallet.transactions.TransactionState
import net.taler.wallet.transactions.TransactionStateComposable
import net.taler.wallet.transactions.TransferOption
import net.taler.wallet.transactions.TransitionsComposable
import net.taler.wallet.withdraw.TransferOptionChooser

@Composable
fun TransactionDepositComposable(
    t: TransactionDeposit,
    devMode: Boolean,
    onSelectOption: (option: TransferOption?) -> Unit,
    onWireTransfer: () -> Unit,
    onShowQrCodes: () -> Unit,
    onConfirmKyc: (url: String) -> Unit,
    onTransition: (t: TransactionAction) -> Unit,
    modifier: Modifier = Modifier,
) {
    val scrollState = rememberScrollState()
    Column(
        modifier = modifier
            .fillMaxWidth()
            .verticalScroll(scrollState),
        horizontalAlignment = Alignment.CenterHorizontally,
    ) {
        val defaultOptionIndex = 0
        var selectedOptionIndex by remember { mutableIntStateOf(defaultOptionIndex) }
        // Transfer options can be absent while the KYC info is present; indexing an empty list crashes.
        val selectedOption =
            t.kycAuthTransferInfo?.transferOptions?.getOrNull(selectedOptionIndex)

        LaunchedEffect(selectedOption) {
            selectedOption?.let { onSelectOption(it) }
        }

        val showOptionChooser = t.kycAuthTransferInfo != null
                && t.kycAuthTransferInfo.transferOptions.size > 1
                && t.txState.minor == TransactionMinorState.KycAuthRequired

        if (showOptionChooser) {
            TransferOptionChooser(
                options = t.kycAuthTransferInfo.transferOptions,
                selectedIndex = selectedOptionIndex,
                onSelectOption = { selectedOptionIndex = it },
            )
            Spacer(Modifier.height(16.dp))
        }

        TransactionStateComposable(state = t.txState)

        Text(
            modifier = Modifier.padding(16.dp),
            text = t.timestamp.ms.toAbsoluteTime(LocalContext.current).toString(),
            style = MaterialTheme.typography.bodyLarge,
        )

        val qrCodes = selectedOption?.qrCodes ?: emptyList()
        DepositActions(t,
            qrCodes = qrCodes,
            onWireTransfer = onWireTransfer,
            onShowQrCodes = onShowQrCodes,
            onConfirmKyc = onConfirmKyc)

        TransactionAmountComposable(
            label = stringResource(id = R.string.amount_chosen),
            amount = t.amountRaw,
            amountType = AmountType.Neutral,
        )

        if (t.amountEffective > t.amountRaw) {
            val fee = t.amountEffective - t.amountRaw
            TransactionAmountComposable(
                label = stringResource(id = R.string.amount_fee),
                amount = fee,
                amountType = AmountType.Negative,
            )
        }

        TransactionAmountComposable(
            label = stringResource(id = R.string.amount_sent),
            amount = t.amountEffective,
            amountType = AmountType.Negative,
        )

        TransitionsComposable(t, devMode, onTransition)
        if (devMode && t.error != null) {
            ErrorTransactionButton(error = t.error)
        }

        BottomInsetsSpacer()
    }
}

@Preview
@Composable
fun TransactionDepositComposablePreview() {
    val t = TransactionDeposit(
        transactionId = "transactionId",
        timestamp = Timestamp.fromMillis(System.currentTimeMillis() - 360 * 60               * 1000),
        txState = TransactionState(Pending),
        txActions = listOf(Retry, Suspend, Abort),
        depositGroupId = "fooBar",
        amountRaw = Amount.fromString("TESTKUDOS", "42.1337"),
        amountEffective = Amount.fromString("TESTKUDOS", "42.23"),
        targetPaytoUri = "https://exchange.example.org/peer/pull/credit",
        error = TalerErrorInfo(code = EXCHANGE_GENERIC_KYC_REQUIRED),
        scopes = listOf(ScopeInfo.Exchange(
            currency = "TESTKUDOS",
            url = "exchange.test.taler.net",
        ))
    )
    Surface {
        TransactionDepositComposable(
            t = t,
            devMode = true,
            onSelectOption = {},
            onConfirmKyc = {},
            onWireTransfer = {},
            onShowQrCodes = {},
            onTransition = {},
        )
    }
}