/*
 * Copyright (c) 2026 QPQ IaaS AG <info@qpq.swiss>. All rights reserved.
 * Project: Gajumaru Core Java Libraries <gajumaru.io>
 *
 * This program is dual-licensed:
 * 1) Under the GNU Affero General Public License as published by the Free
 *    Software Foundation, either version 3 of the License, or (at your option)
 *    any later version (AGPL-3.0-or-later).
 *
 * 2) Under a commercial/proprietary license available directly from QPQ IaaS AG.
 *    If you wish to use this software outside the strict constraints of the
 *    AGPLv3 (e.g. within a closed-source or proprietary product), you must
 *    purchase a commercial license from QPQ IaaS AG.
 *
 * Authors:
 *   - Craig Everett <craigeverett@qpq.swiss>
 *
 * SPDX-License-Identifier: AGPL-3.0-or-later OR LicenseRef-QPQ-Commercial
 */

package swiss.qpq.gajumobile.ui.screens

import android.content.Intent
import androidx.core.net.toUri
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import swiss.qpq.gajumobile.R
import swiss.qpq.gajumobile.ui.components.SuccessLayout

@Composable
fun TransactionSuccessScreen(
    amount: String,
    recipient: String,
    payload: String,
    txHash: String,
    senderLabel: String,
    walletName: String,
    networkId: String,
    onExit: () -> Unit,
) {
    val context = LocalContext.current
    SuccessLayout(
        title = stringResource(R.string.send_title),
        proceedText = stringResource(R.string.exit_button_caps),
        onProceed = onExit,
        requireRiskAcknowledgment = false,
        content = {
            Text(stringResource(R.string.transaction_success_title), color = MaterialTheme.colorScheme.primary, fontWeight = FontWeight.Bold)
            Spacer(modifier = Modifier.height(32.dp))
            Text(stringResource(R.string.transaction_success_payment_prefix), fontSize = 14.sp, color = MaterialTheme.colorScheme.onBackground)
            Text(amount, fontSize = 24.sp, fontWeight = FontWeight.Bold, color = MaterialTheme.colorScheme.primary)
            Text(stringResource(R.string.transaction_success_to), fontSize = 14.sp, color = MaterialTheme.colorScheme.onBackground)
            Text(recipient, fontSize = 12.sp, color = MaterialTheme.colorScheme.onBackground, maxLines = 1)
            
            if (payload.isNotBlank()) {
                Spacer(modifier = Modifier.height(16.dp))
                Text(stringResource(id = R.string.payload_label), fontSize = 12.sp, color = MaterialTheme.colorScheme.onSurfaceVariant)
                Text(payload, fontSize = 12.sp, fontWeight = FontWeight.Bold, color = MaterialTheme.colorScheme.onBackground)
            }

            Spacer(modifier = Modifier.height(32.dp))
            Text(
                text = stringResource(R.string.transaction_success_subtracted_msg, senderLabel, walletName, networkId), 
                textAlign = TextAlign.Center, 
                fontSize = 12.sp, 
                color = MaterialTheme.colorScheme.onBackground
            )
            
            Spacer(modifier = Modifier.height(32.dp))
            Text(stringResource(R.string.transaction_success_track_hint), fontSize = 12.sp, color = MaterialTheme.colorScheme.onBackground)
            Text(
                text = stringResource(R.string.view_transaction_on_chain), 
                color = MaterialTheme.colorScheme.primary, 
                fontWeight = FontWeight.Bold, 
                fontSize = 12.sp,
                modifier = Modifier
                    .clickable {
                        val url = "https://$networkId.gajumaru.io/tx/$txHash"
                        val intent = Intent(Intent.ACTION_VIEW, url.toUri())
                        context.startActivity(intent)
                    }
                    .padding(8.dp)
            )
        },
    )
}
